我的应用程序通过几个不同的调用我们的服务器Asynctask
,它两次提取数据,最后JSONArray
通过 POST 发送一个。
它一直正常工作,但最近停止了,没有进行任何更改。这是我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-Server 端:
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
}
如果我通过浏览器访问 url,我的日志写得很好,但是在运行应用程序时没有写入任何内容。我已经测试了所有输出,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');
,但我只是注释掉了我所有的日志记录和要求行,它仍然没有做任何事情。
设备具有 Internet 连接并且已与此 MySQL 数据库建立连接,否则我将无法提交JSONArray
.
测试设备:运行 Android 4.0.1 的三星 Galaxy Tab 10.1 Gen 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 显示正确的域,复制并粘贴到浏览器中,工作正常。 - 我已将 URL 更改为另一个文件,并将
/db/completeSurvey.php
内容放入新文件中,与上述相同的 404/406 响应。 - 我创建了另一个新的 php 文件,只包含一个
fwrite
日志页面调用,与上面相同的 404/406 响应。 - 我定向到我们的根域“http://www.myDomain.com”,与上述相同的 404/406 响应
我开始认为它必须是设备或我的代码。