使用Android Volley发送带有Body的JSON帖子

使用Android Volley发送带有Body的JSON帖子,第1张

概述我试图使用AndroidVolley库发送JSONPost请求,但我似乎没有正确的json的身体,我在我的Web服务器上得到未定义的身体参数.我需要json的参数body作为单个对象“name=someVal&comment=someOtherVal”.name和comment是键,someVal和someOtherVal是值.StringspreadsheetID="1

我试图使用Android Volley库发送JsON Post请求,但我似乎没有正确的Json的身体,我在我的Web服务器上得到未定义的身体参数.我需要Json的参数body作为单个对象“name = someVal& comment = someOtherVal”. name和comment是键,someVal和someOtherVal是值.

String spreadsheetID = "1111111-11111N92RT9h-11111111111111111111111111111";String url = "https://script.Google.com/macros/s/" + spreadsheetID + "/exec";// Instantiate the RequestQueue.RequestQueue queue = Volley.newRequestQueue(this);// Request a string response from the provIDed URL.JsonObjectRequest JsonObjReq = new JsonObjectRequest(Request.Method.POST,        url, null,        new Response.Listener<JsONObject>() {@OverrIDepublic voID onResponse(JsONObject response) {    Log.d("JsONPost", response.toString());    //pDialog.hIDe();}        }, new Response.ErrorListener() {    @OverrIDe    public voID one rrorResponse(VolleyError error) {        VolleyLog.d("JsONPost", "Error: " + error.getMessage());        //pDialog.hIDe();    }}) {    @OverrIDe    protected Map<String, String> getParams() {        Map<String, String> params = new HashMap<String, String>();        params.put("name=someVal&comment=someOtherVal");        //params.put("comment", "someOtherVal");        return params;    }};// Add the request to the RequestQueue.queue.add(JsonObjReq);

}

我也在上面的代码中试过这个,但没有运气:

params.put("comment", "someOtherVal");params.put("name", "someVal");

解决方法:

试着把

Map<String, String> params = new HashMap<String, String>();params.put("comment", "someOtherVal");params.put("name", "someVal");

在JsonObjectRequest之前JsonObjReq …并更改null值

new JsonObject(params)

所以你的代码将是

Map<String, String> params = new HashMap<String, String>();    params.put("comment", "someOtherVal");    params.put("name", "someVal");JsonObjectRequest JsonObjReq = new JsonObjectRequest(Request.Method.POST,        url, new JsonObject(params),        new Response.Listener<JsONObject>() {@OverrIDepublic voID onResponse(JsONObject response) {    Log.d("JsONPost", response.toString());    //pDialog.hIDe();}        }, new Response.ErrorListener() {    @OverrIDe    public voID one rrorResponse(VolleyError error) {        VolleyLog.d("JsONPost", "Error: " + error.getMessage());        //pDialog.hIDe();    }})
总结

以上是内存溢出为你收集整理的使用Android Volley发送带有Body的JSON帖子全部内容,希望文章能够帮你解决使用Android Volley发送带有Body的JSON帖子所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1111051.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存