我的应用程序通过几个不同的Asynctask
来调用我们的服务器,它会两次提取数据,最后通过POST发送JSONArray
。
它一直运作正常,但最近停止了,没有进行任何更改。这是我的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');
发生冲突,但我只是注释掉了所有的日志记录并要求行,但它仍然没有做任何事情。
设备已连接互联网并与此MySQL数据库建立了连接,否则我无法提交JSONArray
。
测试设备:运行Android 4.0.1的三星Galaxy Tab 10.1 Gen 1
修改2
我修复了InputStream
给我一个回复并获得了以下404&amp; 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 - 我最近采取的排查步骤:
URL
类而不是String
用于new HttpPost(url.toURI());
。 LogCat显示正确的域名,复制并粘贴到浏览器中,工作正常。/db/completeSurvey.php
内容放入新文件中,与上述相同的404/406响应。fwrite
日志页面调用,与上面相同的404/406响应。我开始认为它必须是设备或我的代码。
答案 0 :(得分:1)
你想用这条线inString = in.toString();
做什么?你在toString()
上呼叫InputStream
它不会返回响应数据。
尝试以下代码来阅读json:
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();
答案 1 :(得分:0)
我联系了我们的网站主机(Surpass Hosting)并给了他们以下信息:
406不可接受
在此服务器上找不到所请求资源/db/completedSurvey.php的适当表示。
此外,尝试使用ErrorDocument处理请求时遇到404 Not Found错误。
Apache服务器,网址为www.myDomain.com端口80
起初他们说我需要编写更多的日志代码来确定原因是什么,因为它不在他们的最后。
最终我终于被送到技术支持二级,那个人立即给了我答案:
解决方案:
Surpass Tech:
“这是因为脚本与mod_security中的规则匹配。我只禁用了该文件的规则。”