2012-08-23 123 views
1

我正在将数据从Android设备保存到MySQL。我的代码使用Localhost工作正常,但是当我尝试使用我的托管域时。它给我和错误。PHP返回无效请求错误

返回错误

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代码里面的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); 
. 
. 
. 

请帮我解决了这个问题。它再次使用Localhost工作。

编辑:

PHP代码:这仅仅是一个测试,如果我的设备能达到给定的URL

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

解决了这个问题,你可以在这里提供insert.php代码和logcat的也? –

+0

@MishuCn我认为php代码没有必要发布,但正如你所要求的。我张贴我的PHP代码。任何帮助,不胜感激。请帮助我们。 – jayellos

+0

没有错误也没有在Logcat中发现异常,“请求的URL无法检索...”是HttpResonse的值。 – jayellos

回答

0

我通过删除线

urlEncodeFormEntity.setChunked(true); 
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 } 

找到anyerror在日志中你可以把这个代码

catch (Exception anyError) { 
      Log.e("Mishu-Cn-Error", "exception",anyError); 

     } 
+0

我试过这个代码,但它给了我一个例外。 – jayellos

+0

W/System.err的(5751):org.apache.http.client.HttpResponseException:长度必需 W/System.err的(5751):\t在org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler。的java:71) W/System.err的(5751):\t在org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:59) W/System.err的(5751):\t在有机apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:657) – jayellos

+0

奇怪!!!这段代码适合我,你能提供完整的日志错误吗? –