简体   繁体   中英

org.json.JSONException: No value for success Exception

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.

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

Eg 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) {
    // ...
}
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
}

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!");
       }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM