我希望使用volly library
将数据发送到服务器当我使用volley library
向服务器发送数据时出现以下错误:
org.json.JSONException: Value failure_post of type
java.lang.String cannot be converted to JSONObject
这是我的java代码:
public void insertClick(View view)
{
RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
HashMap<String, JSONObject> jsonParams = new HashMap<String, JSONObject>();
JSONObject params = new JSONObject();
try
{
params.put("title","BOOK");
params.put("intro","hellothere");
}
catch (JSONException e)
{
Log.i(LABLE, "erorr is : "+ e.getMessage().toString());
}
try {
JsonObjectRequest postRequest = new JsonObjectRequest(Request.Method.POST, url,
new JSONObject(jsonParams),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Toast.makeText(getApplicationContext() , " insert ok ",Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
// Handle Error
Log.i(LABLE, "erorr is : "+ error.getMessage().toString());
Toast.makeText(getApplicationContext() , " insert failed ",Toast.LENGTH_LONG).show();
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json");
return headers;
}
};
queue.add(postRequest);
}
catch (Exception e)
{
Toast.makeText(this , "erorr in Json Object Request ",Toast.LENGTH_LONG).show();
}
答案 0 :(得分:0)
创建JsonArray
将jsonObjects添加到该数组
然后从JsonObject
向JsonArray
发送完整的JsonObjectRequest
广告
答案 1 :(得分:0)
尝试将params
代替jsonParams
放入JsonObjectRequest
,即
替换
JsonObjectRequest postRequest = new JsonObjectRequest(Request.Method.POST, url,new JSONObject(jsonParams),...
。通过强>
JsonObjectRequest postRequest = new JsonObjectRequest(Request.Method.POST, url,params,...
或按照以下方式从地图中检索
jsonParams.get("key")