I am trying to pass JSONObject with Httppost but getting 406 Error (SC_NOT_ACCEPTABLE).
I have tried different solutions proposed in SO like adding header, using StringEntity, but nothing is working for me.
Can anybody please help me?
ASYNCTASK CLASS: doInBackground method:
String URL = "www.myURL.com/myfile.php";
String key = "mykey";
mylibmandbhandler db = new mylibmandbhandler(ImExActivity.this);
JSONArray jason = db.exportDb(); // exportDb() returns JSONArray
db.close();
HttpParams httpParameters = new BasicHttpParams();
int timeoutConnection = 10000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
HttpConnectionParams.setSoTimeout(httpParameters, timeoutConnection);
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(URL);
HttpResponse response = null;
try {
JSONObject jsonObject = new JSONObject();
jsonObject.put("key", key);
jsonObject.put("output", jason);
StringEntity output = new StringEntity(jsonObject.toString());
httppost.setEntity(output);
httppost.addHeader("Accept", "application/json");
httppost.addHeader("Content-type", "application/json");
response = httpclient.execute(httppost);
if(response.getStatusLine().getStatusCode()!=200){
return " E511: "+response.getStatusLine().getStatusCode()+": "+response.getStatusLine().toString();
}
HttpEntity entity = response.getEntity();
String responseBody = EntityUtils.toString(entity);
return responseBody;
} catch (Exception e) {
return "E509: Error in connection";
}
myfile.php
if(!isset($_POST['key'])){
echo "Denied.";
exit();
}else{
echo $post['output'];
exit();
}
LOGCAT: There is no error, but if I print responseBody, I get the following in logcat. However, there is no problem with the existence of the file, as if I send normal string (not JSON), everything works fine.
12-07 04:58:33.090: D/aaaa(1243): E511: 406: HTTP/1.1 406 Not Acceptable
12-07 04:58:33.130: D/aaaa(1243): <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
12-07 04:58:33.130: D/aaaa(1243): <html><head>
12-07 04:58:33.130: D/aaaa(1243): <title>406 Not Acceptable</title>
12-07 04:58:33.130: D/aaaa(1243): </head><body>
12-07 04:58:33.130: D/aaaa(1243): <h1>Not Acceptable</h1>
12-07 04:58:33.130: D/aaaa(1243): <p>An appropriate representation of the requested resource /myfile.php could not be found on this server.</p>
12-07 04:58:33.130: D/aaaa(1243): <p>Additionally, a 404 Not Found
12-07 04:58:33.130: D/aaaa(1243): error was encountered while trying to use an ErrorDocument to handle the request.</p>
12-07 04:58:33.130: D/aaaa(1243): <hr>
12-07 04:58:33.130: D/aaaa(1243): <address>Apache Server at www.myURL.com Port 80</address>
12-07 04:58:33.130: D/aaaa(1243): </body></html>