0

My Registration AsyncTask working fine and my data is saving on PHP MYSQL working good but I am facing a exception.

 public class SignupJson extends AsyncTask<String,String,String> {


            //SignupJSONParser sjson = new SignupJSONParser();
            JSONObject json = new JSONObject();
            //private static final String TAG_SUCCESS = "success";

            HttpParams mhttpparams = new BasicHttpParams();


            @Override
            protected void onPreExecute() {

                pDialog = new ProgressDialog(SignupActivity.this);
                pDialog.setMessage("Creating Account..");
                pDialog.setIndeterminate(false);
                pDialog.setCancelable(true);
                pDialog.show();
                super.onPreExecute();
            }

            @Override
            protected String doInBackground(String... args) {

                String getName = _Name.getText().toString();
                String  getEmail = _Email.getText().toString();
                String  getPassword = _Password.getText().toString();

         List<NameValuePair> pair = new    ArrayList<NameValuePair>();
                pair.add(new BasicNameValuePair("name",getName));
                pair.add(new BasicNameValuePair("id",getEmail));
                pair.add(new BasicNameValuePair("pass", getPassword));

                // int success=0;
                json = JSONParser.makeHttpRequest( URL,"POST", pair);

                Log.d("Create Response", json.toString());

                try {

                    String success = json.getString(TAG_SUCCESS);

                    //success = json.getInt(TAG_SUCCESS);

                    if (json.getString(TAG_SUCCESS)!=null) {

                        if (Integer.parseInt(success)==1)
                        {


                        Log.d("User Created!", json.toString());
                        Intent login = new Intent(getApplicationContext(), LoginActivity.class);
                        startActivity(login);
                        // closing this screen

                        finish();

                        //return json.getString(TAG_MESSAGE);

                    }
                        else {

                            Log.d("Registration Failure!", json.getString(TAG_MESSAGE));

                            return json.getString(TAG_MESSAGE);
                        }
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                    // success=0;
                }

                return null;
                // return ""+success;
            }

            @Override
            protected void onPostExecute(String s) {


                pDialog.dismiss();

                if (json != null){
                    Toast.makeText(getApplicationContext(),"Registration Done",Toast.LENGTH_SHORT).show();
                    //Toast.makeText(MainActivity.this, json.toString(), Toast.LENGTH_LONG).show();
                }

            }



        }

    }

LogCat:

com.example.ahmadkhan.routeapp W/System.err﹕ org.json.JSONException: No value for success /com.example.ahmadkhan.routeapp W/System.err﹕ at org.json.JSONObject.get(JSONObject.java:354) com.example.ahmadkhan.routeapp W/System.err﹕ at org.json.JSONObject.getString(JSONObject.java:514) /com.example.ahmadkhan.routeapp W/System.err﹕ at com.example.ahmadkhan.routeapp.SignupActivity$SignupJson.doInBackground(SignupActivity.java:169) com.example.ahmadkhan.routeapp W/System.err﹕ at com.example.ahmadkhan.routeapp.SignupActivity$SignupJson.doInBackground(SignupActivity.java:129) com.example.ahmadkhan.routeapp W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:287) com.example.ahmadkhan.routeapp W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:234) com.example.ahmadkhan.routeapp W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) com.example.ahmadkhan.routeapp W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:108com.example.ahmadkhan.routeapp W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:57com.example.ahmadkhan.routeapp W/System.err﹕ at java.lang.Thread.run(Thread.java:841)

Error on Line

String success = json.getString(TAG_SUCCESS);

//success = json.getInt(TAG_SUCCESS);

if (json.getString(TAG_SUCCESS)!=null) {
}

Can Anyone suggest me a solution how to get rid off this error and also tell me how to handle a server response in case of successful and failure of connection with PHP Wamp Server.

Aracthor
  • 5,757
  • 6
  • 31
  • 59
