我的主要活动是
package com.blogspot.ajjavaprograms.i_bid;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class Painting extends AppCompatActivity {
private RecyclerView mList;
private LinearLayoutManager linearLayoutManager;
private List<Data> data;
private DividerItemDecoration dividerItemDecoration;
private RecyclerView.Adapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_painting);
mList=findViewById(R.id.recyclerview1);
data=new ArrayList<>();
adapter=new Data_Adapter(getApplicationContext(),data);
linearLayoutManager=new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
dividerItemDecoration= new DividerItemDecoration(mList.getContext(),linearLayoutManager.getOrientation());
mList.setAdapter(adapter);
mList.setLayoutManager(linearLayoutManager);
mList.addItemDecoration(dividerItemDecoration);
mList.setHasFixedSize(true);
getData();
}
private void getData()
{
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading...");
progressDialog.show();
final JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Constants.paint, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
for (int i = 0; i < response.length(); i++) {
try {
JSONObject jsonObject = response.getJSONObject(i);
Data data1 = new Data();
if(jsonObject.has("itemname"))
data1.setItemname(jsonObject.getString("itemname"));
data1.setDescription(jsonObject.getString("description"));
data1.setPrice(jsonObject.getString("price"));
data.add(data1);
} catch (JSONException e) {
e.printStackTrace();
progressDialog.dismiss();
}
}
adapter.notifyDataSetChanged();
progressDialog.dismiss();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley", error.toString());
progressDialog.dismiss();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsonArrayRequest);
}
}
运行时的错误是
W / System.err:org.json.JSONException:无描述值 在org.json.JSONObject.get(JSONObject.java:392)W / System.err:在org.json.JSONObject.getStri
ng(JSONObject.java:553)
at com.blogspot.ajjavaprograms.i_bid.Painting$1.onResponse(Painting.java:67) at com.blogspot.ajjavaprograms.i_bid.Painting$1.onResponse(Painting.java:57) at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:90) at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:102) at android.os.Handler.handleCallback(Handler.java:873) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:193) W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6762) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
W / System.err:org.json.JSONException:无描述值 在org.json.JSONObject.get(JSONObject.java:392) 在org.json.JSONObject.getString(JSONObject.java:553)W / System.err:at com.blogspot.ajjavaprograms.i_bid.Painting $ 1.onResponse(Painting.java:67) 在com.blogspot.ajjavaprograms.i_bid.Painting $ 1.onResponse(Painting.java:57) 在com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:90) 在com.android.volley.ExecutorDelivery $ ResponseDeliveryRunnable.run(ExecutorDelivery.java:102) 在android.os.Handler.handleCallback(Handler.java:873) 在android.os.Handler.dispatchMessage(Handler.java:99) 在android.os.Looper.loop(Looper.java:193) 在android.app.ActivityThread.main(ActivityThread.java:6762) 在java.lang.reflect.Method.invoke(本机方法)W / System.err:在 com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:493) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) org.json.JSONException:价格没有价值 在org.json.JSONObject.get(JSONObject.java:392)W / System.err:在org.json.JSONObject.getString(JSONObject.java:553) 在com.blogspot.ajjavaprograms.i_bid.Painting $ 1.onResponse(Painting.java:68) 在com.blogspot.ajjavaprograms.i_bid.Painting $ 1.onResponse(Painting.java:57) 在com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:90) 在com.android.volley.ExecutorDelivery $ ResponseDeliveryRunnable.run(ExecutorDelivery.java:102) 在android.os.Handler.handleCallback(Handler.java:873) 在android.os.Handler.dispatchMessage(Handler.java:99) 在android.os.Looper.loop(Looper.java:193) 在android.app.ActivityThread.main(ActivityThread.java:6762) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:493) W / System.err:位于 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) org.json.JSONException:无描述值 在org.json.JSONObject.get(JSONObject.java:392) 在org.json.JSONObject.getString(JSONObject.java:553) 在com.blogspot.ajjavaprograms.i_bid.Painting $ 1.onResponse(Painting.java:67) 在com.blogspot.ajjavaprograms.i_bid.Painting $ 1.onResponse(Painting.java:57) 在com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:90) 在com.android.volley.ExecutorDelivery $ ResponseDeliveryRunnable.run(ExecutorDelivery.java:102) W / System.err:位于 android.os.Handler.handleCallback(Handler.java:873) 在android.os.Handler.dispatchMessage(Handler.java:99) 在android.os.Looper.loop(Looper.java:193) 在android.app.ActivityThread.main(ActivityThread.java:6762) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:493) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) org.json.JSONException:无描述值W / System.err:位于org.json.JSONObject.get(JSONObject.java:392) 在org.json.JSONObject.getString(JSONObject.java:553) 在com.blogspot.ajjavaprograms.i_bid.Painting $ 1.onResponse(Painting.java:67) 在com.blogspot.ajjavaprograms.i_bid.Painting $ 1.onResponse(Painting.java:57) 在com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:90) 在com.android.volley.ExecutorDelivery $ ResponseDeliveryRunnable.run(ExecutorDelivery.java:102) 在android.os.Handler.handleCallback(Handler.java:873) 在android.os.Handler.dispatchMessage(Handler.java:99) 在android.os.Looper.loop(Looper.java:193) 在android.app.ActivityThread.main(ActivityThread.java:6762)W / System.err:在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:493) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 应用已终止。
请帮助!
答案 0 :(得分:1)
没有用于描述的值意味着当前json对象中没有这样的密钥。请尝试关注,
if(jsonObject.has("itemname")){
data1.setItemname(jsonObject.getString("itemname"));
} else if(jsonObject.has("descript")){
data1.setDescription(jsonObject.getString("descript"));
}else if(jsonObject.has("price")){
data1.setPrice(jsonObject.getString("price"));
}
data.add(data1);