I am trying to work on volley with gridview. JsonObjectRequest does not fetch any response. I looked up for links on stackoverflow abt JsonObjectRequest passing null as parameter and changing volley version but nothing works as the error was Volley - Cannot Resolve Constructor "JSONObjectRequest. Now no error but no response also. Any help appreciated! Below example link : http://hemanthsomaraju.blogspot.com/2019/03/android-grid-view-using-volley-api.html and volley version: implementation 'com.mcxiaoke.volley:library:1.0.19'. Links referred: Volley - Cannot Resolve Constructor "JSONObjectRequest
public class OutputActivity extends AppCompatActivity {
GridView ListView;
VolleyListViewAdapter adapter;
private List<CommonBean> VolleyList = new ArrayList<CommonBean>();
private ProgressDialog mprocessingdialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_output);
ListView = (GridView) findViewById(R.id.ListView);
adapter = new VolleyListViewAdapter(OutputActivity.this, VolleyList);
ListView.setAdapter(adapter);
new OutputActivity.GetListAsync().execute();
}
private class GetListAsync extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
mprocessingdialog = new ProgressDialog(OutputActivity.this);
mprocessingdialog.setTitle("Please Wait..");
mprocessingdialog.setMessage("Loading");
mprocessingdialog.setIndeterminate(false);
mprocessingdialog.setCancelable(false);
mprocessingdialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
JSONObject jsonObject = null;
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
"https://api.fnpplus.com/productapi/api/rest/v1.2/productList?catalogId=india",
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("rlog", response.toString());
try {
JSONObject jsonObj = new JSONObject(response.toString());
JSONObject jsonObject = jsonObj.getJSONObject("data");
JSONArray jsonArray = jsonObject.getJSONArray("productResults");
Log.d("rlog", jsonArray.toString());
for (int i = 0; i < jsonArray.length(); i ) {
JSONObject obj = jsonArray.getJSONObject(i);
CommonBean commonBean = new CommonBean();
commonBean.setTextView1(obj.optString("productName"));
JSONObject jsonObject2 = obj.getJSONObject("price");
Log.d("rlog1", jsonObject2.toString());
commonBean.setTextView2(jsonObject2.optString("price"));
Log.d("rlog12", jsonObject2.optString("price"));
JSONObject jsonObject1 = obj.getJSONObject("productImage");
Log.d("rlog1", jsonObject1.toString());
commonBean.setImageView(jsonObject1.optString("path"));
Log.d("rlog12", jsonObject1.optString("path"));
VolleyList.add(commonBean);
}
mprocessingdialog.dismiss();
} catch (Exception e) {
e.printStackTrace();
}
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void one rrorResponse(VolleyError error) {
VolleyLog.d("rlog", "Error: " error.getMessage());
}
});
jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(0, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
RequestQueue requestQueue = Volley.newRequestQueue(OutputActivity.this);
requestQueue.add(jsonObjReq);
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
new Timer().schedule(new TimerTask() {
@Override
public void run() {
mprocessingdialog.dismiss();
}
}, 30000);
}
}
}
CodePudding user response:
Go with the StringRequest and add your data as per your expected conditions
No Need to use AsyncTask and you can add loader in code also.
RequestQueue queue;
String URL = "https://api.fnpplus.com/productapi/api/rest/v1.2/productList?catalogId=india";
queue = Volley.newRequestQueue(this);
StringRequest request = new StringRequest(Request.Method.GET, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
JSONObject dataJsonObject = jsonObject.getJSONObject("data");
JSONArray jsonArray = dataJsonObject.getJSONArray("productResults");
Log.d("rlog", jsonArray.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void one rrorResponse(VolleyError error) {
Log.d("error", error.toString());
}
});
request.setRetryPolicy(new DefaultRetryPolicy(5000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
queue.add(request);
}