我正在将数据从Android设备保存到MySQL。我的代码使用Localhost运行良好,但当我尝试使用托管域时。它给了我错误。
返回错误
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
AsyncTask内的Java代码
.
.
.
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);
.
.
.
请帮我解决这个问题。它再次使用Localhost工作。
编辑:
PHP代码:这只是为了测试我的设备是否可以访问给定的URL
<?php
echo "Ive been reached.";
//Codes to connect DB and To Insert goes here
?>
尝试此代码
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 }
要在日志中找到任何错误,您可以将此代码放入
catch (Exception anyError) {
Log.e("Mishu-Cn-Error", "exception",anyError);
}
我通过删除行解决了这个问题
urlEncodeFormEntity.setChunked(true);