简体   繁体   English

onResponse(String response) try { JSONObject jsonObject=new JSONObject(response);java.lang.String 无法转换为 JSONObject

[英]onResponse(String response) try { JSONObject jsonObject=new JSONObject(response);java.lang.String cannot be converted to JSONObject

I can't find answer.我找不到答案。 tried many websites including this.尝试了很多网站,包括这个。 i want to know where from the problem coming.我想知道问题出在哪里。

Error is: org.json.JSONException: Value connection of type java.lang.String cannot be converted to JSONObject错误是:org.json.JSONException: 类型 java.lang.String 的值连接无法转换为 JSONObject

This is my andorid code:-这是我的安卓代码:-

    StringRequest stringRequest=new StringRequest(Request.Method.POST, URL_LOGIN,
            new Response.Listener<String>() {

                @Override
                public void onResponse(String response) {

                    try {

                        JSONObject jsonObject=new JSONObject(response);

                        String success=jsonObject.getString("success");

                        JSONArray jsonArray=jsonObject.getJSONArray("login");

                        if (success.equals("1")){
                            for (int i=0;i<jsonArray.length();i++) {
                                JSONObject object=jsonArray.getJSONObject(i);

                                String uname=object.getString("username").trim();
                                String email=object.getString("email");

                                Toast.makeText(MainActivity.this, "Login Successful!!! \n Your Name: "+uname+"\nYour Email: "+email, Toast.LENGTH_SHORT).show();
                                loading.setVisibility(View.GONE);
                            }

                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                        Toast.makeText(MainActivity.this," Error!!"+e.toString(),Toast.LENGTH_LONG).show();
                        loading.setVisibility(View.GONE);
                        signin_btn.setVisibility(View.VISIBLE);
                    }

                }
            },

Logcat逻辑猫

2019-09-06 19:40:37.264 7944-7944/com.example.project W/System.err: org.json.JSONException: Value connection of type java.lang.String cannot be converted to JSONObject 2019-09-06 19:40:37.264 7944-7944/com.example.project W/System.err: at org.json.JSON.typeMismatch(JSON.java:111) 2019-09-06 19:40:37.264 7944-7944/com.example.project W/System.err: at org.json.JSONObject.(JSONObject.java:160) 2019-09-06 19:40:37.264 7944-7944/com.example.project W/System.err: at org.json.JSONObject.(JSONObject.java:173) 2019-09-06 19:40:37.264 7944-7944/com.example.project W/System.err: org.json.JSONException: 类型 java.lang.String 的值连接无法转换为 JSONObject 2019-09-06 19:40:37.264 7944-7944/com.example.project W/System.err:在 org.json.JSON.typeMismatch(JSON.java:111) 2019-09-06 19:40:37.264 7944-7944/com .example.project W/System.err: at org.json.JSONObject.(JSONObject.java:160) 2019-09-06 19:40:37.264 7944-7944/com.example.project W/System.err: at org.json.JSONObject.(JSONObject.java:173)

As the exception implies, the response String cannot be converted to a JSONObject :正如异常所暗示的那样, response String 不能转换为JSONObject

public JSONObject(java.lang.String source) throws JSONException

Throws: JSONException - If there is a syntax error in the source string or a duplicated key.抛出: JSONException - 如果源字符串中存在语法错误或重复的键。

from: http://stleary.github.io/JSON-java/org/json/JSONObject.html#JSONObject-java.lang.String-来自: http : //stleary.github.io/JSON-java/org/json/JSONObject.html#JSONObject-java.lang.String-

Ensure your response String (obtained from a POST request to URL_LOGIN ) is indeed a valid JSON string.确保您的response字符串(从对URL_LOGIN的 POST 请求URL_LOGIN )确实是有效的 JSON 字符串。 Either write an end-to-end test incorporating the system hosting URL_LOGIN or manually test with a client such as Postman that the system serving URL_LOGIN works as intended given the request from your application.要么编写包含托管URL_LOGIN的系统的端到端测试,要么使用 Postman 等客户端手动测试服务URL_LOGIN的系统URL_LOGIN按照应用程序的请求工作。

Your Retrofit callback response listener will be Response type of Retrofit.您的 Retrofit 回调响应侦听器将是 Retrofit 的响应类型。 Make your code like this and it will work fine.使您的代码像这样,它会正常工作。

StringRequest stringRequest=new StringRequest(Request.Method.POST, URL_LOGIN,
        new Response.Listener<Response>() {

            @Override
            public void onResponse(Response response) {

                try {

                    JSONObject jsonObject=new JSONObject(response.body());

                    String success=jsonObject.getString("success");

                    JSONArray jsonArray=jsonObject.getJSONArray("login");

                    if (success.equals("1")){
                        for (int i=0;i<jsonArray.length();i++) {
                            JSONObject object=jsonArray.getJSONObject(i);

                            String uname=object.getString("username").trim();
                            String email=object.getString("email");

                            Toast.makeText(MainActivity.this, "Login Successful!!! \n Your Name: "+uname+"\nYour Email: "+email, Toast.LENGTH_SHORT).show();
                            loading.setVisibility(View.GONE);
                        }

                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                    Toast.makeText(MainActivity.this," Error!!"+e.toString(),Toast.LENGTH_LONG).show();
                    loading.setVisibility(View.GONE);
                    signin_btn.setVisibility(View.VISIBLE);
                }

            }
        },

L

when I use these statements my code doesn't show a problem当我使用这些语句时,我的代码没有显示问题

JSONObject js=new JSONObject(response);
final String res = js.getString("result");

but when I don't use it work correctly:但是当我不使用它时,它工作正常:

        try {
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
            HttpResponse httpResponse = httpClient.execute(httpPost);
            final String response = EntityUtils.toString(httpResponse.getEntity());
            JSONObject js=new JSONObject(response);
            final String res =  js.getString("result");
            JSONObject js=new JSONObject(response);
            final String res =  js.getString("result");






            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getApplicationContext(), "Problem", Toast.LENGTH_SHORT).show();
                }
            });
        }


        catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM