【问题标题】:Volley error.ParseError Response Return [duplicate]Volley error.ParseError 响应返回 [重复]
【发布时间】:2018-03-05 03:41:19
【问题描述】:

非常简单的 json 被调用和解析,但无论我尝试什么,我都会得到以下结果

com.android.volley.ParseError: org.json.JSONException: Value $items of 类型 java.lang.String 无法转换为 JSONObject

以下 json 是在 PHP 脚本中创建的。当通过 Volley 请求调用时,脚本会加载 json,操作几个字段,然后将其保存回文件。

json

$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();
    }
});

我觉得这与修改后 json 保存回来的方式有关,但我无法指出它主要是因为我正在使用第二个 json 文件,该文件具有在一秒钟内以相同方式创建的不同信息凌空请求并没有收到错误,加载完美。任何建议将不胜感激。

【问题讨论】:

  • 发布您的 json 响应..

标签: php android json android-volley


【解决方案1】:

您需要在 GET 请求的标头中将 content-type 设置为 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; 
    } 
};

【讨论】:

  • 谢谢,我以前看过这个建议,只是简单地看了看它,因为它与我的情况有关。不知道为什么说得通。
  • 很高兴知道这有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-15
  • 1970-01-01
  • 2021-09-09
  • 2018-01-01
相关资源
最近更新 更多