Volley-Json阵列发布请求

时间:2015-04-04 06:16:38

标签: android json android-volley

我试图通过POST方法获取json数组。我无法发送参数。

这是我的班级

 public class fbuk extends Activity {
// Log tag
private static final String TAG = MainActivity.class.getSimpleName();

// Movies json url
private static final String url = "http://example.com/s.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(TAG, "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"));
                    movie.setThumbnailUrl(obj.getString("img"));
                    movie.setRating(obj.getString("stts"));
                    movie.setYear(obj.getString("rel"));

                    // Genre is json array


                    // adding movie to movies array
                    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();


        }
    };


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

    AppController.getInstance().addToRequestQueue(req);
}

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

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


public class PostJsonArrayRequest extends JsonArrayRequest {

    public PostJsonArrayRequest(int method, String url, String requestBody,
                                Response.Listener<JSONArray> listener, Response.ErrorListener errorListener) {
        super(method, url, requestBody, listener, errorListener);
    }

    @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("q", "ram");
        return params;
    }   
    @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));
        }
    }

}

}

请以正确的方式帮助我发送params。经过几次goole搜索和stackoverflow回答这种方式发送POST请求对我有用,但无法发送参数。请帮我介绍如何发送参数

我的logcat响应

 04-04 12:36:35.248  24381-24494/com.xyz.xyz E/Error﹕ Index 1 out of range [0..1)
   04-04 12:36:35.248  24381-24494/com.xyz.xyz W/System.err﹕ org.json.JSONException:      Index 1 out of range [0..1)
  04-04 12:36:35.248  24381-24494/com.xyz.xyz W/System.err﹕ at  org.json.JSONArray.get(JSONArray.java:282)
 04-04 12:36:35.248  24381-24494/com.xyz.xyz W/System.err﹕ at org.json.JSONArray.getJSONObject(JSONArray.java:510)
 04-04 12:36:35.248  24381-24494/com.xyz.xyz W/System.err﹕ at com.xyz.xyz.News$DownloadJSON.doInBackground(News.java:187)
 04-04 12:36:35.248  24381-24494/com.xyz.xyz W/System.err﹕ at com.xyz.xyz.News$DownloadJSON.doInBackground(News.java:134)
  04-04 12:36:35.248  24381-24494/com..xyz.xyz W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:288)
  04-04 12:36:35.248  24381-24494/com..xyz.xyz W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
  04-04 12:36:35.248  24381-24494/com..xyz.xyz W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
 04-04 12:36:35.248  24381-24494/com..xyz.xyz W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
 04-04 12:36:35.248  24381-24494/com..xyz.xyz W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
  04-04 12:36:35.248  24381-24494/com..xyz.xyz W/System.err﹕ at   java.lang.Thread.run(Thread.java:841)
04-04 12:36:35.268  
 04-04 12:36:35.478  24381-24535/com..xyz.xyz I/MemoryCache﹕ cache size=203312 length=1
 04-04 12:36:35.528  24381-24536/com..xyz.xyz D/dalvikvm﹕ GC_FOR_ALLOC freed 5897K, 13% free 40087K/46020K, paused 19ms, total 19ms
 04-04 12:36:35.568  24381-24532/com..xyz.xyz I/MemoryCache﹕ cache size=203312 length=1
 04-04 12:36:35.568  24381-24536/com..xyz.xyz I/MemoryCache﹕ cache size=203312 length=1
 04-04 12:36:35.578  24381-24534/com..xyz.xyz I/MemoryCache﹕ cache size=203312 length=1
 04-04 12:36:35.638  24381-24533/com..xyz.xyz I/MemoryCache﹕ cache size=203312 length=1
  04-04 12:36:35.638  24381-24535/com..xyz.xyz I/MemoryCache﹕ cache size=203312 length=1
  04-04 12:36:35.658  24381-24534/com..xyz.xyz I/MemoryCache﹕ cache size=203312 length=1
  04-04 12:36:35.658  24381-24532/com..xyz.xyz I/MemoryCache﹕ cache size=203312 length=1
  04-04 12:36:35.658  24381-24536/com..xyz.xyz I/MemoryCache﹕ cache size=203312 length=1
    04-04 12:36:40.268  24381-24381/com..xyz.xyz I/Timeline﹕ Timeline: Activity_launch_request id:com..xyz.xyz time:24184635
    04-04 12:36:40.638  24381-24381/com..xyz.xyz I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@42b257d8 time:24185013
  04-04 12:36:42.098  24381-24381/com.xyz.xyz D/Volley﹕ [1] 2.onErrorResponse:

0 个答案:

没有答案