简体   繁体   English

Volley String无法转换为JSONObject

[英]Volley String cannot be converted to JSONObject

I'm always getting the following error as long as i put a array into Params. 只要将数组放入Params中,我总是会遇到以下错误。 Even after converting to String it still gives that error. 即使转换为String后,它仍然会给出该错误。 The code works fine without the contactlist array inside it. 该代码可以在其中没有contactlist数组的情况下正常工作。 Any idea? 任何想法?

Error 错误

com.android.volley.ParseError: org.json.JSONException: Value Created of type java.lang.String cannot be converted to JSONObject com.android.volley.ParseError:org.json.JSONException:创建的类型为java.lang.String类型的值无法转换为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. PostCatcher尽管允许我们发布请求,但其响应基本上是纯字符串“ Created”,而不是Json格式。 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. 一件事是,即使没有带有普通(字符串,字符串)K,V对的ArrayList对象,它也会失败。

You can verify it if you try sending request through Advanced Rest Client (see attached) 如果您尝试通过Advanced Rest Client发送请求,则可以验证它(请参阅附件) 在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 字符串不能转换为JSONObject吗? - String cannot be converted to JSONObject? (URL)字符串不能转换为JSONObject - (URL) String cannot be converted to JSONObject JSONException 字符串无法转换为 JSONObject - JSONException String cannot be converted to JSONObject java.lang.string 类型的连接值无法转换为 jsonobject [Android Volley Library] - Value connected of type java.lang.string cannot be converted to jsonobject [Android Volley Library] java.lang.String 类型的值无法转换为 JSONObject (Volley Android Php API) - Value of type java.lang.String cannot be converted to JSONObject ( Volley Android Php API) volley.parsererror:org.json.JSONException:类型java.lang.string的值br不能转换为JSONObject - volley.parsererror:org.json.JSONException: value br of type java.lang.string cannot be converted to JSONObject Volley 的 JsonObjectRequest 给出“org.json.JSONException: Value - Volley's JsonObjectRequest gives "org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject" exception Volley的JsonObjectRequest提供了“ org.json.JSONException:值 - Volley's JsonObjectRequest gives “org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject” exception com.android.volley.parseerror org.json.json异常值类型为java.lang.String的无法转换为JSONObject - com.android.volley.parseerror org.json.jsonexception value yes of type java.lang.String cannot be converted to JSONObject Volley库W / System.err:org.json.JSONException:类型为java.lang.String的Value Connected无法转换为JSONObject - Volley library W/System.err: org.json.JSONException: Value Connected of type java.lang.String cannot be converted to JSONObject
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM