Php 截击错误。解析错误响应返回
调用和解析非常简单的json,但无论我做什么尝试,都会得到以下结果 com.android.volley.ParseError:org.json.JSONException:Value$items of 无法将类型java.lang.String转换为JSONObject 以下json是在PHP脚本中创建的。通过截取请求调用时,脚本加载json,处理几个字段,然后将其保存回文件 jsonPhp 截击错误。解析错误响应返回,php,android,json,android-volley,Php,Android,Json,Android Volley,调用和解析非常简单的json,但无论我做什么尝试,都会得到以下结果 com.android.volley.ParseError:org.json.JSONException:Value$items of 无法将类型java.lang.String转换为JSONObject 以下json是在PHP脚本中创建的。通过截取请求调用时,脚本加载json,处理几个字段,然后将其保存回文件 json $items=array ( 0 => array ( 'first' =>
$items=array (
0 =>
array (
'first' => 'Tom',
'last' => 'Sawyer',
'email' => 'u@gmail.com',
'phone' => '1112223344',
'zip' => '11122',
'status' => '3',
),
);
$unObiect->items = $items;
$json = json_encode($unObiect);
echo($json);
json是通过
$data[]=array(
'first'=>$temp[0]['first'],
'last'=>$temp[0]['last'],
'phone'=>$temp[0]['phone'],
'email'=>$temp[0]['email'],
'zip'=>$temp[0]['zip'],
);
file_put_contents($filename, '$items=', FILE_APPEND);
file_put_contents($filename, var_export($data,true) . ';'."\n", FILE_APPEND);
file_put_contents($filename, '$unObiect->items = $items;'."\n", FILE_APPEND);
file_put_contents($filename, '$json = json_encode($unObiect);'."\n", FILE_APPEND);
file_put_contents($filename, 'echo($json);'."\n", FILE_APPEND);
file_put_contents($filename, '?>', FILE_APPEND);
截击请求
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, dataurl, new Response.Listener <JSONObject>() {
@Override
public void onResponse(JSONObject response) {
if (response != null) {
try {
userArray = response.getJSONArray("items");
for (int i = 0; i < userArray.length(); i++) {
JSONObject tempObj = userArray.getJSONObject(i);
UserData userData = new UserData();
userData.setFirst_name(tempObj.getString("first"));
userData.setLast_name(tempObj.getString("last"));
userData.setUser_phone(tempObj.getString("phone"));
userData.setEmail(tempObj.getString("email"));
userData.setTrialdays(tempObj.getString("day"));
userData.setStatus(tempObj.getString("status"));
userList.add(userData);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(SplashActivity.this, "No Registration. . ." + error, Toast.LENGTH_SHORT).show();
checkBox();
}
});
JsonObjectRequest-JsonObjectRequest=new-JsonObjectRequest(Request.Method.GET、dataurl、new-Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
if(响应!=null){
试一试{
userArray=response.getJSONArray(“项”);
对于(int i=0;i
我有一种感觉,这与修改后json的保存方式有关,但我不能指出主要原因,因为我使用的是第二个json文件,在第二次截击请求中以相同方式创建了不同的信息,并且没有收到任何错误。任何建议都将不胜感激。您需要在GET请求的标题中将
内容类型设置为application/json
。创建请求时重写getHeaders
函数。下面是一个java示例
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, jsonBody,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("TAG", response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("TAG", error.getMessage(), error);
}
}){
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/json");
return params;
}
};
JsonObjectRequest-JsonObjectRequest=newjsonobjectrequest(url,jsonBody,
新的Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
Log.d(“TAG”,response.toString());
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
Log.e(“TAG”,error.getMessage(),error);
}
}){
@凌驾
public Map getHeaders()引发AuthFailureError{
Map params=新的HashMap();
参数put(“内容类型”、“应用程序/json”);
返回参数;
}
};
post your json response.。谢谢,我以前看过这个建议,只是因为它与我的情况有关,所以我把它看了一遍。不知道为什么这么说很有道理。很高兴知道这有帮助。