Android - Volley Error 403将无法解决

时间:2016-05-02 11:58:14

标签: php android json android-volley

我实际上是在尝试使用排球创建登录活动,但我无法理解为什么它总是给我error 403 : unexpected response

我知道我的PHP代码和我的服务器一样好,因为我用Postman试了它,它运行得很好。 (已激活Internet权限)

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);
        }
    });
}

以下是screenshot of Postman.

1 个答案:

答案 0 :(得分:0)

您需要在请求标头中设置Content-Type,如下例所示。只需@Override getHeaders方法。

@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;
}

如果标题未更新,请参阅此answer here