1

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.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98

1 Answers1

1

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.

Community
  • 1
  • 1
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
  • but maybe i didn't place it at the right spot. I put it after my hashmap who help me create the header. Or maybe it's because i just ctrl+c ctrl+v all. I need to change something in your code? – pierre-charles Mangin May 02 '16 at 14:50
  • Nope I guess. You need to put this inside your `SigninRequest`. – Reaz Murshed May 02 '16 at 15:28
  • what i saw by testing with log is that the onResponse function doesn't even start so i'm really stuck! Something missing but i can't tell what – pierre-charles Mangin May 02 '16 at 15:47
  • my request is always http://192.168.1.25:80/tutorial2/user_control.php. How can i put email and password in it? with the getHeader? – pierre-charles Mangin May 02 '16 at 15:48
  • Reaz? you still here bro? – pierre-charles Mangin May 03 '16 at 04:53
  • Yes I am here. What's now? :) – Reaz Murshed May 03 '16 at 04:57
  • haha sorry to bother you but i don't really understand what's is the role of the getHeader – pierre-charles Mangin May 03 '16 at 05:54
  • No problem. You are setting some specific things that you want to put in your Http request header in `getHeader` method. Like yo've set the `Content-Type` of your request to `form-data` because your API accepts the body in `form-data` format. If your API was expecting to have a JSON body you had to set `application/json` as your `Content-Type`. You can pass more additional parameters in your header too. – Reaz Murshed May 03 '16 at 06:32
  • but i can't understand why error403 Myserver don't have a problem neither my php code; so it must be my java code. I tryed to put **application/json** on **Content-Type** but it doesn't change anything. With Log i saw that it does: 1-getParams 2-getHeaders 3-send me the error403 4-onErrorResponse – pierre-charles Mangin May 03 '16 at 07:33
  • May be your header is not updating. See the answer attached with my answer. That might help you – Reaz Murshed May 03 '16 at 08:34
  • @ReazMurshed how can i do the same for my problem here is my question : https://stackoverflow.com/questions/67874965/unexpected-response-code-403-but-work-fine-in-browser – Tehleel Mir Jun 08 '21 at 01:58