无法从Android手机发送截击请求

时间:2016-04-28 12:54:14

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

我使用Volley从我的Android应用程序发送带有参数的http发布请求到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开发很新,所以任何见解都会非常感激。

提前致谢。

1 个答案:

答案 0 :(得分:0)

对于遇到此问题的其他任何人,您都需要忘记标题重写,并设置自己的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;
            }
        }
    };
相关问题