我正在将数据从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代码:这只是为了测试我的设备是否可以访问指定的网址
<?php
echo "Ive been reached.";
//Codes to connect DB and To Insert goes here
?>
答案 0 :(得分:0)
试试这段代码
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);
}
答案 1 :(得分:0)
我通过删除行
解决了这个问题urlEncodeFormEntity.setChunked(true);