在使用手机数据发送请求时收到404错误,但在连接到wifi时收到良好响应
问题描述:
请记住,我的所有权限都可以,并且它不是库问题,因为我尝试过使用urlConnection和凌空抽象但仍然得到相同的错误。它是一个简单的json发布到云托管的API 而且我还应该提到,这个问题似乎只发生在不同的手机上,也是在执行时得到具有404响应状态的IOException。 这里是我的代码,请协助在使用手机数据发送请求时收到404错误,但在连接到wifi时收到良好响应
@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;
}
答
感谢大家的反馈,但我终于想通了这个问题,它是与你从你的移动网络运营商,得到了连通性设置,这在我来说,我只是请求新的设置,一切正常。
检查您的网址是否正确。首先测试你的网络服务,如果它在Android中实施之前工作 – Sharj
URL是好的@Sharj,因为当我关掉手机上的数据并连接到无线网络时它能够发送请求没有任何问题 –
尝试在手机中使用网络浏览器访问该URL或该Web服务器,并检查结果 – BNK