Ami*_*mir 3 post android json android-volley
我正在尝试使用Android Volley库发送JSON Post请求,但是我似乎没有正确获取JSON的主体,并且在Web服务器上得到了未定义的主体参数。我需要json的参数主体为单个对象“ name = someVal&comment = someOtherVal”。名称和注释是键,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>() {
@Override
public void onResponse(JSONObject response) {
Log.d("JSONPost", response.toString());
//pDialog.hide();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(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);
Run Code Online (Sandbox Code Playgroud)
}
我也在上面的代码中尝试过,但是没有运气:
params.put("comment", "someOtherVal");
params.put("name", "someVal");
Run Code Online (Sandbox Code Playgroud)
尝试把
Map<String, String> params = new HashMap<String, String>();
params.put("comment", "someOtherVal");
params.put("name", "someVal");
Run Code Online (Sandbox Code Playgroud)
在JsonObjectRequest jsonObjReq ...之前并通过以下方式更改空值
new JsonObject(params)
Run Code Online (Sandbox Code Playgroud)
所以你的代码将是
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>() {
@Override
public void onResponse(JSONObject response) {
Log.d("JSONPost", response.toString());
//pDialog.hide();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("JSONPost", "Error: " + error.getMessage());
//pDialog.hide();
}
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7511 次 |
| 最近记录: |