我的應用程序通過幾個不同的Asynctask
打電話給我們的服務器,它將數據提取兩次,最後通過POST發送JSONArray
。應用程序突然停止發佈到數據庫,返回狀態碼406
它一直在正常工作,但最近停止了,沒有做任何改變。這是我AsyncTask
到POST
一個JSONArray
,只有必要的信息:
更新的Java
String inString;
JSONObject realjObject;
JSONArray compJArray;
int completeCount = 0;
URL url;
@Override
protected Void doInBackground(JSONArray... jsonArray) {
if(jsonArray.length > 0) {
compJArray = jsonArray[0];
}
completeCount = compJArray.length();
//String url_select = "http://www.myDomain.com/db/completedSurvey.php";
HttpResponse response;
for (int i = 0; i < completeCount; i++) {
try {
realjObject = compJArray.getJSONObject(i);
Log.i("realjObject", realjObject.toString());
try {
url = new URL("http://www.myDomain.com/db/completedSurvey.php");
HttpPost httpPost = new HttpPost(url.toURI());
HttpClient httpClient = new DefaultHttpClient();
httpPost.setEntity(new StringEntity(realjObject.toString(), "UTF-8"));
httpPost.setHeader(HTTP.DEFAULT_CONTENT_TYPE, "application/json");
response = (HttpResponse) httpClient.execute(httpPost);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
StringBuilder builder = new StringBuilder();
for (String line = null; (line = reader.readLine()) != null;) {
builder.append(line).append("\n");
}
inString = builder.toString();
Log.w("RESPONSE", inString);
} catch (UnsupportedEncodingException e1) { Log.e("UnsupportedEncodingException", e1.toString());
} catch (ClientProtocolException e2) { Log.e("ClientProtocolException", e2.toString());
} catch (IllegalStateException e3) { Log.e("IllegalStateException", e3.toString());
} catch (IOException e4) { Log.e("IOException", e4.toString());
} catch (URISyntaxException e) {Log.e("URISyntaxException", "" + e.getMessage());
}
} catch (JSONException e) { Log.e("doInBackground", "JSONException: " + e.getMessage());
}
}
return null;
}
這裏是我如何收到代碼,PHP的服務器端:
require("../logger/logging.class.php");
$genLog->lwrite("Is Android accessing this url?");
$json = file_get_contents('php://input');
$genLog->lwrite($json);
if ($json) {
// Parse JSONArray, was working properly
}
如果我通過瀏覽器訪問網址,我的日誌寫得非常好,但是沒有什麼是w在運行應用程序時運行。我已測試AsyncTask
中的所有輸出,並且一切看起來都正常。輸出
LogCat
realjObject
(覈實www.jsonlint.com):
{
"Notes": "null",
"SurveyPhoto": "null",
"id": 2,
"siteNotes": "",
"loc_id": 1,
"sign_type": "FREESTANDING SIGN",
"Lighting": 0,
"AdditionalService": 0,
"view_id": 2,
"survey_order": 1
}
編輯
我剛開始以爲我require("../logger/logging.class.php");
可能產生衝突$json = file_get_contents('php://input');
,但我只是表示排除了我所有的日誌記錄並需要線路,但它仍然沒有做任何事情。
設備有互聯網連接,並與此MySQL數據庫建立連接,或者我無法提交JSONArray
。
測試設備:三星Galaxy Tab 10.1創1運行Android 4.0.1
編輯2
我固定InputStream
給我一個迴應,並得到了以下404 & 406迴應:
12-05 09:31:39.345: W/RESPONSE(19023): <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
12-05 09:31:39.345: W/RESPONSE(19023): <html><head>
12-05 09:31:39.345: W/RESPONSE(19023): <title>406 Not Acceptable</title>
12-05 09:31:39.345: W/RESPONSE(19023): </head><body>
12-05 09:31:39.345: W/RESPONSE(19023): <h1>Not Acceptable</h1>
12-05 09:31:39.345: W/RESPONSE(19023): <p>An appropriate representation of the requested resource /db/completeSurvey.php could not be found on this server.</p>
12-05 09:31:39.345: W/RESPONSE(19023): <p>Additionally, a 404 Not Found
12-05 09:31:39.345: W/RESPONSE(19023): error was encountered while trying to use an ErrorDocument to handle the request.</p>
12-05 09:31:39.345: W/RESPONSE(19023): <hr>
12-05 09:31:39.345: W/RESPONSE(19023): <address>Apache Server at www.myDomain.com Port 80</address>
12-05 09:31:39.345: W/RESPONSE(19023): </body></html>
什麼會突然造成這種情況?
編輯3 - 最近採取的步驟我把排查:
- 我已經聯繫了我們的虛擬主機,他們都告訴我一切關於他們到底是罰款。 服務器日誌(error_log中)不顯示任何錯誤
- 我稍微改變了上面的Java顯示我返回響應以及改變使用
URL
類而不是String
爲new HttpPost(url.toURI());
。 LogCat顯示正確的域,複製並粘貼到瀏覽器中,工作正常。 - 我已將網址更改爲另一個文件,其中
/db/completeSurvey.php
內容放置在新文件中,與上面相同的404/406響應。 - 我創建了另一個新的php文件,其中只包含一個
fwrite
來記錄頁面調用,與上面相同的404/406響應。 - 我涉及我們的根域,「http://www.myDomain.com」,相同四百零六分之四百零四響應如上
我開始認爲它必須是在設備或者我的代碼。
我現在沒有做任何事情,最終我會得到一個點,我要返回一個成功或不成功的響應代碼。我的AsyncTask沒有從工作變成不工作。這不是問題。 – jnthnjns
仍試圖瞭解問題。什麼是'AsyncTask'不工作?請顯示代碼以存儲不工作的數據? –
商店數據非常長,並且自工作以來一直沒有改變,出於某種原因,我甚至沒有達到這一點。我仍然可以發佈它,我只是不想阻止人們因長期問題而提供幫助。 – jnthnjns