【问题标题】:Can't send volley post request from android phone无法从安卓手机发送凌空发帖请求
【发布时间】:2016-08-23 06:09:10
【问题描述】:

我正在使用 Volley 将带有参数的 http post 请求从我的 android 应用程序发送到在 http://192.168.1.4:3000/battery_signal_report 中运行的本地服务器 我很确定服务器运行正常(我用 Postman 成功检查过)。

另外,我使用 ip 10.0.2.2 通过 Android Studio 的模拟器成功发送了请求

为了让它工作,我使用了各种请求实现,包括 JsonObjectRequest、StringRequest 和此处描述的自定义请求:Volley JsonObjectRequest Post request not working

另外,我在某处读到 Volley 发布请求的请求标头存在一些问题,因此我尝试以不同的方式覆盖它。

没有任何作用。每次使用空 VolleyError 输入调用 onErrorResponse。

我对 android 开发还很陌生,因此非常感谢任何见解。

提前致谢。

【问题讨论】:

  • 你的错误信息的内容是什么?
  • 我没有收到任何错误消息 .. 正如我所说的 - 错误处理程序回调 onErrorResponse(VolleyError error) 正在以 error = null 触发
  • 您解决了这个问题吗?如果不是发布您请求代码块...查找问题并检查清单中给出的互联网权限

标签: android-studio http-post android-volley


【解决方案1】:

对于遇到此问题的其他人,您需要忘记标题覆盖并设置自己的 getBodyContentType() 和 getBody() 方法。遵循这个模式:

StringRequest stringRequest = new StringRequest(Request.Method.POST, url, successListener, errorListener) {
        @Override
        public String getBodyContentType() {
            return "application/json; charset=utf-8";//set here instead
        }

        @Override
        public byte[] getBody() {
            try {
                Map<String, String> params = yourObject.getMappedParams();
                JSONObject json = new JSONObject(params);
                String requestBody = json.toString();
                return requestBody == null ? null : requestBody.getBytes("utf-8");
            } catch (UnsupportedEncodingException uee) {
                return null;
            }
        }
    };

【讨论】:

    猜你喜欢
    • 2017-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多