volley无法将数据发送到服务器

时间:2018-04-02 07:34:21

标签: android android-volley

我正在使用此代码

StringRequest stringRequest = new StringRequest(Request.Method.POST,
            uploadUrl,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                        Log.d("sfhyoutubeghhj",response);


                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                   //loading.dismiss();
                }
            }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> prams = new HashMap<>();
            prams.put("aaaa", "1111");
            prams.put("bbbb", "2222");
            return prams;
        }
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("Content-Type", "application/json; charset=utf-8");
            headers.put("User-agent", "My useragent");
            return headers;
        }
    };
    RequestQueue requestQueuea = Volley.newRequestQueue(this);
    requestQueuea.add(stringRequest);

请求进入服务器并且响应也即将到来,但它不会在GET或POST方法中从android发送任何变量

我正在使用

的print_r($ _ REQUEST);

在PHP结束

1 个答案:

答案 0 :(得分:0)

我认为只有“请求队列”存在问题

使用此代码@ Ali Azhar's Answer

正常工作
StringRequest sr = new StringRequest(Request.Method.POST, url , new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        Log.d(TAG, response.toString());
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        VolleyLog.d(TAG, "Error: " + error.getMessage());
        Log.d(TAG, ""+error.getMessage()+","+error.toString());
    }
}){
    @Override
    protected Map<String,String> getParams(){
        Map<String, String> params = new HashMap<String, String>();
        params.put("id", "28");
        params.put("value", "1");

        return params;
    }

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

    AppController.getInstance().addToRequestQueue(sr);