I have a php and android studio files that should register a user when the correct details are input, the database and php code seem to work fine when using Postman and this adds the user correctly to the database.
[php]<?php
require_once ‘…/includes/DbOperations.php’;
$response = array();
if($_SERVER[‘REQUEST_METHOD’]==‘POST’){
if(
isset($_POST[‘username’]) and
isset($_POST[‘email’]) and
isset($_POST[‘password’]))
{
//operate the data further
$db = new DbOperations();
$result = $db->createUser( $_POST['username'],
$_POST['password'],
$_POST['email']
);
if($result == 1){
$response['error'] = false;
$response['message'] = "User registered successfully";
}elseif($result == 2){
$response['error'] = true;
$response['message'] = "Some error occurred please try again";
}elseif($result == 0){
$response['error'] = true;
$response['message'] = "It seems you are already registered, please choose a different email and username";
}
}else{
$response['error'] = true;
$response['message'] = "Required fields are missing";
}
}else{
$response[‘error’] = true;
$response[‘message’] = “Invalid Request”;
}
echo json_encode($response);[/php]
but when emulating using my .java file
[code]package com.example.androidantitheft;
import android.app.ProgressDialog;
import android.provider.SyncStateContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText editTextUsername, editTextEmail, editTextPassword;
private Button buttonRegister;
private ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextUsername = (EditText) findViewById(R.id.editTextUsername);
editTextEmail = (EditText) findViewById(R.id.editTextEmail);
editTextPassword = (EditText) findViewById(R.id.editTextPassword);
buttonRegister = (Button) findViewById(R.id.buttonRegister);
progressDialog = new ProgressDialog(this);
buttonRegister.setOnClickListener(this);
}
private void registerUser(){
final String email = editTextEmail.getText().toString().trim();
final String username = editTextUsername.getText().toString().trim();
final String password = editTextPassword.getText().toString().trim();
progressDialog.setMessage("Please Wait Whilst We Register You...");
progressDialog.show();
StringRequest stringRequest = new StringRequest(Request.Method.POST,
Contstants.URL_REGISTER,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
progressDialog.dismiss();
try {
JSONObject jsonObject = new JSONObject(response);
Toast.makeText(getApplicationContext(), jsonObject.getInt("message"), Toast.LENGTH_LONG).show();
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progressDialog.hide();
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("username", username);
params.put("email", email);
params.put("password", password);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
@Override
public void onClick(View v) {
if (v == buttonRegister)
registerUser();
}
}
[/code]
The notification should read one of the three error messages within the .php file but it just comes up as a blank notification (shown below) and does not register the user?