【问题标题】:How to post JSON in volley using android?如何使用 android 在 volley 中发布 JSON?
【发布时间】:2016-08-08 06:14:36
【问题描述】:

我正在研究 Volley,但我对这个框架很陌生,我不知道如何使用 volley 发布 JSON 数据,我有一个示例 JSON,请告诉我如何发布这个 JSON 数据。

JSONObject jsonObject = new JSONObject();
jsonObject.put("email", "abcd@g.com");
jsonObject.put("password", "abcd123");
jsonObject.put("device", "jdghfdhgdhi");
jsonObject.put("latitude", 1.2456);
jsonObject.put("longitude", 1.3466);

请仔细阅读我的 JSON,让我知道如何使用 volley 发布此 JSON。

【问题讨论】:

标签: android android-volley


【解决方案1】:

试试这个:

    StringRequest stringRequest = new StringRequest(Request.Method.POST, your url,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            try {

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Toast.makeText(NoolReaderDashboard.this, error.toString(), Toast.LENGTH_LONG).show();
                        }
                    }) {
                @Override
                protected Map<String, String> getParams() {
                    Map<String, String> params = new HashMap<String, String>();
           params .put("email", "abcd@g.com");
         params .put("password", "abcd123");
        params .put("device", "jdghfdhgdhi");
        params .put("latitude", 1.2456);
         params .put("longitude", 1.3466);
                    return params;
                }
            };
            RequestQueue requestQueue = Volley.newRequestQueue(this);
            requestQueue.add(stringRequest);

【讨论】:

  • 嘿,谢谢,但在参数中 .put("latitude", 1.2456);参数 .put("经度", 1.3466);它显示错误“错误的参数找到双倍”
  • 参数 .put("纬度",String.valueOf(1.2456));和参数 .put("longitude", String.valueOf(1.3466));
  • 转换为字符串并像(1.2456+"" 或 String.valueOf(1.2456));)@AnshumanPattnaik 一样传递它
  • 不按照后端API设计,它想要双格式的纬度和经度而不是字符串
  • 他们会转换成双格式问你的后端开发者@AnshumanPattnaik
【解决方案2】:

private void Postdata() {

    String url = "http://www.example.com";                              //url where you want to post data
    StringRequest postRequest = new StringRequest(Request.Method.POST, url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        Log.i("response is","==>"+response);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    Log.d("Response-------", "------>" + response);
                }
            },
            new Response.ErrorListener() {
                public void onErrorResponse(VolleyError error) {
                    Log.e("Responce error==","===>"+error);
                    error.printStackTrace();
                }
            }
    ) {

        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<>();
            // the POST parameters:
            params.put("email", "abcd@g.com");
            params.put("password", "abcd123");
            params.put("latitude", 1.2456);
            params.put("longitude", 1.3466);
            Log.d("Value is ----------", ">" + params);
            return params;
        }
    };
    MyApplication .getInstance().addToRequestQueue(postRequest);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-26
    • 2017-10-27
    相关资源
    最近更新 更多