L Wira Satria M
L Wira Satria M

Reputation: 67

JSONException: Value of type java.lang.String cannot be converted to JSONObject (Project Eclipse To Android Studio)

after i import the project and fix some problem i got the json error like the title i mention, can you help me please. In the IDE nothing with error sign (red sign)

ImageProblemHere

if you need anything else just ask below

Here is my code

 @Override
    protected void onPostExecute(String Result) {
        dialog.dismiss();
        if (Result != null) {
            parseJson(Result); // here problem
        } else {
            // intent_depan();
            Toast.makeText(LoginActivity.ctx,
                    "Login Failed, Connection Error", Toast.LENGTH_SHORT)
                    .show();
        }
    }


    // Android monitor show the problem maybe here
    public void parseJson(String s) {
        try {
            JSONObject jobj = new JSONObject();
            String stat = jobj.getString("stat");

            // Log.i("json result", "string result " + s1);
            if (stat.equals("failed")) {
                String msg = jobj.getString("msg");
                Toast.makeText(LoginActivity.ctx, msg, Toast.LENGTH_SHORT)
                        .show();
            } else {
                userName = jobj.getString("name");
                // Toast.makeText(LoginActivity.ctx, userName,
                // Toast.LENGTH_SHORT).show();

                saveLoginState();

                goToMainActivity();
            }

            /*
             * if (s1 == "success") { intent_depan(); } else {
             * intent_depan(); }
             */
        } catch (JSONException e) {
            e.printStackTrace();
            Toast.makeText(LoginActivity.ctx, e.getMessage(),
                    Toast.LENGTH_SHORT).show();
        }
    }

Upvotes: 0

Views: 1051

Answers (2)

Codo
Codo

Reputation: 78825

The server's response cannot cannot be parsed as JSON because the server returns an error message (in HTML) instead of JSON data.

It's a HTTP 404 Not found error, indicating that the URL was probably invalid.

It would also be good practice to check if the HTTP request was successful at all. If it wasn't, don't even try to parse the response as JSON.

Upvotes: 0

Himank shah
Himank shah

Reputation: 131

creat a json object from result like this and before parsing also check the result string is a valid json string or not.

JSONObject jobj = new JSONObject(s);

Upvotes: 1

Related Questions