在POST请求上排队jsonobject到jsonarray错误

时间:2015-04-04 12:39:39

标签: android json android-volley

我正在使用volley库通过POST请求获取json数组。当我使用get方法时,我能够获取所需的值,但是当我使用POST方法时,我无法获取任何值,而是收到此错误

类型org.json.jsonobject的值无法转换为jsonarray

这是我的代码

public class fbuk extends Activity {

private static final String url = "http://example.com/q.php";
private ProgressDialog pDialog;
private List<Movie> movieList = new ArrayList<Movie>();
private ListView listView;
private CustomListAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listview_main);

    listView = (ListView) findViewById(R.id.listview);
    adapter = new CustomListAdapter(this, movieList);
    listView.setAdapter(adapter);


    pDialog = new ProgressDialog(this);
    // Showing progress dialog before making http request
    pDialog.setMessage("Loading...");
    pDialog.show();

    // changing action bar color
    Response.ErrorListener errorListener = new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError errorResponce)
        {

            VolleyLog.d( "Error: " + errorResponce.getMessage());
            hidePDialog();


        }
    };

    Response.Listener<JSONArray > jsonArrayListener = new Response.Listener<JSONArray>()
    {
        @Override
        public void onResponse(JSONArray response) {
            // TODO Auto-generated method stub

            hidePDialog();

            // Parsing json
            for (int i = 0; i < response.length(); i++) {
                try {

                    JSONObject obj = response.getJSONObject(i);
                    Movie movie = new Movie();
                    movie.setTitle(obj.getString("ttl"));

                    movieList.add(movie);

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            // notifying list adapter about data changes
            // so that it renders the list view with updated data
            adapter.notifyDataSetChanged();


        }
    };
    Map<String, String> mParam = new HashMap<String, String>();
    mParam.put("q", "ram");

  PostJsonArrayRequest req = new PostJsonArrayRequest(Request.Method.POST, url , null, jsonArrayListener, errorListener,mParam);

    // Creating volley request obj

    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(req);


    // Adding request to request queue

}

@Override
public void onDestroy() {
    super.onDestroy();
    hidePDialog();
}

private void hidePDialog() {
    if (pDialog != null) {
        pDialog.dismiss();
        pDialog = null;
    }
}


public class PostJsonArrayRequest extends JsonRequest<JSONArray>  {
    private Map<String,String> mParam;
    private Response.Listener<JSONArray> mListener;
    public PostJsonArrayRequest(int method, String url, String requestBody,
                                Response.Listener<JSONArray> listener, Response.ErrorListener errorListener,Map param) {
        super(method, url, requestBody, listener, errorListener);
        mListener=listener;
        mParam=param;
    }

    @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        return mParam;
    }
    @Override
    protected Response<JSONArray> parseNetworkResponse(NetworkResponse response) {
        try {
            String jsonString =
                    new String(response.data, HttpHeaderParser.parseCharset(response.headers));
            return Response.success(new JSONArray(jsonString),
                    HttpHeaderParser.parseCacheHeaders(response));
        } catch (UnsupportedEncodingException e) {
            return Response.error(new ParseError(e));
        } catch (JSONException je) {
            return Response.error(new ParseError(je));
        }
    }
    @Override
    protected void deliverResponse(JSONArray response) {
        mListener.onResponse(response);

    }

}

 }

请指导我哪里弄错了。我正确地通过了参数吗?

0 个答案:

没有答案