Girraj Sharma
Girraj Sharma

Reputation: 19

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

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)

Upvotes: 0

Views: 2261

Answers (3)

alighanemy
alighanemy

Reputation: 1

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();
        }

Upvotes: 0

Ashish
Ashish

Reputation: 116

Your Retrofit callback response listener will be Response type of 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

Upvotes: 0

Elias
Elias

Reputation: 1562

As the exception implies, the response String cannot be converted to a 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.

from: 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. 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.

Upvotes: 1

Related Questions