Reputation: 113
I am trying to insert new user in phpMyAdmin database when users populate textboxes on android device. For that i write php code and I test it on POSTMAN and it's works but when i test on android real device i got AuthFailureError! In POSTMAN i set Authorization to "No Auth". This is my code in android studio:
public class Registration extends AppCompatActivity {
private EditText name,surname,email,password,adress;
private Button btnLog;
private RequestQueue requestQueue;
private static final String URL="http://192.168.*.*/Log/LogUser.php";
private StringRequest request;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registruj_se);
name=(EditText) findViewById(R.id.txtName);
surname=(EditText)findViewById(R.id.txtSurname);
email=(EditText)findViewById(R.id.txtEmail);
password=(EditText)findViewById(R.id.txtPassword);
adress=(EditText)findViewById(R.id.txtAdress);
btnLog=(Button)findViewById(R.id.btnLog);
requestQueue= Volley.newRequestQueue(this);
btnLog.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
request=new StringRequest(Request.Method.POST,URL,new Response.Listener<String>(){
@Override
public void onResponse(String response) {
try
{
JSONObject jsonObject=new JSONObject(response);
if(jsonObject.names().get(0).equals("success")){
Toast.makeText(getApplicationContext(),"success:"+jsonObject.getString("success"),Toast.LENGTH_SHORT).show();
startActivity(new Intent(getApplicationContext(),Welcome.class));
}else{ Toast.makeText(getApplicationContext(),"error:"+jsonObject.getString("error"),Toast.LENGTH_SHORT).show();}
}
catch (JSONException e){e.printStackTrace();}
}
},new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error) {
if (error instanceof NetworkError) {
Toast.makeText(getApplicationContext(),"Cannot connect to Internet...Please check your connection!",Toast.LENGTH_SHORT).show();
} else if (error instanceof ServerError) {
Toast.makeText(getApplicationContext(),"The server could not be found. Please try again after some time!!",Toast.LENGTH_SHORT).show();
} else if (error instanceof AuthFailureError) {
Toast.makeText(getApplicationContext(),"AuthFailureError",Toast.LENGTH_SHORT).show();
} else if (error instanceof ParseError) {
Toast.makeText(getApplicationContext(),"Parsing error! Please try again after some time!!",Toast.LENGTH_SHORT).show();
} else if (error instanceof NoConnectionError) {
Toast.makeText(getApplicationContext(),"NoConnectionError",Toast.LENGTH_SHORT).show();
} else if (error instanceof TimeoutError) {
Toast.makeText(getApplicationContext(),"Connection TimeOut! Please check your internet connection.",Toast.LENGTH_SHORT).show();
}
}
})
{
@Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String,String>hashMap=new HashMap<String, String>();
hashMap.put("name",name.getText().toString());
hashMap.put("surname",surname.getText().toString());
hashMap.put("email",email.getText().toString());
hashMap.put("password",password.getText().toString());
hashMap.put("adress",adress.getText().toString());
return hashMap;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
// do not add anything here
return headers;
}
@Override
public String getBodyContentType() {
return "application/json";
}
};
requestQueue.add(request);
}
});
I appreciate any help!
Upvotes: 1
Views: 7720
Reputation: 3692
Try this:
In getBodyContentType()
method return "application/x-www-form-urlencoded"
instead of "application/json"
Code
@Override
public String getBodyContentType() {
return "application/x-www-form-urlencoded";
}
Upvotes: 1