0

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();
        }
    }
12
  • 4
    First: Please post your error as text in the issue, rather than sharing a screenshot. Second: The response you get is '<h1>404' which isn't valid JSON off course Commented Aug 19, 2016 at 10:21
  • The URL you are calling is wrong Commented Aug 19, 2016 at 10:22
  • 1
    Is that even the code that resulted in that error? I can't really see a line where it tries to parse the invalid JSON. Commented Aug 19, 2016 at 10:22
  • in the parseJson(String s) method..You should create your JSONObject as JSONObject jobj = new JSONObject(s); Commented Aug 19, 2016 at 11:09
  • Your question is not about Android Studio, so please don't tag it. It also isn't the problem because making the same network request from running the code in Eclipse would get the exact same HTTP error response and throw the exact same error. So, the problem is either the API or how you access it Commented Aug 19, 2016 at 11:11

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

This will throw a JSON parse exception on its own. No need to check the string
0

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.

2 Comments

I try to change the url and i got username and password is invalid, it means the url dont have database login like i input ? or any other problem with my code ? before i check on the rest url, the user and pass are true.. but on the apps is invalid
This is difficult to tell without further information. It depends on the specific web app / service, which URLs require authentication and which don't. And there can be a variety of different authentication scheme, e.g. using a cookies, specifying username/password in each request or some sort of token in the request header. If you're stuck with authentication, start a new question and provide all the relevant information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.