【问题标题】:Volley String cannot be converted to JSONObjectVolley String 无法转换为 JSONObject
【发布时间】:2015-07-23 15:57:53
【问题描述】:

只要将数组放入 Params,我总是会收到以下错误。即使转换为 String 后,它仍然会出现该错误。该代码在其中没有联系人列表数组的情况下工作正常。有什么想法吗?

错误

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

示例响应:

{
"username": "test2",
"lists": [
    "contact_0",
    "contact_1",
    "contact_2",
    "contact_3",
    "contact_4",
    "contact_5",
    "contact_6",
    "contact_7",
    "contact_8",
    "contact_9"
]
}

ArrayList<String> contactList = new ArrayList<String>();
public String joinInfo;


Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
        while (phones.moveToNext())
        {
            String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            System.out.println("name : " + name + ", ID : " + phoneNumber);
            joinInfo = name;
            contactList.add(joinInfo);
        }
        phones.close();

 RequestQueue rq = Volley.newRequestQueue(this);

        JSONObject params = new JSONObject();
        try {
            params.put("username", "test2");
            params.put("lists", contactList.toString()); // When i change this to simply "test" a string, it works fine.
            Log.d("PANDA", contactList.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }

        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
                "http://postcatcher.in/catchers/55521f03f708be0300001d28", params, //Not null.
                new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {
                        Log.d("PANDA", response.toString());
                    }
                }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d("PANDA", "Error: " + error.getMessage());
                Log.d("PANDA", error.toString());
            }
        });

        // Adding request to request queue
        rq.add(jsonObjReq);

【问题讨论】:

  • 你能发布回复吗?
  • @inmyth 没有响应,我只得到 java.lang.String 类型的 Created 无法转换为 JSONObject。我尝试了一个在线邮政捕手。什么都没有
  • @inmyth 你是怎么得到这个回复的。看不懂,说真的只有在我开始添加东西后,我才设法得到一个 [],错误就出来了
  • 我刚刚点击了您的链接并等待。那么它现在有效吗?
  • 尝试做:JSONArray jarr=new JSONArray(contactList); params.put("列表", jarr);您可能会在循环中填充虚拟数据,例如 contactList.add("Contact_"+i)

标签: java android arrays json android-volley


【解决方案1】:

PostCatcher 虽然允许我们发布请求,但它的响应基本上是一个纯字符串“Created”,而不是 Json 格式。因此,我们的客户端代码无法确定它并引发错误。有一件事是即使没有带有普通 (String, String) K,V 对的 ArrayList 对象,它也会失败。

如果您尝试通过 Advanced Rest Client 发送请求,您可以验证它(见附件)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-09
    • 1970-01-01
    • 2016-11-13
    • 2017-01-27
    • 1970-01-01
    相关资源
    最近更新 更多