Android org.json.JSONException:java.lang.String类型的值失败\u post无法转换为JSONObject
我想用Android org.json.JSONException:java.lang.String类型的值失败\u post无法转换为JSONObject,android,json,android-volley,jsonobject,Android,Json,Android Volley,Jsonobject,我想用volley库将数据发送到服务器当我用volley库将数据发送到服务器时,我得到以下错误: org.json.JSONException: Value failure_post of type java.lang.String cannot be converted to JSONObject 这是我的java代码: public void insertClick(View view) { RequestQueue queue = Vo
volley库
将数据发送到服务器当我用volley库
将数据发送到服务器时,我得到以下错误:
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();
}
public void insertClick(视图)
{
RequestQueue=Volley.newRequestQueue(getApplicationContext());
HashMap jsonParams=新HashMap();
JSONObject参数=新的JSONObject();
尝试
{
参数put(“标题”、“书”);
参数put(“介绍”、“问候”);
}
捕获(JSONException e)
{
Log.i(标签“erorr is:”+e.getMessage().toString());
}
试一试{
JsonObjectRequest postRequest=新的JsonObjectRequest(Request.Method.POST,url,
新的JSONObject(jsonParams),
新的Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
Toast.makeText(getApplicationContext(),“插入ok”,Toast.LENGTH_LONG.show();
}
},
新的Response.ErrorListener()
{
@凌驾
公共无效onErrorResponse(截击错误){
//处理错误
Log.i(标签“erorr is:”+error.getMessage().toString());
Toast.makeText(getApplicationContext(),“插入失败”,Toast.LENGTH_LONG.show();
}
}) {
@凌驾
公共映射getHeaders()引发AuthFailureError{
HashMap headers=新的HashMap();
headers.put(“内容类型”、“应用程序/json”);
返回标题;
}
};
添加(postRequest);
}
捕获(例外e)
{
makeText(这是“Json对象请求中的erorr”,Toast.LENGTH_LONG.show();
}
创建一个JsonArray
将JSONObject添加到该数组中
然后将一个完整的
JsonObject
create fromJsonArray
发送到JsonObjectRequest
创建一个JsonArray
将JSONObject添加到该数组中
然后将一个完整的
JsonObject
create fromJsonArray
发送到JsonObjectRequest
尝试将params
而不是jsonParams
放在JsonObjectRequest
ie中
替换此
JsonObjectRequest postRequest = new JsonObjectRequest(Request.Method.POST, url,new JSONObject(jsonParams),...
与
JsonObjectRequest postRequest = new JsonObjectRequest(Request.Method.POST, url,params,...
或者在放入地图后从地图中检索,如下所示
jsonParams.get("key")
尝试将
params
而不是jsonParams
放入JsonObjectRequest
ie中
替换此
JsonObjectRequest postRequest = new JsonObjectRequest(Request.Method.POST, url,new JSONObject(jsonParams),...
与
JsonObjectRequest postRequest = new JsonObjectRequest(Request.Method.POST, url,params,...
或者在放入地图后从地图中检索,如下所示
jsonParams.get("key")
错误在哪里??新建JSONObject(jsonParams)这仅适用于MAPInstead的
新建JSONObject(jsonParams),
,IMO,您应仅使用参数或新建JSONObject(params),
错误在哪里??新建JSONObject(jsonParams)这仅适用于MAPInstead的新建JSONObject(jsonParams),
,依我看,您应该只使用params
或新JSONObject(params),