pierre-charles Mangin pierre-charles Mangin - 7 months ago 99
PHP Question

Android - Volley Error 403 won't be solved

I'm actually trying to create a login activity using volley but i can't understand why it always give me the

error 403 : unexpected response
.

I know that my php code is as good as my server because i tryed it with Postman and it work just fine.
(Internet permission is activated)

public class LoginActivity extends AppCompatActivity {
private EditText email,password;
private Button sign_in_register;
private RequestQueue requestQueue;
private static final String URL = "http://192.168.1.25:80/tutorial2/user_control.php";
private StringRequest request;

@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);

email = (EditText) findViewById(R.id.email);
password = (EditText) findViewById(R.id.password);
sign_in_register = (Button) findViewById(R.id.sign_in_register);

requestQueue = Volley.newRequestQueue(this);

sign_in_register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

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) {
NetworkResponse response = error.networkResponse;
if (error instanceof ServerError && response != null) {
try {
String res = new String(response.data,
HttpHeaderParser.parseCharset(response.headers, "utf-8"));
// Now you can use any deserializer to make sense of data
JSONObject obj = new JSONObject(res);
} catch (UnsupportedEncodingException e1) {
// Couldn't properly decode data to string
e1.printStackTrace();
} catch (JSONException e2) {
// returned data is not JSONObject?
e2.printStackTrace();
}
}

}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String,String> hashMap = new HashMap<String, String>();
hashMap.put("email",email.getText().toString());
hashMap.put("password",password.getText().toString());

return hashMap;
}
};

requestQueue.add(request);
}
});
}


Here is the screenshot of Postman.

Answer

You need to set Content-Type in you request header like the example below. Just @Override getHeaders method.

@Override
public Map<String, String> getHeaders() throws AuthFailureError
{
    Map<String, String> headers = new HashMap<String, String>();
    headers.put("Content-Type", "application/form-data");
    return headers;
}

In case of header not updating, see this answer here.