0
請記住,我的所有權限都可以,並且它不是庫問題,因爲我嘗試過使用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