我被这个错误困扰。我正在检索要在Android Studio的recyclerview中显示的JSON对象。我已经根据此处的先前答案进行了一些更改,但似乎没有什么不同。
该代码接收带有嵌套JSON数组(“数据”)的JSON对象,其中包含收据列表。
示例JSON
{
"status": true,
"message": "Data fetched successfully",
"data": [
{
"id": "2",
"receipt_id": "2",
"parent_id": "0",
"has_children": "0",
"product_id": "4",
"product_code": "24904",
"product_name": "LIGHT SOLAR S/STEEL GLASS LENS COLOUR CHANGE PDQ H25.5CM",
"quantity_expected": "120",
"quantity_received": "120",
"quantity_putaway": "120",
"quantity_held": "0",
"reason_held": "",
"unit_quantity": "0",
"subinner_uom": "",
"subinner_quantity": "0",
"subinner_unit_quantity": "0",
"subinner_weight": "0.000",
"subinner_cubic": "0.000000",
"inner_uom": "",
"inner_quantity": "0",
"inner_unit_quantity": "0",
"inner_weight": "0.000",
"inner_cubic": "0.000000",
"outer_expected": "10",
"outer_uom": "CTN",
"outer_quantity": "10",
"outer_unit_quantity": "12",
"outer_weight": "0.000",
"outer_cubic": "0.000000",
"outer_length": "0.000",
"outer_width": "0.000",
"outer_height": "0.000",
"pallet_uom": "",
"pallet_quantity": "0",
"pallet_unit_quantity": "0",
"pallet_weight": "0.000",
"pallet_cubic": "0.000000",
"batch_number": " ",
"date_expiry": "0000-00-00",
"serial_number": "",
"length": "36.000",
"width": "29.000",
"height": "29.000",
"cubic": "0.030276",
"weight": "3.000",
"barcode": "",
"cost_current": "0.00",
"status": "0",
"is_uniform_pallet": "0",
"pallet_id": "",
"is_checked": "1",
"is_putaway": "1",
"location_id": "1757",
"last_location": "10139",
"split_key": "V09AFONYL7",
"created_by": "9",
"modified_by": "6",
"datetime_created": "2019-08-13 12:11:06",
"datetime_modified": "2019-08-14 16:58:52"
},
{
"id": "3",
"receipt_id": "2",
"parent_id": "0",
"has_children": "0",
"product_id": "3",
"product_code": "24900",
"product_name": "LIGHT SOLAR FAIRY WH LED PK100",
"quantity_expected": "1008",
"quantity_received": "1008",
"quantity_putaway": "1008",
"quantity_held": "0",
"reason_held": "",
"unit_quantity": "0",
"subinner_uom": "",
"subinner_quantity": "0",
"subinner_unit_quantity": "0",
"subinner_weight": "0.000",
"subinner_cubic": "0.000000",
"inner_uom": "",
"inner_quantity": "0",
"inner_unit_quantity": "0",
"inner_weight": "0.000",
"inner_cubic": "0.000000",
"outer_expected": "84",
"outer_uom": "CTN",
"outer_quantity": "84",
"outer_unit_quantity": "12",
"outer_weight": "0.000",
"outer_cubic": "0.000000",
"outer_length": "0.000",
"outer_width": "0.000",
"outer_height": "0.000",
"pallet_uom": "",
"pallet_quantity": "0",
"pallet_unit_quantity": "0",
"pallet_weight": "0.000",
"pallet_cubic": "0.000000",
"batch_number": " ",
"date_expiry": "0000-00-00",
"serial_number": "",
"length": "36.000",
"width": "23.000",
"height": "23.000",
"cubic": "0.019044",
"weight": "3.500",
"barcode": "",
"cost_current": "0.00",
"status": "0",
"is_uniform_pallet": "0",
"pallet_id": "",
"is_checked": "1",
"is_putaway": "1",
"location_id": "1512",
"last_location": "0",
"split_key": "",
"created_by": "9",
"modified_by": "6",
"datetime_created": "2019-08-13 12:11:06",
"datetime_modified": "2019-08-14 16:58:52"
},
获取JSON
private void fetchJSON(){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(ReceiptsLineInterface.JSONURL)
.addConverterFactory(ScalarsConverterFactory.create())
.build();
ReceiptsLineInterface api = retrofit.create(ReceiptsLineInterface.class);
Call<String> call = api.getString();
call.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
Log.i("Responsestring", response.body());
//Toast.makeText()
if (response.isSuccessful()) {
if (response.body() != null) {
Log.i("onSuccess", response.body());
setRecyclerItems(response.body());
} else {
//Log.i("onEmptyResponse", "Returned empty response");//Toast.makeText(getContext(),"Nothing returned",Toast.LENGTH_LONG).show();
}
}
}
@Override
public void onFailure(Call<String> call, Throwable t) {
}
});
}
有错误的代码块
/**
* Array list generator that should take JSON array from API query of Job
* or Item list
* @return items
*/
public void setRecyclerItems(String response){
try {
//getting the whole json object from the response
JSONObject obj = new JSONObject(response);
if(obj.optString("status").equals("true")){
ArrayList<Receipts> modelRecyclerArrayList = new ArrayList<>();
JSONArray dataArray = obj.getJSONArray("data");
for (int i = 0; i < dataArray.length(); i++) {
Receipts modelRecycler = new Receipts();
JSONObject dataobj = dataArray.getJSONObject(i);
modelRecycler.setName(dataobj.getString("product_name"));
modelRecycler.setProductCode(dataobj.getString("product_code"));
modelRecycler.setReceiptID(dataobj.getString("receipt_id"));
modelRecyclerArrayList.add(modelRecycler);
}
retrofitAdapter = new RetrofitAdapter(this,modelRecyclerArrayList);
recyclerView.setAdapter(retrofitAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false));
}else {
Toast.makeText(getApplicationContext(), obj.optString("message")+"", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
Logcat响应
2019-12-16 14:39:33.498 8562-8562/com.prostock.scanner I/onSuccess: array (
'status' => true,
'message' => 'Data fetched successfully',
'data' =>
array (
0 =>
array (
'id' => '1',
'receipt_id' => '1',
'parent_id' => '0',
'has_children' => '0',
'product_id' => '26',
'product_code' => '57280',
'product_name' => 'LIGHTS FAIRY CONNECTABLE 400',
'quantity_expected' => '400',
'quantity_received' => '400',
'quantity_putaway' => '400',
'quantity_held' => '0',
'reason_held' => '',
'unit_quantity' => '0',
'subinner_uom' => '',
'subinner_quantity' => '0',
'subinner_unit_quantity' => '0',
'subinner_weight' => '0.000',
'subinner_cubic' => '0.000000',
'inner_uom' => '',
'inner_quantity' => '0',
'inner_unit_quantity' => '0',
'inner_weight' => '0.000',
'inner_cubic' => '0.000000',
'outer_expected' => '50',
'outer_uom' => 'CTN',
'outer_quantity' => '50',
'outer_unit_quantity' => '8',
'outer_weight' => '0.000',
'outer_cubic' => '0.000000',
'outer_length' => '0.000',
'outer_width' => '0.000',
'outer_height' => '0.000',
'pallet_uom' => '',
'pallet_quantity' => '0',
'pallet_unit_quantity' => '0',
'pallet_weight' => '0.000',
'pallet_cubic' => '0.000000',
'batch_number' => ' ',
'date_expiry' => '0000-00-00',
'serial_number' => '',
'length' => '50.000',
'width' => '22.000',
'height' => '36.000',
'cubic' => '0.039600',
'weight' => '8.700',
'barcode' => '',
'cost_current' => '0.00',
'status' => '0',
'is_uniform_pallet' => '1',
'pallet_id' => '',
'is_checked' => '1',
'is_putaway' => '1',
'location_id' => '1641',
'last_location' => '1562',
'split_key' => '',
'created_by' => '9',
'modified_by' => '9',
'datetime_created' => '2019-08-06 12:18:23',
'datetime_modified' => '2019-08-06 16:30:34',
),
),
)
2019-12-16 14:39:33.499 8562-8562/com.prostock.scanner W/System.err: org.json.JSONException: Value array of type java.lang.String cannot be converted to JSONObject
2019-12-16 14:39:33.499 8562-8562/com.prostock.scanner W/System.err: at org.json.JSON.typeMismatch(JSON.java:112)
2019-12-16 14:39:33.499 8562-8562/com.prostock.scanner W/System.err: at org.json.JSONObject.<init>(JSONObject.java:163)
2019-12-16 14:39:33.499 8562-8562/com.prostock.scanner W/System.err: at org.json.JSONObject.<init>(JSONObject.java:176)
2019-12-16 14:39:33.499 8562-8562/com.prostock.scanner W/System.err: at com.prostock.scanner.Activities.ScanSessionActivity.setRecyclerItems(ScanSessionActivity.java:154)
2019-12-16 14:39:33.499 8562-8562/com.prostock.scanner W/System.err: at com.prostock.scanner.Activities.ScanSessionActivity$1.onResponse(ScanSessionActivity.java:130)
2019-12-16 14:39:33.499 8562-8562/com.prostock.scanner W/System.err: at retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall$1$1.run(DefaultCallAdapterFactory.java:83)
2019-12-16 14:39:33.500 8562-8562/com.prostock.scanner W/System.err: at android.os.Handler.handleCallback(Handler.java:873)
2019-12-16 14:39:33.500 8562-8562/com.prostock.scanner W/System.err: at android.os.Handler.dispatchMessage(Handler.java:99)
2019-12-16 14:39:33.500 8562-8562/com.prostock.scanner W/System.err: at android.os.Looper.loop(Looper.java:193)
2019-12-16 14:39:33.500 8562-8562/com.prostock.scanner W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6669)
2019-12-16 14:39:33.500 8562-8562/com.prostock.scanner W/System.err: at java.lang.reflect.Method.invoke(Native Method)
2019-12-16 14:39:33.500 8562-8562/com.prostock.scanner W/System.err: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
2019-12-16 14:39:33.500 8562-8562/com.prostock.scanner W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
答案 0 :(得分:0)
您的JSON字符串响应很可能格式错误/包含多余的字符,从而使其成为无效的JSON格式。您应再次检查是否从服务器发送正确的UTF编码。另外,您应该考虑在改造电话中使用GsonConverter
。它将把您的响应自动装箱到您的自定义Java对象中。因此,您的改造实例将被替换为:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(ReceiptsLineInterface.JSONURL)
.addConverterFactory(GsonConverterFactory.create()) // this line changed
.build();
您的改造呼叫的返回类型将变为:
Call<MyCustomObject>
有关更多详细信息,请参见本文:https://futurestud.io/tutorials/retrofit-2-adding-customizing-the-gson-converter
答案 1 :(得分:0)
感谢大家的帮助,我最终找到了问题。我的适配器指向一个额外的端点。现在一切正常!
import retrofit2.Call;
import retrofit2.http.GET;
public interface ReceiptsLineInterface {
String JSONURL = "http://[censored]/api/putaway/id/{receipts_line}/";
@GET("WRONG ENDPOINT")
Call<String> getString();
}