I try to connect Login and Registration form through PHP API, But I get the error and I don't understand how its will be resolve. I check the solution regarding this error but I don't understand it. Thank you in advance!
Error :-
W/System.err: org.json.JSONException: Value null of type org.json.JSONObject$1 cannot be converted to JSONObject
Here is my code for Registration form (Login.java)
Login. java (This is the java file where for Registration)
public class Login extends Activity {
EditText editname, editemail, editpassword, editmobile;
Button btnRegister;
private static final String TAG = "Login";
private static final String URL_FOR_REGISTRATION = "http://codexpertise.com/codexpertise.com/apitest/signup.php";
ProgressDialog progressDialog;
ImageButton btnfb;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration);
// Progress dialog
progressDialog = new ProgressDialog(this);
progressDialog.setCancelable(false);
editname = (EditText) findViewById(R.id.editname);
editemail = (EditText) findViewById(R.id.editemail);
editpassword = (EditText) findViewById(R.id.editpassword);
editmobile = (EditText) findViewById(R.id.editmobile);
btnRegister = (Button) findViewById(R.id.btnRegister);
btnfb = (ImageButton)findViewById(R.id.btnfb);
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
submitForm();
}
private void submitForm() {
registerUser(editname.getText().toString(),
editemail.getText().toString(),
editpassword.getText().toString(),
editmobile.getText().toString());
}
private void registerUser(final String name, final String email, final String password,
final String mobile) {
// 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(
Login.this,
MainActivity.class);
startActivity(intent);
finish();
} else {
String errorMsg = jObj.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);
params.put("gender", mobile);
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();
}
});
btnfb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Uri uri = Uri.parse("https://www.facebook.com/"); // missing 'http://' will cause crashed
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
}
}
Here is the some image which i test it on postman
[1
Check this
[2