[英]W/System.err: org.json.JSONException: Value array of type java.lang.String cannot be converted to JSONObject
I'm getting floored by this error.我被这个错误弄糊涂了。 I'm retrieving a JSON Object to display in a recyclerview in Android Studio.我正在检索一个 JSON 对象以显示在 Android Studio 的回收视图中。 I've made some changes based on previous answers here, but it seems not to make a difference.我根据以前的答案在这里做了一些更改,但似乎没有什么不同。
The code receives a JSON Object with a nested JSON array ("data"), containing a list of receipts.该代码接收一个带有嵌套 JSON 数组(“数据”)的 JSON 对象,其中包含一个收据列表。
Example 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"
},
Fetch JSON获取 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) {
}
});
}
Code block with the errors有错误的代码块
/**
* 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 Response 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)
It is most likely that your JSON string response is malformed/contains extra characters that makes it an invalid JSON format.很可能您的 JSON 字符串响应格式错误/包含额外的字符,使其成为无效的 JSON 格式。 You should double check that you're sending the right UTF encoding from your server.您应该仔细检查您是否从服务器发送了正确的 UTF 编码。 Also, you should consider using a GsonConverter
in your retrofit call.此外,您应该考虑在改造调用中使用GsonConverter
。 It will automatically box your response into your custom Java Object.它会自动将您的响应装箱到您的自定义 Java 对象中。 So your retrofit instance will be replaced with:因此,您的改造实例将被替换为:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(ReceiptsLineInterface.JSONURL)
.addConverterFactory(GsonConverterFactory.create()) // this line changed
.build();
And the return type of your retrofit call would become: Call<MyCustomObject>
您的改造调用的返回类型将变为: Call<MyCustomObject>
See this article for more details: https://futurestud.io/tutorials/retrofit-2-adding-customizing-the-gson-converter有关更多详细信息,请参阅本文: https : //futurestud.io/tutorials/retrofit-2-adding-customizing-the-gson-converter
Thanks for the help everyone, I ended up finding the problem.感谢大家的帮助,我终于找到了问题所在。 My adapter was pointing to an extra endpoint.我的适配器指向一个额外的端点。 Works all good now!现在一切正常!
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.