Racheli Elkaslasi Racheli Elkaslasi - 6 months ago 30
Android Question

JsonObjectRequest Always returns on ErrorResponse

JsonObjectRequest Always returns onErrorResponse in android

public void clickFind(final View view) {
EditText find = (EditText) findViewById((R.id.editText));
// String
web = "http://api.tvmaze.com/search/shows?q=" + find.getText();
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, web, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("value");

lv = (ListView) findViewById(R.id.listView);
RowItem ri = null;
URL newurl = null;
for (int i = 0; i < jsonArray.length(); i++) {

try {
newurl = new URL(jsonArray.getJSONObject(i).getString("image"));
} catch (MalformedURLException e) {
e.printStackTrace();
}
ri = new RowItem(newurl,jsonArray.getJSONObject(i).getString("name")
,jsonArray.getJSONObject(i).getString("summary"));


}

CustomListViewAdapter adapter = new CustomListViewAdapter(view.getContext(),R.layout.cf, ri);
lv.setAdapter(adapter);

} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
Context context = view.getContext();
Toast.makeText(context, "error to upload "+web, Toast.LENGTH_SHORT).show();
}
});
queue.add(request);

} }


For example:
http://api.tvmaze.com/search/shows?q=bob

Which brings reply but run of6 android phone is always falling on error
Does anyone have an idea what is wrong with the code?

Answer
 JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, web, null, new Response.Listener<JSONArray>() {

        public void onResponse(JSONArray response) {

            try {
                whichList++;
                lv = (ListView) findViewById(R.id.listView);
                RowItem ri[] = new RowItem[response.length()];


                for (int i = 0; i < response.length(); i++) {

                    JSONObject jsonArray = response.getJSONObject(i).getJSONObject("show");
                    ri[i] = new RowItem(jsonArray.getString("id"), "", "", jsonArray.getJSONObject("image").getString("medium"), jsonArray.getString("name")
                            , jsonArray.getString("summary"));


                }

                ArrayAdapterItem adapter = new ArrayAdapterItem(view.getContext(), R.layout.cf, ri);

                lv.setAdapter(adapter);

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Context context = view.getContext();
            Toast.makeText(context, "error to upload " + web, Toast.LENGTH_SHORT).show();
        }
    });
    queue.add(request);

}
Comments