如何在android排球中发布数组参数?

时间:2017-10-04 04:55:55

标签: android api android-volley

对于Ex

 [{
            "id": 1,
            "name": "Christina Walker",
            "selectedGrade": 4
        }, {
            "id": 2,
            "name": "Bond Mccormick",
            "selectedGrade": 1
        }]
像这样? 谁能帮我 ?我被审了三天了?

1 个答案:

答案 0 :(得分:2)

请尝试以下代码。

   JSONObject objectJson1= new JSONObject();
   objectJson1.put("id","1");
   objectJson1.put("selectedGrade","8");
   objectJson1.put("selectedGrade","8");

//第二个数组

   JSONObject objectJson2= new JSONObject();
   objectJson2.put("id","2");
   objectJson2.put("selectedGrade","9");
   objectJson2.put("selectedGrade","9");
   JSONArray mainArray=new JSONArray();
   mainArray.put(0,objectJson1);
   mainArray.put(1,objectJson2);
   String data=mainArray.toString();

然后在凌空中传递这个

public void requestVolley(){
StringRequest stringRequest = null;
    final String requestBody = data;
    stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            //catch your resposne
        }
    }, new Response.ErrorListener() {

 @Override
        public void onErrorResponse(VolleyError error) {
            String json="";
            if (error.networkResponse != null) {
                // = String.valueOf(response.statusCode);
                try {
                    json = new String(
                            error.networkResponse.data, HttpHeaderParser.parseCharset(error.networkResponse.headers));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                Response<String> rr =Response.success(json, HttpHeaderParser.parseCacheHeaders(error.networkResponse));
                setCustomResponse(rr);
            }
        }
    }) {
@Override
        public String getBodyContentType() {
            return "application/json; charset=utf-8";
        }

        @Override
        public byte[] getBody() throws AuthFailureError {
            try {
                return requestBody == null ? null : requestBody.getBytes("utf-8");
            } catch (UnsupportedEncodingException uee) {
                VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", requestBody, "utf-8");
                return null;
            }
        }

        @Override
        protected Response<String> parseNetworkResponse(NetworkResponse response) {
            String responseString = "";
            String json="";
            if (response != null) {
                // = String.valueOf(response.statusCode);
                try {
                    json = new String(
                            response.data, HttpHeaderParser.parseCharset(response.headers));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                // can get more details such as response.headers
            }
            return Response.success(json, HttpHeaderParser.parseCacheHeaders(response));
        }
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String,String> params = new HashMap<String, String>();
            params.put("Authorization", "your key if any");
            return params;
        }
    };
    stringRequest.setRetryPolicy(new DefaultRetryPolicy(
            30000,
            0,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    stringRequest.setShouldCache(false);
    YourApplicationLevelClass.volleyQueueInstance.addToRequestQueue(stringRequest);
}

在应用级variable定义此public static VolleySingleton volleyQueueInstance;并在应用级别定义instantiateVolleyQueue

public void instantiateVolleyQueue() {
    volleyQueueInstance = VolleySingleton.getInstance(getApplicationContext());
}

创建排球单一类

  public class VolleySingleton 
    {
    private static VolleySingleton mInstance;
    private RequestQueue mRequestQueue;
    private ImageLoader mImageLoader;
    private static Context mCtx;
    private RequestQueue mRequestQueuePatch;

    private VolleySingleton(Context context) {

        final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
        final int cacheSize = maxMemory / 8;

        this.mCtx = context;
        mRequestQueue = getRequestQueue();
        mImageLoader = new ImageLoader(mRequestQueue,

                new ImageLoader.ImageCache() {
                    private final LruCache<String, Bitmap>
                            cache = new LruCache<String, Bitmap>(cacheSize);

                    @Override
                    public Bitmap getBitmap(String url) {
                        return cache.get(url);
                    }

                    @Override
                    public void putBitmap(String url, Bitmap bitmap) {
                        cache.put(url, bitmap);
                    }
                });
    }

    public static synchronized VolleySingleton getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new VolleySingleton(context);
        }
        return mInstance;
    }

    public RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            // getApplicationContext() is key, it keeps you from leaking the
            // Activity or BroadcastReceiver if someone passes one in.
            mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
        }
        return mRequestQueue;
    }
    public RequestQueue getRequestQueuePatch() {
        if (mRequestQueuePatch == null) {
            // getApplicationContext() is key, it keeps you from leaking the
            // Activity or BroadcastReceiver if someone passes one in.
            mRequestQueuePatch = Volley.newRequestQueue(mCtx.getApplicationContext());
        }
        return mRequestQueuePatch;
    }

    public <T> void addToRequestQueue(Request<T> req) {
        getRequestQueue().add(req);
    }

    public <T> void addToRequestQueuePatch(Request<T> req) {
        getRequestQueuePatch().add(req);
    }

    public <T> void cancelRequestInQueue(String tag) {
        getRequestQueue().cancelAll(tag);
    }

    public ImageLoader getImageLoader() {
        return mImageLoader;
    }
   }