My Application makes calls to our server through a couple different Asynctask‘s, it pulls data twice and finally sends a JSONArray through POST.
It had been working properly but recently stopped, no changes were made. Here is my AsyncTask to POST a JSONArray with only the necessary information:
UPDATED 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;
}
And here is how I am receiving the code, PHP-Server side:
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
}
My log is writing perfectly fine if the I access the url through the browser, however nothing is written when running the application. I have tested all output in the AsyncTask and everything seems to work properly.
LogCat output of realjObject (Verified on http://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
}
Edit
I just started thinking that my require("../logger/logging.class.php"); might conflict with $json = file_get_contents('php://input');, but I just commented out all of my logging and require lines and it still isn’t doing anything.
Device has internet connection and has an established connection with this MySQL database or I wouldn’t get a point of being able to submit the JSONArray.
Test Device: Samsung Galaxy Tab 10.1 Gen 1 running Android 4.0.1
Edit 2
I fixed the InputStream to give me a response and got the following 404 & 406 response:
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>
What could suddenly cause this?
EDIT 3 – Recent steps I took to troubleshoot:
- I have contacted our web host and they are telling me everything on their end is fine.
The server logs (error_log) don’t show any errors - I have slightly changed the above Java to show my response return as well as changing to using
URLclass rather thenStringfor thenew HttpPost(url.toURI());. LogCat shows the correct domain, copied and pasted into browser, works fine. - I have changed the URL to another file with the
/db/completeSurvey.phpcontents placed into the new file, same 404/406 response as above. - I created another new php file only containing an
fwriteto log page call, same 404/406 response as above. - I directed to our root domain, “http://www.myDomain.com”, same 404/406 response as above
I’m starting to think that it has to be either the device or my code.
I contacted our Web Host (Surpass Hosting) and gave them the following message:
At first they said that I needed to write more logging code to determine what the cause was because it wasn’t on their end.
Eventually I finally got sent to Tech Support Level II, the guy there gave me the answer immediately:
Solution: