我遇到一个问题,但是我从服务器检索JSON数据,然后将其显示在Android的recyclerview中。数据可用,就像我记录它一样,确实显示如下:
D/------>: onSuccess: {"{\"vtmtransaction\":\"f53fa6ba-ccbf-4aa0-b948-b4305f279cfd\",\"vtmowner\":\"a8dd0bd7-1d6f-4262-ab66-14492ce183ac\",\"vtmoriginname\":\"Dar es Salaam\",\"vtmdestinationname\":\"Arusha\",\"vtmpickupdate\":\"2019-05-26T22:00:00.000Z\",\"vtmpickuptime\":\"08:30:00\",\"vtmvehicletype\":1,\"vtmmodel\":\"Land cruisera\",\"vtmfueltype\":1,\"vtmenginenumber\":\"sdasda\",\"vtmchassisnumber\":\"sdasd\",\"vtmenginecapacity\":2000,\"vtminsurance\":false,\"vtminsurancetype\":null,\"vtmclearingandforwarding\":false,\"vtmvehicleservice\":false,\"vtmvehicleservicetype\":null,\"vtmvehiclemake\":1,\"vtmorigincountry\":\"Tanzania\",\"vtmdestinationcountry\":\"Tanzania\",\"vtmbookedwhen\":\"2019-05-26T22:00:00.000Z\",\"vtmorigincoords\":\"0101000020E6100000FC694881AA9A43404C6BD3D85E2B1BC0\",\"vtmdestinationcoords\":\"0101000020E610000010F4064E6C5742400AEF17586C180BC0\"}":{"vtmtransaction":"f53fa6ba-ccbf-4aa0-b948-b4305f279cfd","vtmowner":"a8dd0bd7-1d6f-4262-ab66-14492ce183ac","vtmoriginname":"Dar es Salaam","vtmdestinationname":"Arusha","vtmpickupdate":"2019-05-26T22:00:00.000Z","vtmpickuptime":"08:30:00","vtmvehicletype":1,"vtmmodel":"Land cruisera","vtmfueltype":1,"vtmenginenumber":"sdasda","vtmchassisnumber":"sdasd","vtmenginecapacity":2000,"vtminsurance":false,"vtminsurancetype":null,"vtmclearingandforwarding":false,"vtmvehicleservice":false,"vtmvehicleservicetype":null,"vtmvehiclemake":1,"vtmorigincountry":"Tanzania","vtmdestinationcountry":"Tanzania","vtmbookedwhen":"2019-05-26T22:00:00.000Z","vtmorigincoords":"0101000020E6100000FC694881AA9A43404C6BD3D85E2B1BC0","vtmdestinationcoords":"0101000020E610000010F4064E6C5742400AEF17586C180BC0"}}
一旦收到JSONArray,我便将其转换为JSONObject,因为我不想使用索引,但直接从数据中使用列名,但总是会遇到以下错误:
W/System.err: org.json.JSONException: No value for vtmoriginname
这是片段中的相关部分,该片段使用AsyncHttpRequest检索数据并在请求完成时触发过程。数据来自节点。
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable final Bundle savedInstanceState) {
View mybookings = inflater.inflate(R.layout.fragment_mybookings, container, false);
preferences = Objects.requireNonNull(getActivity()).getSharedPreferences("mykago-cars", Context.MODE_PRIVATE);
t = new Tools(getActivity().getApplication(), getContext(), getActivity());
prgBookings = mybookings.findViewById(R.id.prgBookings);
final RecyclerView recyclerViewMyBookings = mybookings.findViewById(R.id.recyclerViewMyBookings);
AsyncHttpRequest.client.setMaxRetriesAndTimeout(2, 5000);
RequestParams params = new RequestParams();
params.put("uuid", preferences.getString("myUUID", ""));
AsyncHttpRequest.get("my_bookings", params, new JsonHttpResponseHandler() {
@Override
public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
super.onFailure(statusCode, headers, throwable, errorResponse);
t.myDialog(R.string.dataError, R.string.databaseDataNotAvailable, true, false);
}
@Override
public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
prgBookings.setVisibility(View.INVISIBLE);
// Covert the JSONArray to a JSONObject so we can reference the columns by name and not index. Makes things easier and avoids mistakes..
try {
JSONObject jsonObject = response.toJSONObject(response);
Log.d(TAG, "onSuccess: " + jsonObject);
// Loop through response and add all items to the mybooking list
for (int i = 0; i < jsonObject.length(); i++) {
MyBookings_Data myBookings_data = new MyBookings_Data();
try {
myBookings_data.setMbdorigin(jsonObject.getString("vtmoriginname"));
myBookings_data.setMbddestination(jsonObject.getString("vtmdestinationname"));
myBookings_data.setMbdpickupdate(jsonObject.getString("vtmpickupdate"));
myBookings_data.setMbdpickuptime(jsonObject.getString("vtmpickuptime"));
myBookings_data.setMdbdate(jsonObject.getString("vtmbookedwhen"));
myBookings_data_list.add(myBookings_data);
} catch (JSONException e) {
e.printStackTrace();
}
}
MyBookingsRecyclerViewAdapter myBookingsRecyclerViewAdapter = new MyBookingsRecyclerViewAdapter(getContext(), myBookings_data_list);
recyclerViewMyBookings.setAdapter(myBookingsRecyclerViewAdapter);
recyclerViewMyBookings.setLayoutManager(new LinearLayoutManager(getContext()));
} catch (JSONException e) {
e.printStackTrace();
}
}
});
return mybookings;
}
答案 0 :(得分:0)
请删除for loop
,然后重试,因为在您的json
回复JsonArray
中不可用。
因此,无需使用for loop