2

I received the error: JSON Parser﹕ Error parsing data org.json.JSONException: Value Success of type java.lang.String cannot be converted to JSONObject at success = json.getBoolean(SUCCESS);

I searched around SO and the Internet and I am still not understanding what I am doing wrong.

My JSON looks like: Success

  @Override
protected Boolean doInBackground(Void... params) {
    //Log.v("LoginActivity", "UserLoginTask-AsyncTask-doinBackground");
    Boolean success;
    String username = mUsernameView.getText().toString();
    String password = mPasswordView.getText().toString();

    try {

        //Building Parameters
        ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
        postParameters.add(new BasicNameValuePair("username", mUsernameView.getText().toString()));
        postParameters.add(new BasicNameValuePair("password", mPasswordView.getText().toString()));

        Log.d("request!", "starting");

        //getting product details by making HTTP request
        //JSONObject json = jsonParser.makeHttpRequest(LOGIN_SUCCESS_URL, "POST", postParameters);
        //Error parsing data org.json.JSONException:
        //Value Success of type java.lang.String cannot be converted to JSONObject

        String response = jsonParser.makeHttpRequest(LOGIN_SUCCESS_URL, "POST", postParameters);
        Log.v("SUCCESS", SUCCESS.toString());

        // Log.v("json", json.toString()); //null
        //success = json.getBoolean(SUCCESS);

        //if (success == true) {
        if (response.toLowerCase().contains("success")) {
            // Log.d("Login Successful!", json.toString());
            Toast.makeText(LogIn.this, "Login Successful!", Toast.LENGTH_LONG).show();
            Intent i = new Intent(getApplicationContext(), Main.class);
            finish();
            startActivity(i);
            //return json.getBoolean(FAILURE);
        } else {
            //Log.d("Login Failure!", json.getString(FAILURE));
             Toast.makeText(LogIn.this, "Login Fail!", Toast.LENGTH_LONG).show();
            Intent i = new Intent(getApplicationContext(), MainActivity.class);
            finish();
            startActivity(i);
        }
         } catch (JSONException e) { //error here
         e.printStackTrace();
         }

        return null;
    }
}

public String makeHttpRequest(String url, String method, List<NameValuePair> params) {
        // Making HTTP request
        try {

            // check for request method
            if(method == "POST"){
                // request method is POST
                // defaultHttpClient
                DefaultHttpClient httpClient = new DefaultHttpClient();
                Log.v("makeHttpRequest client", httpClient.toString());
                HttpPost httpPost = new HttpPost(url);
                Log.v("makeHttpRequest post", httpPost.toString());
                httpPost.setEntity(new UrlEncodedFormEntity(params));

                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
                Log.v("makeHttpRequest is", is.toString());

            }else if(method == "GET"){
                // request method is GET
                DefaultHttpClient httpClient = new DefaultHttpClient();
                String paramString = URLEncodedUtils.format(params, "utf-8");
                url += "?" + paramString;
                HttpGet httpGet = new HttpGet(url);

                HttpResponse httpResponse = httpClient.execute(httpGet);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
            }

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

        try {
            //BufferedReader reader = new BufferedReader(new InputStreamReader(
                   // is, "iso-8859-1"), 8);
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, HTTP.UTF_8), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
            Log.v("json=sb", sb.toString());
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        return json;

    }
JohnWilliams
  • 139
  • 1
  • 13
  • Show error log or response which getting from server in `Log.v("json", json.toString());` line – ρяσѕρєя K Nov 06 '15 at 18:11
  • If you are using IntelliJ or Android Studio, try setting a breakpoint at the `success = json.getBoolean(SUCCESS);` line and test the JSON in the local console. – Jack Ryan Nov 06 '15 at 18:12
  • @ρяσѕρєяK Null pointer exception since the makeHttpRequest cannot be completed since Error parsing data org.json.JSONException: Value Success of type java.lang.String cannot be converted to JSONObject – JohnWilliams Nov 06 '15 at 18:13
  • where is you json data? – Androider Nov 06 '15 at 18:16
  • @JohnWilliams: any issue in showing json data or full error log ? – ρяσѕρєя K Nov 06 '15 at 18:16
  • @JohnWilliams: i think problem is due to ` Log.v("json", json.toString());` line because `json` is `null` try it as `Log.v("json", "JSONObject ::"json);` and check what you are getting in log – ρяσѕρєя K Nov 06 '15 at 18:23
  • @ρяσѕρєяK I removed the logging as a whole and am getting null at success = json.getBoolean(SUCCESS);. Please see update. Could this error have to do with the JSON Object's configuration? – JohnWilliams Nov 06 '15 at 18:26
  • @JohnWilliams: Agree getting NPE because `json` is `null`. add logs inside `makeHttpRequest` method and check what getting from server – ρяσѕρєя K Nov 06 '15 at 18:31
  • @ρяσѕρєяK Does agree mean that you believe his error have to do with the JSON Object's configuration? And updated. – JohnWilliams Nov 06 '15 at 18:33
  • @JohnWilliams: please add log for `json = sb.toString();` line in `makeHttpRequest` method and check what getting from server and if possible please share latest logs with us – ρяσѕρєя K Nov 06 '15 at 18:36
  • @ρяσѕρєяK Updated. json = sb.toString() has a value of Success – JohnWilliams Nov 06 '15 at 18:40
  • @JohnWilliams: see my answer probably help you in fixing issue – ρяσѕρєя K Nov 06 '15 at 18:48

1 Answers1

1

As in log:

Parser﹕ Error parsing data org.json.JSONException: Value Success of type java.lang.String cannot be converted to JSONObject

Means not getting JSONObject from server but just getting Success String as response from server.

So, no need to convert json to JSONObject. do following change to get it work :

1. Change return type of makeHttpRequest method to String and return json(remove or comment jObj = new JSONObject(json); line )

2. In doInBackground call makeHttpRequest method as:

String response = jsonParser.makeHttpRequest(LOGIN_SUCCESS_URL,
                                            "POST", 
                                             postParameters);
if(response.toLowerCase().contains("success")){
  /// do task
}else{
  // do task
}
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213