在使用手机数据发送请求时在我的Android项目上收到404错误,但在连接到wifi时收到良好响应

时间:2017-02-27 17:37:12

标签: android android-studio http-status-code-404 okhttp3

请记住,我的所有权限都是正常的并且它不是库问题,因为我尝试过使用urlConnection和volley但仍然得到相同的错误。它是一个简单的json帖子到云托管的API     我还应该提一下,这个问题似乎只发生在不同的手机上,也是在执行时我得到了具有404响应状态的IOException。 这是我的代码,请帮助

 @Override
        protected Boolean doInBackground(Void... params) {
            // TODO: attempt authentication against a network service.
            Boolean respValue = false;

            try {

            MediaType JSON = MediaType.parse("application/json; charset=utf-8");
                Map<String, String> url_params = new HashMap<String, String>();
                url_params.put("appid", "123456");
                url_params.put("apppass", "1234567");
                url_params.put("username",mEmail);
                url_params.put("userpassword",mPassword);
                url_params.put("appserviceid","12345");
                JSONObject parameter = new JSONObject(url_params);

                OkHttpClient client = new OkHttpClient();
                RequestBody body = RequestBody.create(JSON, parameter.toString());

                Request request = new Request.Builder()
                        .url(getResources().getString(R.string.API_login))
                        .post(body)
                        .addHeader("content-type", "application/json; charset=utf-8")
                        .build();

                Response response = client.newCall(request).execute();


                if (!response.isSuccessful()) {
                    throw new IOException("Unexpected code " + response);
                } else {
                    Log.i("Server_Response", response.body().string());

                    String respd = (client.newCall(request).execute()).body().string();

                    JSONObject json_obj = new JSONObject(respd);
                    String state = json_obj.getString("state");
                    Response = Integer.valueOf(state);
                    MESSAGE = json_obj.getString("msg");

                    Log.i("State", state);
                    Log.i("Msg", MESSAGE);
                    respValue = true;
                }

            } catch (Exception e) {
                e.printStackTrace();
                Log.e("responseError", "Error");
                respValue = false;
            }

            return respValue;
        }

1 个答案:

答案 0 :(得分:0)

感谢大家的反馈,但我终于找到了问题,它与您从移动网络运营商处获得的连接设置有关,在我的情况下,我只是要求新的设置,一切正常。