1

我正在将数据从 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);
.
.
.

请帮我解决这个问题。它再次使用本地主机工作。

编辑:

PHP 代码:这只是为了测试我的设备是否可以访问给定的 URL

<?php
    echo "Ive been reached.";
    //Codes to connect DB and To Insert goes here
?>
4

2 回答 2

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);

        }
于 2012-08-23T10:17:03.183 回答
0

我通过删除线解决了这个问题

urlEncodeFormEntity.setChunked(true);
于 2012-10-25T09:44:29.653 回答