gpt4 book ai didi

java - Android:在各个 TextView 中使用 stringrequest 的结果

转载 作者:行者123 更新时间:2023-12-02 02:50:49 25 4
gpt4 key购买 nike

我已经能够使用 volley 从 url 获取字符串。该字符串现在在 TextView 中显示为一个 block 。但我希望能够在单独的 TextView 中显示这些数据。我怎样才能做到这一点?

也许重要的是要知道:我在编程方面是全新的,这是我这样做的第一周。所以我使用的方法可能很奇怪,这可能是一个愚蠢的问题,但我只是想学习,并得到我想要的结果。

这是我现在的代码,用于从 url 获取数据:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);
textView = findViewById(R.id.text);
queue = Volley.newRequestQueue(this);
StringRequest request = new StringRequest(Request.Method.GET, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
textView.setText(response.toString());
Toast.makeText(MainActivity.this,response.toString(),Toast.LENGTH_LONG).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("error",error.toString());

}
});

queue.add(request);

这就是从 url 获取结果的样子:

{"DeliveryDetailId":91003,"Delivery":{"DeliveryId":91,"DeliveryNumber":"1248","DropLocation":null,"DeliveryState":0},"ProductNumber":null,"Description":null,"PickLocation":"104","LocationCheck":null,"Quantity":64.0,"Histories":[],"BinNumberToUse":null}

因此,在这种情况下,我想要显示 DeliveryID、Picklocation 和 Quantity 的 TextView 。如何从字符串中提取此信息,以便可以在 TextView 中显示它?

最佳答案

创建如下所示的模型类并将响应存储在 Response.class 中然后您可以通过调用 getDeliveryID() 来访问 DeliveryID

  -----------------------------------com.saranga.app.model.Delivery.java-----------------------------------


package com.saranga.app.model;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Delivery {

@SerializedName("DeliveryId")
@Expose
private Integer deliveryId;
@SerializedName("DeliveryNumber")
@Expose
private String deliveryNumber;
@SerializedName("DropLocation")
@Expose
private Object dropLocation;
@SerializedName("DeliveryState")
@Expose
private Integer deliveryState;

public Integer getDeliveryId() {
return deliveryId;
}

public void setDeliveryId(Integer deliveryId) {
this.deliveryId = deliveryId;
}

public String getDeliveryNumber() {
return deliveryNumber;
}

public void setDeliveryNumber(String deliveryNumber) {
this.deliveryNumber = deliveryNumber;
}

public Object getDropLocation() {
return dropLocation;
}

public void setDropLocation(Object dropLocation) {
this.dropLocation = dropLocation;
}

public Integer getDeliveryState() {
return deliveryState;
}

public void setDeliveryState(Integer deliveryState) {
this.deliveryState = deliveryState;
}

}
-----------------------------------com.saranga.app.model.Response.java-----------------------------------

package com.saranga.app.model;

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Response {

@SerializedName("DeliveryDetailId")
@Expose
private Integer deliveryDetailId;
@SerializedName("Delivery")
@Expose
private Delivery delivery;
@SerializedName("ProductNumber")
@Expose
private Object productNumber;
@SerializedName("Description")
@Expose
private Object description;
@SerializedName("PickLocation")
@Expose
private String pickLocation;
@SerializedName("LocationCheck")
@Expose
private Object locationCheck;
@SerializedName("Quantity")
@Expose
private Double quantity;
@SerializedName("Histories")
@Expose
private List<Object> histories = null;
@SerializedName("BinNumberToUse")
@Expose
private Object binNumberToUse;

public Integer getDeliveryDetailId() {
return deliveryDetailId;
}

public void setDeliveryDetailId(Integer deliveryDetailId) {
this.deliveryDetailId = deliveryDetailId;
}

public Delivery getDelivery() {
return delivery;
}

public void setDelivery(Delivery delivery) {
this.delivery = delivery;
}

public Object getProductNumber() {
return productNumber;
}

public void setProductNumber(Object productNumber) {
this.productNumber = productNumber;
}

public Object getDescription() {
return description;
}

public void setDescription(Object description) {
this.description = description;
}

public String getPickLocation() {
return pickLocation;
}

public void setPickLocation(String pickLocation) {
this.pickLocation = pickLocation;
}

public Object getLocationCheck() {
return locationCheck;
}

public void setLocationCheck(Object locationCheck) {
this.locationCheck = locationCheck;
}

public Double getQuantity() {
return quantity;
}

public void setQuantity(Double quantity) {
this.quantity = quantity;
}

public List<Object> getHistories() {
return histories;
}

public void setHistories(List<Object> histories) {
this.histories = histories;
}

public Object getBinNumberToUse() {
return binNumberToUse;
}

public void setBinNumberToUse(Object binNumberToUse) {
this.binNumberToUse = binNumberToUse;
}

}

关于java - Android:在各个 TextView 中使用 stringrequest 的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57109711/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com