Search code examples
phpandroidandroid-studioandroid-databasejsonexception

How to solve JSONException


I m trying to do a registration page from an android application activity connectiong the data to my SQLdatabase, but whenever I run it I'm getting this error:

W/System.err: org.json.JSONException: Valu of type java.lang.String cannot be converted to JSONObject

, how to solve this problem?

this register class

public class RegistrationActivity extends AppCompatActivity {

private static final String TAG = "RegisterActivity";
private static final String URL_FOR_REGISTRATION = "http://192.168.1.33/arswebservices/register.php";
ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_registration);
   final EditText NameRegister = (EditText) findViewById(R.id.reg_UserName);
    final  EditText EmailRegister = (EditText) findViewById(R.id.reg_Email);
    final EditText PasswordRegister = (EditText)                                           findViewById(R.id.reg_Password);
    Button registrationButton = (Button) findViewById(R.id.reg_Button);

    // Progress dialog
    progressDialog = new ProgressDialog(this);
    progressDialog.setCancelable(false);
    registrationButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            registerUser(NameRegister.getText().toString(), EmailRegister.getText().toString(),
                    PasswordRegister.getText().toString());
        }
    });
}

private void registerUser(final String name,  final String email, final String password) {
    // Tag used to cancel the request
    String cancel_req_tag = "register";

    progressDialog.setMessage("Adding you ...");
    showDialog();

    StringRequest strReq = new StringRequest(Request.Method.POST,
            URL_FOR_REGISTRATION, new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
            Log.d(TAG, "Register Response: " + response.toString());
            hideDialog();

            try {
                JSONObject jObj = new JSONObject(response);
                boolean error = jObj.getBoolean("error");


                if (!error) {
                    String user = jObj.getJSONObject("user").getString("name");
                    Toast.makeText(getApplicationContext(), "Hi " + user +", You are successfully Added!", Toast.LENGTH_SHORT).show();

                    // Launch login activity
                    Intent intent = new Intent(
                            RegistrationActivity.this,
                            LoginActivity.class);
                    startActivity(intent);
                    finish();
                } else {

                    String errorMsg = jObj.getJSONObject("user").getString("error_msg");
                    Toast.makeText(getApplicationContext(),
                            errorMsg, Toast.LENGTH_LONG).show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e(TAG, "Registration Error: " + error.getMessage());
            Toast.makeText(getApplicationContext(),
                    error.getMessage(), Toast.LENGTH_LONG).show();
            hideDialog();
        }
    }) {
        @Override
        protected Map<String, String> getParams() {
            // Posting params to register url
            Map<String, String> params = new HashMap<String, String>();
            params.put("name", name);
            params.put("email", email);
            params.put("password", password);
            return params;
        }
    };
    // Adding request to request queue
    AppSingleton.getInstance(getApplicationContext()).addToRequestQueue(strReq, cancel_req_tag);
}

private void showDialog() {
    if (!progressDialog.isShowing())
        progressDialog.show();
}

private void hideDialog() {
    if (progressDialog.isShowing())
        progressDialog.dismiss();
}
}

Solution

  • Have u made use of volley ?

    Refer to the link below for registration form , u might find it useful
    https://www.android-examples.com/volley-user-registration/