2014-10-08 52 views
0

我有一個錯誤,我似乎無法弄清楚爲什麼。我試圖發佈數據到一個PHP文件,並陷入一個捕獲錯誤。我收到的敬酒是「數據未發送」,但我收到了「數據已準備好」之前敬酒。任何人都可以看看並幫助我確定我的問題是什麼?使用android發佈數據時發生錯誤

Android的方法:

public void insertToDB(String data){ 
     try { 
      HttpClient client=new DefaultHttpClient(); 
      HttpPost getMethod=new HttpPost("http://shawnc.webuda.com/upload.php"); 
      ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
      nameValuePairs.add(new BasicNameValuePair("RPM",data)); 
      getMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8)); 
      String toastText = "Data was prepared"; 
      Toast.makeText(getApplicationContext(), toastText, Toast.LENGTH_SHORT).show(); 
      try { 
       client.execute(getMethod); 
       toastText = "Data has been sent: " + data; 
       Toast.makeText(getApplicationContext(), toastText, Toast.LENGTH_SHORT).show(); 
      }catch(Exception e){ 
       toastText = "Data was not sent"; 
       Toast.makeText(getApplicationContext(), toastText, Toast.LENGTH_SHORT).show(); 
      } 
     }catch(Exception e){ 
      String toastText = "Data could not be prepared"; 
      Toast.makeText(getApplicationContext(), toastText, Toast.LENGTH_SHORT).show(); 
     } 
    } 

和我的PHP文件:

<? 
$hostname_host ="MYSQLxxxxx.Smarterasp.net"; 
$database_host ="db_xxxxx_db"; 
$username_host ="9xxxxx_db"; 
$password_host ="password"; 

$server= mysql_connect($hostname_localhost,$username_localhost,$password_localhost) 
or 
trigger_error(mysql_error(),E_USER_ERROR); 

mysql_select_db("db_xxxxx_db"); 
$sql=mysql_query("INSERT INTO driverdata (RPM)VALUES('".$_REQUEST['RPM']."')"); 

$r=mysql_query($sql); 
if(!$r) 
echo "Error in query: ".mysql_error(); 
mysql_close(); 
?> 
+3

檢查異常中的錯誤消息。最有可能包含有助於您解決問題的有用信息。這可能是網絡問題,服務器配置,誰知道。看看例外。異常中的堆棧跟蹤和消息是什麼? – 2014-10-08 04:27:06

+0

我怎樣才能看到堆棧跟蹤。我從手機上運行它,因爲它連接到我車裏的obdII傳感器,所以我不能使用模擬器? – Shawn 2014-10-08 04:28:44

+1

它可能是這行'$ server = mysql_connect($ hostname_localhost,$ username_localhost,$ password_localhost) 或 trigger_error(mysql_error(),E_USER_ERROR);' - 它可能需要一個'if/else' - 嘗試'if server = mysql_connect($ hostname_localhost,$ username_localhost,$ password_localhost)) else {trigger_error(mysql_error(),E_USER_ERROR);}' - 請參閱http://php.net/manual/en/function.trigger-error.php再舉幾個例子。 – 2014-10-08 04:33:24

回答

1

當你不知道哪裏是問題的嘗試

<?php 
echo "OK"; 
?> 

取代PHP腳本,並嘗試閱讀來自PHP的響應

try { 
    HttpResponse response = client.execute(getMethod); 
    toastText = "Data has been sent: " + data; 

當你得到響應確定問題在PHP中。

當我使用httppost我使用此代碼來獲得響應。當我有迴應http請求是好的。

 HttpClient client = new DefaultHttpClient(httpParameters); 
     client.getParams().setParameter("http.protocol.version", HttpVersion.HTTP_1_1); 
     client.getParams().setParameter("http.socket.timeout", 2000); 
     client.getParams().setParameter("http.protocol.content-charset", HTTP.UTF_8); 
     httpParameters.setBooleanParameter("http.protocol.expect-continue", false); 
     HttpPost request = new HttpPost("http://xxxxxx.com/upload.php"); 
     request.getParams().setParameter("http.socket.timeout", 5000); 

     List<NameValuePair> postParameters = new ArrayList<NameValuePair>(); 
     postParameters.add(new BasicNameValuePair("RPM",data));    

     try { 
     UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters, HTTP.UTF_8); 
     request.setEntity(formEntity); 

     HttpResponse response = client.execute(request); 
     //do with response