gpt4 book ai didi

java - Volley String 无法转换为 JSONObject

转载 作者:行者123 更新时间:2023-12-01 11:33:50 30 4
gpt4 key购买 nike

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

错误

com.android.volley.ParseError: org.json.JSONException: Value Created of type java.lang.String cannot be converted to 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);

最佳答案

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

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

关于java - Volley String 无法转换为 JSONObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30196601/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com