使用volley以json格式将数据发布到服务器 [英] posting data to server in json format using volley

查看:109
本文介绍了使用volley以json格式将数据发布到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我以json格式向服务器发布数据,
但它在错误响应中返回排球服务器错误。

Hello i am posting data to server in json format, but it returns volley server error in error response`

RequestQueue queue = Volley.newRequestQueue(this);
        JSONObject jobj=new JSONObject();
        try {
            jobj.put("id","123");
            jobj.put("session","new");
            JSONArray array = null;
            array = new JSONArray();
            for (int i = 0;i<3;i++){
               JSONObject jsonObject = new JSONObject();
                jsonObject.put("name","test");
                jsonObject.put("content","test");
                jsonObject.put("time"," 2016-04-07T11:44:22.407Z ");
                array.put(jsonObject);
            }
            Log.e(" array "," arrr"+array.toString());
            jobj.put("data",array);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Log.e("jsondata",jobj.toString());
JsonObjectRequest sr=  new JsonObjectRequest(post_url, jobj, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                            }

        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }){
            @OveRequestQueue queue = Volley.newRequestQueue(this);
        JSONObject jobj=new JSONObject();
        try {
            jobj.put("id","123");
            jobj.put("session","new");
            JSONArray array = null;
            array = new JSONArray();
            for (int i = 0;i<3;i++){
               JSONObject jsonObject = new JSONObject();
                jsonObject.put("name","test");
                jsonObject.put("content","test");
                jsonObject.put("time"," 2016-04-07T11:44:22.407Z ");
                array.put(jsonObject);
            }
            Log.e(" array "," arrr"+array.toString());
            jobj.put("data",array);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Log.e("jsondata",jobj.toString());
JsonObjectRequest sr=  new JsonObjectRequest(post_url, jobj, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                            }

        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {

            }rride
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String,String> params = new HashMap<String, String>();
                params.put("Content-Type","application/json");
                return params;
            }
        };
        queue.add(sr);`


推荐答案

有一个处理请求队列的单例。

Have a singleton which handles the request queue.

public class VolleySingleton {

    // Singleton object...
    private static VolleySingleton instance;

    final private RequestQueue requestQueue;
    private static ImageLoader imageLoader;

    //Constructor...
    private VolleySingleton(Context context) {

        requestQueue = Volley.newRequestQueue(context);

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


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

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

    // Singleton method...
    public static VolleySingleton getInstance(Context context) {
        if (instance == null) {
            instance = new VolleySingleton(context);
        }
        return instance;
    }

    public RequestQueue getRequestQueue(Context context) {
        if (requestQueue != null) {
        return requestQueue;
        } else {
            getInstance(context);
            return requestQueue;
        }
    }

    private  RequestQueue getRequestQueue() {
        return requestQueue;
    }

    public static ImageLoader getImageLoader(Context context) {
        if (imageLoader != null) {
            return imageLoader;
        } else {
            getInstance(context);
            return imageLoader;
        }
    }

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

}

然后使用以下方法即可发送请求。

Then using the below method you can send the request.

public void postVolley(final Context context, String url, JSONObject jsonObject) {

        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject responseJson) {
                Log.e("success", "" + responseJson.toString());
            }


        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("error", "" + error);

            }
        }) {

            /**
             * Setting the content type
             */

            @Override
            public String getBodyContentType() {
                return "application/json;charset=UTF-8";
            }
        };

        VolleySingleton.getInstance(context).addToRequestQueue(jsonObjReq);

    }

这对我来说很好。

这篇关于使用volley以json格式将数据发布到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