gpt4 book ai didi

java - org.json.JSONException : Value OR-12345 at order_no of type java. lang.String 无法转换为 int

转载 作者:行者123 更新时间:2023-11-30 00:06:47 25 4
gpt4 key购买 nike

我正在尝试使用来自虚拟 api 的 json 数据填充回收器 View ,但它不起作用。我几乎尝试了堆栈溢出中给出的几种解决方案。

下面是我的代码:

public class loadOrdersList extends AsyncTask<Void, Void, Void> {

@Override
protected void onPreExecute() {
ordersList = new ArrayList<>();
rvor = findViewById(R.id.recycler_view_orders);
rvor.setHasFixedSize(true);
rvor.setLayoutManager(new LinearLayoutManager(OrdersActivity.this));
}

@Override
protected void onPostExecute(Void aVoid) {

if (new CheckNetworkUtil(OrdersActivity.this).isNetworkAvailable()) {
Log.d("TEST", "------------------ordersList: " + ordersList.size());
OrdersAdapter adapter = new OrdersAdapter(getApplicationContext(), ordersList);
rvor.setAdapter(adapter);
srl.setRefreshing(false);
} else
Toast.makeText(OrdersActivity.this, "No Internet Connection!", Toast.LENGTH_SHORT).show();
}

@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}

@Override
protected Void doInBackground(Void... voids) {

try {
OkHttpClient client = new OkHttpClient();

final Request request = new Request.Builder()
.url("https://api.myjson.com/bins/la2gh")
.build();

Response responses = client.newCall(request).execute();

JSONArray orders = new JSONArray(responses.body().string());

ordersList = new ArrayList<>();

for (int i = 0; i < orders.length(); i++) {
JSONObject name = orders.getJSONObject(i);
String customerName = name.getString("customer_name");
String agentAssigned = name.getString("agent_assigned");
String orderId = name.getString("order_id");
Integer totalQuantity = name.getInt("total_quantity");
String orderDate = name.getString("order_date");
Integer orderNo = name.getInt("order_no");
String schoolYear = name.getString("school_year");
String company = name.getString("company");
String deliveryDate = name.getString("delivery_date");
String orderStatus = name.getString("order_status");
Integer grossRevenue = name.getInt("gross_revenue");
Integer netRevenue = name.getInt("net_revenue");
Integer totalOrdered = name.getInt("total");
Integer grandTotalOrdered = name.getInt("grand_total");
OrderModel orderModel = new OrderModel(customerName
,agentAssigned
,orderId
,totalQuantity
,orderDate
,orderNo
,schoolYear
,company
,deliveryDate
,orderStatus
,grossRevenue
,netRevenue
,totalOrdered
,grandTotalOrdered);

ordersList.add(orderModel);
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
}

我怀疑执行时字符串不支持整数

最佳答案

你期望 order_no 是一个整数,因此写成这个整数 orderNo = name.getInt("order_no");但是您在回收站 View 中收到了 orderNo 的字母数字值。因此,您必须将 OrderModelorderNo 属性的类型从 Integer 更改为 String 或将值限制为 Integer 类型而已。

关于java - org.json.JSONException : Value OR-12345 at order_no of type java. lang.String 无法转换为 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48832353/

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