I am saving data from Android device to MySQL. My code works fine using Localhost, but when I try it using my hosted domain. It gives me and error.
Error returned
ERROR
The requested URL could not be retrieved
While trying to process the request:
POST /insert.php HTTP/1.1
Transfer-Encoding: chunked
Content-Type: application/x-www-form-urlencoded
Host: mydomain.com
Connection: Keep-Alive
User-Agent: Apache-HttpClient/UNAVAILABLE (java 1.4)
The following error was encountered:
Invalid Request
Java code inside AsyncTask
.
.
.
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("user_id", userId));
HttpPost httppost = new HttpPost("http://mydomain.com/insert.php");
HttpClient httpclient = new DefaultHttpClient();
UrlEncodedFormEntity urlEncodeFormEntity = new UrlEncodedFormEntity(nameValuePairs);
urlEncodeFormEntity.setChunked(true);
httppost.setEntity(urlEncodeFormEntity);
HttpResponse response = httpclient.execute(httppost);
.
.
.
Please help me solved this problem. Again it working using Localhost.
EDIT:
PHP Code: this is just to test if my device can reach the given URL
<?php
echo "Ive been reached.";
//Codes to connect DB and To Insert goes here
?>
I solved this problem by removing the line
urlEncodeFormEntity.setChunked(true);
try this code
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://exampple.com/insert-data.php");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("id", "1"));
try{
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String response = httpclient.execute(httppost, responseHandler);
String reverseString = response;
Log.d("Mishu Complain",reverseString);
} catch(Exception e){ // check error here }
to find anyerror in log you can put this code
catch (Exception anyError) {
Log.e("Mishu-Cn-Error", "exception",anyError);
}