【问题标题】:volley unable to send data to server凌空无法将数据发送到服务器
【发布时间】:2018-04-02 07:34:20
【问题描述】:

我正在使用此代码

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 结束时

【问题讨论】:

  • 你检查过 key 是 aaaa & bbbb 吗?
  • print_r($_REQUEST);它将显示所有可能的值和键
  • 检查 getParams() 方法中的 keys,如果没有则正确设置。

标签: android android-volley


【解决方案1】:

我认为只有请求队列有问题

使用此代码可以正常工作@ 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);

【讨论】:

    猜你喜欢
    • 2014-03-22
    • 2017-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多