-1

JsonObjectRequest 在 android 中总是返回 onErrorResponse

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);

} }

例如: http ://api.tvmaze.com/search/shows?q=bob

这带来了回复,但运行 of6 android 手机总是出错 有人知道代码有什么问题吗?

4

3 回答 3

0
 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);

}
于 2016-05-22T19:19:23.303 回答
0

引用文档,getJSONArray

如果存在并且是 JSONArray,则返回按名称映射的值,否则抛出。

并检查来自该 api 端点“值”的响应似乎不存在。我希望我没有错,但我找不到任何名为“值”的属性。

于 2016-05-17T20:53:15.740 回答
0

The response from http://api.tvmaze.com/search/shows?q=bob is a JSON Array and not JSONObject. So you need to use JsonArrayRequest instead of JsonObjectRequest like this -

JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, web,new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                try {

                    for(int i = 0; i < response.length(); i++){
                        JSONObject obj = response.getJSONObject(i);//This is your Json object which you can parse now.
                    }

                } 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();
            }
        });
于 2016-05-17T21:17:54.617 回答