2012-08-23 157 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

奇怪!!!這段代碼適合我,你能提供完整的日誌錯誤嗎? –