I am trying to learn how to create a login and registration system for my Android App. I followed a YouTube video for that and I keep getting this error when I run my App.
Error Log:
06-03 14:32:14.646 2563-2563/example.login W/System.err: org.json.JSONException: Value <html><body><script of type java.lang.String cannot be converted to JSONObject
06-03 14:32:14.646 2563-2563/example.login W/System.err: at org.json.JSON.typeMismatch(JSON.java:111)
06-03 14:32:14.646 2563-2563/example.login W/System.err: at org.json.JSONObject.<init>(JSONObject.java:160)
06-03 14:32:14.646 2563-2563/example.login W/System.err: at org.json.JSONObject.<init>(JSONObject.java:173)
06-03 14:32:14.646 2563-2563/example.login W/System.err: at vipanchithreddy.login.RegisterActivity$1$1.onResponse(RegisterActivity.java:41)
06-03 14:32:14.646 2563-2563/example.login W/System.err: at vipanchithreddy.login.RegisterActivity$1$1.onResponse(RegisterActivity.java:37)
06-03 14:32:14.647 2563-2563/example.login W/System.err: at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
06-03 14:32:14.647 2563-2563/example.login W/System.err: at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
06-03 14:32:14.647 2563-2563/example.login W/System.err: at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
06-03 14:32:14.647 2563-2563/example.login W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
06-03 14:32:14.647 2563-2563/example.login W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
06-03 14:32:14.647 2563-2563/example.login W/System.err: at android.os.Looper.loop(Looper.java:148)
06-03 14:32:14.647 2563-2563/example.login W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5417)
06-03 14:32:14.647 2563-2563/example.login W/System.err: at java.lang.reflect.Method.invoke(Native Method)
06-03 14:32:14.647 2563-2563/example.login W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
06-03 14:32:14.647 2563-2563/example.login W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
I am new to JSON encoding. I read many threads regarding this problem, but still no clue on how to solve it. These are my php and java files.
Register.php:
<?php
$con = mysqli_connect("my_server", "my_user", "my_password", "my_database");
$username = $_POST["username"];
$password = $_POST["password"];
$statement = mysqli_prepare($con, "INSERT INTO user (username, password) VALUES (?, ?)");
mysqli_stmt_bind_param($statement, "ss", $username, $password);
mysqli_stmt_execute($statement);
$response = array();
$response["success"] = true;
echo json_encode($response);
?>
RegisterRequest.java:
package example.login;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import java.util.HashMap;
import java.util.Map;
public class RegisterRequest extends StringRequest {
private static final String REGISTER_REQUEST_URL = "http://www.vipanchith.byethost17.com/file/Register.php";
private Map<String, String> params;
public RegisterRequest(String username, String password, Response.Listener<String> listener) {
super(Method.POST, REGISTER_REQUEST_URL, listener, null);
params = new HashMap<>();
params.put("username", username);
params.put("password", password);
}
@Override
public Map<String, String> getParams() {
return params;
}
}
RegisterActivity.java:
package example.login;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;
public class RegisterActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
final EditText etUsername = (EditText) findViewById(R.id.etUsername);
final EditText etPassword = (EditText) findViewById(R.id.etPassword);
final Button bRegister = (Button) findViewById(R.id.bRegister);
assert bRegister != null;
bRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String username = etUsername.getText().toString();
final String password = etPassword.getText().toString();
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");
if (success) {
Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
RegisterActivity.this.startActivity(intent);
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
builder.setMessage("Registration Failed")
.setNegativeButton("Retry", null)
.create()
.show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
RegisterRequest registerRequest = new RegisterRequest(username, password, responseListener);
RequestQueue queue = Volley.newRequestQueue(RegisterActivity.this);
queue.add(registerRequest);
}
});
}
}
Any help would be appreciated. Thank you.