我试图使用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帖子所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)