-2

I am working on Login page and data receiving by server.Here IP, username and password is Entered by user. When IP is correct so he is checked the username and password.After that he is receiving the JSON message.Here, result is saved the ""User exists"" and i am checking the "User exists".

When i am tried to convert in JSONObject so he gives the error..... here is the error description

JSON Response: "User Exists"

MainActivity.java

  private class HttpAsyncTask extends AsyncTask<String, Void, String>{
    @Override
    protected String doInBackground(String... urls) {
        return GET(urls[0]);
    }
    @Override
    protected void onPostExecute(String result) {
        try {
            JSONObject jsonResponse = new JSONObject(result);
            if(jsonResponse.equals("User exists")){
                Toast.makeText(getBaseContext(),"Login Successfully",Toast.LENGTH_LONG).show();
                Intent intent = new Intent(getApplicationContext(),Welcome.class);
                startActivity(intent);
            }
            else{
                Toast.makeText(getBaseContext(),"User does not exists",Toast.LENGTH_LONG).show();
            }
         } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

LogCat

12-27 10:53:20.662 507-572/system_process E/WifiStateMachine: [1,482,836,000,662 ms] noteScanstart no scan source uid -1
12-27 10:53:21.375 6641-6662/com.example.anew.addbtnontitlebar D/result: "User exists"
12-27 10:53:21.376 6641-6641/com.example.anew.addbtnontitlebar D/error: Value User exists of type java.lang.String cannot be converted to JSONObject
12-27 10:53:23.168 507-572/system_process E/WifiStateMachine: [1,482,836,003,168 ms] noteScanEnd no scan source onTime=0
Charuක
  • 12,953
  • 5
  • 50
  • 88

2 Answers2

0

You can access JSON values with key value pairs. so you need to obtain a key from JSON data and compare with values.

JSONObject jsonResponse = new JSONObject(result); 
String jsonKey=jsonResponse.getString("result");
if(jsonKey.equeal("User exists"))
     {
       //do you stuff
    }
Maaz Patel
  • 756
  • 2
  • 7
  • 20
0

You get a json response but its in Invalid format, its a string value

To identify its in the valid format you can use http://jsonlint.com/ or something similar.Just paste your response and check it

if its in a valid json format it should be like this

{
    "status": "user exists"
}

If you had a response like above you can do this;

JSONObject jsonResponse = new JSONObject(result);
String status = jsonResponse .getString("status");

We catch String using the key, now you don't have any key. You have only a String value.So there is no point of passing that value to a JSONObject

so you can do this or ask for a better response form the developer who returns that responce;

@Override
    protected void onPostExecute(String result) {
        if (result!= null){
              if (result.equals("User exists")){
                      Toast.makeText(getBaseContext(),"User exists",Toast.LENGTH_LONG).show();
                     }
                   else{
                         Toast.makeText(getBaseContext(),result,Toast.LENGTH_LONG).show();
                         }
              }
    }
Charuක
  • 12,953
  • 5
  • 50
  • 88
  • i am chacked JSON response on URL and this is valid and i am follow your way to solving the problem but problem is not solved................... – Himanshu Srivastava Dec 27 '16 at 07:17
  • @Himanshu Srivastava if you get only something like this "User Exists" it is not valid!!!!!!!!! check all the people who have commented on it and what they say. check my valid json example. – Charuක Dec 27 '16 at 07:22
  • @Himanshu Srivastava read here >http://stackoverflow.com/questions/7487869/is-this-simple-string-considered-valid-json – Charuක Dec 27 '16 at 07:24
  • @Himanshu Srivastava in your onPostExecute add a log and print what is your result and see what do you receive – Charuක Dec 27 '16 at 07:26
  • @Himanshu Srivastava Print that result you get in onPostExcute and post it – Charuක Dec 27 '16 at 07:38
  • @Himanshu Srivastava its saying the same thing i can only help you if you put a Log / sys.out and show the result to me.like this `protected void onPostExecute(String result) { System.out.println("String Result >:" +result); // other your code }` Run your app and check whats the result value you get there it will print in your log search using "String Result" – Charuක Dec 27 '16 at 11:02
  • @Himanshu Srivastava or try what i said ..`@Override protected void onPostExecute(String result) { if (result!= null){ if (result.equals("User exists")){ Toast.makeText(getBaseContext(),"User exists",Toast.LENGTH_LONG).show(); } else{ Toast.makeText(getBaseContext(),result,Toast.LENGTH_LONG).show(); } } }` and post the log if there is any error – Charuක Dec 27 '16 at 11:05
  • String Result >:" +result---""User exists"" when i am debugging it is show on String Result after that he no error show – Himanshu Srivastava Dec 27 '16 at 11:30
  • @Himanshu Srivastava good then use the method that i gave you for your onPostExcute and see "User exists" Toast gets printed :) do not add other codes in it' – Charuක Dec 27 '16 at 11:42