我总是收到以下错误,只要我把一个数组PARAMS。即使在转换为字符串,仍然给出了错误。在code正常工作而不里面的contactlist阵列。你知道吗?
错误
com.android.volley.ParseError:org.json.JSONException:价值创造 类型java.lang.String中不能转换为JSONObject的
样本响应:
{
用户名:TEST2,
清单:
contact_0
contact_1
contact_2
contact_3
contact_4
contact_5
contact_6
contact_7
contact_8
contact_9
]
}
的ArrayList<字符串> contactList =新的ArrayList<字符串>();
公共字符串joinInfo;
。光标手机= getContentResolver()查询(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,NULL,NULL,NULL,NULL);
而(phones.moveToNext())
{
字符串名称= phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
串phoneNumber的= phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
的System.out.println(姓名:+姓名+,ID:+ phoneNumber的);
joinInfo =名称;
contactList.add(joinInfo);
}
phones.close();
请求队列RQ = Volley.newRequestQueue(本);
JSONObject的PARAMS =新的JSONObject();
尝试 {
params.put(用户名,测试2);
params.put(名单,contactList.toString()); //当我更改为简单的测试的字符串,它工作正常。
Log.d(熊猫,contactList.toString());
}赶上(JSONException E){
e.printStackTrace();
}
JsonObjectRequest jsonObjReq =新JsonObjectRequest(Request.Method.POST,
http://postcatcher.in/catchers/55521f03f708be0300001d28,则params,// NOT NULL。
新Response.Listener<的JSONObject>(){
@覆盖
公共无效onResponse(JSONObject的响应){
Log.d(熊猫,response.toString());
}
},新Response.ErrorListener(){
@覆盖
公共无效onErrorResponse(VolleyError错误){
VolleyLog.d(熊猫,错误:+ error.getMessage());
Log.d(熊猫,error.toString());
}
});
//添加请求,请求队列
rq.add(jsonObjReq);
解决方案
PostCatcher虽然允许我们的请求后,它的反应基本上是一个纯字符串创造,而不是JSON格式。因此我们的客户code是无法确定它并引发错误。有一件事是,即使没有ArrayList对象,它与普通的(字符串,字符串)K,V对也将失败。
您可以验证它,如果你试图通过高级REST客户端发送请求(见附件)
I'm always getting the following error as long as i put a array into Params. Even after converting to String it still gives that error. The code works fine without the contactlist array inside it. Any idea?
Error
com.android.volley.ParseError: org.json.JSONException: Value Created of type java.lang.String cannot be converted to JSONObject
Sample response:
{
"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 although allowing us to post requests, its response is basically a plain string "Created" and not in Json format. As such our client code is not able to ascertain it and throws error. One thing is even without ArrayList object that is with plain (String, String) K,V pair also it would fail.
You can verify it if you try sending request through Advanced Rest Client (see attached)
相关推荐
最新文章