ahmed aslam
  • 11
  • 1
  • 6
  • Possible duplicate of http://stackoverflow.com/questions/17489052/jsonobject-parsing-error-when-there-is-no-value – dhke May 05 '15 at 08:54

3 Answers3

1
org.json.JSONException: No value for success
//means no key-value found for this key - "success"

handle this exception using has() method

if(<jsonobject>.has("success"))
{
   //your code
}
Angad Tiwari
  • 1,738
  • 1
  • 12
  • 23
0

JSONObject.get*() methods do not return null when the input key was not found in the object, they throw a JSONException.

E.g. getString():

Throws: JSONException - if there is no string value for the key.

Use JSONObject.opt*() methods, instead:

String success = json.optString(TAG_SUCCESS);
if (success != null) {
    // ...
}
dhke
  • 15,008
  • 2
  • 39
  • 56
  • dhke i tried your solution my crashed nothing is inserted now :( and i am getting this kind of error AddressBook Labels [en_US]: [, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, Α, Β, Γ, Δ, Ε – ahmed aslam May 05 '15 at 10:15
  • I'm pretty sure the adressbook log has nothing to do with your application, so you may need to check the logcat again to find the actual cause of the crash. Also: you made sure that the JSON object contains the correct data? Currently it looks like you are guessing at the error and that won't work. – dhke May 05 '15 at 10:49
  • #dhke i solved my issue there was a problem with my PHP JSON it didn't return a success variable result 1 or 0 :) any way thanks for your time and concern. – ahmed aslam May 05 '15 at 13:01
0

try with this i hope it will help you..!

JSONObject json = JSONParser.makeHttpRequest( URL,"POST", pair);

           if(json!=null)
             {
            Log.d("Create Response", json.toString());

            try {

                String success = json.getString(TAG_SUCCESS);

                //success = json.getInt(TAG_SUCCESS);

                if (json.getString(TAG_SUCCESS)!=null) {

                    if (Integer.parseInt(success)==1)
                    {


                    Log.d("User Created!", json.toString());
                    Intent login = new Intent(getApplicationContext(),  LoginActivity.class);
                    startActivity(login);
                    // closing this screen

                    finish();

                    //return json.getString(TAG_MESSAGE);

                }
                    else {

                        Log.d("Registration Failure!", json.getString(TAG_MESSAGE));

                        return json.getString(TAG_MESSAGE);
                    }
                }

            } catch (JSONException e) {
                e.printStackTrace();
                // success=0;
            }
      }else{
          Log.d("getting null response from server!");
       }
vikas balyan
  • 766
  • 6
  • 12
  • nothing happened and my app crash i got this exception.I/ContactLocale﹕ AddressBook Labels [en_US]: [, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, Α, Β, Γ, Δ, Ε, Ζ, Η, Θ, Ι, Κ, Λ, Μ, Ν, Ξ, Ο, Π, Ρ, Σ, Τ, Υ, Φ, Χ, Ψ, Ω, , А, Б, В, Г, Д, Е, Є, Ж, З, И, І, Ї, Й, К, Л, М, Н, О, П, Р, С, Т, У, Ф, Х, Ц, Ч, Ш, Щ, Ю, Я, , א, ב, ג, ד, ה, ו, ז, ח, ט, י, כ, ל, מ, נ, ס, ע, פ, צ, ק, ר, ש, ת, , ا, ب, ت, ث, ج, ح, خ, د, ذ, ر, ز, س, ش, ص, ض, ط, ظ, ع, غ, ف, ق, ك, ل, م, ن, ه, و, ي, , ก, ข, ฃ, ค, ฅ, ฆ, – ahmed aslam May 05 '15 at 10:01
  • @ahmedaslam, can you share your Url here or you can check it manually what kind of response u getting from server and check the logcat again to find the actual cause of the crash. – vikas balyan May 05 '15 at 11:32
  • #vikas balyan i solved my issue there was a problem with my PHP JSON it didn't return a success variable result 1 or 0 :) any way thanks for your time and concern. – ahmed aslam May 05 '15 at 13:00