android - 使用 volley 以 json 格式将数据发布到服务器

标签 android http-post android-volley json

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

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

最佳答案

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

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

}

然后使用下面的方法你可以发送请求。

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

    }

这对我来说很好。

关于android - 使用 volley 以 json 格式将数据发布到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36905705/

相关文章:

android: layout: 如何让我的对话框用 XML 填满屏幕

java - 在 Android 中使用带有 post 参数的 HttpClient 和 HttpPost

用于发表评论的 Ruby 脚本

android - 如何从扩展 RecyclerView 适配器的类开始启动 DialogFragment

android - 为什么 Volley 调用 https ://safebrowsing. google.com?

android - 使用 Android MediaPlayer 播放声音时出现问题

android - 将播放服务更新到 6.5.87 后找不到 com.google.android.gms 类

javascript - 在键盘感知 ScrollView 中输入文本后按下 React-Native 按钮

python - flask : Ignore if one file is not uploaded in form (containing multiple file upload options)

java - 为什么 VOLLEY 使用不同的参数检索相同的数据?