2012-05-27 64 views
3

我有一個連接數據庫和Android應用程序的問題。我正在嘗試執行this教程。一切似乎都很好,但我沒有取得任何成功而不是錯誤。連接mysql數據庫和Android應用程序

有一個按鈕偵聽器,它可以在點擊時向PHP文件發送帖子並獲得結果。下面是它的代碼: -

ok.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>(); 
      postParameters.add(new BasicNameValuePair("username", un.getText().toString())); 
      postParameters.add(new BasicNameValuePair("password", pw.getText().toString())); 
      //String valid = "1"; 
      String response = null; 
      try { 
       response = CustomHttpClient.executeHttpPost("http://10.0.2.2/check.php", postParameters); 
       String res=response.toString(); 
       Log.d("res:", res); 

       // res = res.trim(); 
       res= res.replaceAll("\\s+","");        
       //error.setText(res); 

       if(res.equals("1")) 
        error.setText("Correct Username or Password"); 
       else 
        error.setText("Sorry!! Incorrect Username or Password"); 
      } catch (Exception e) { 
       un.setText(e.toString()); 
      } 

     } 
    }); 

這裏是HTTP POST方法: -

public static String executeHttpPost(String url, ArrayList<NameValuePair> postParameters) throws Exception { 
    BufferedReader in = null; 
    try { 
     HttpClient client = getHttpClient(); 
     HttpPost request = new HttpPost(url); 
     UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters); 
     request.setEntity(formEntity); 
     HttpResponse response = client.execute(request); 
     in = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); 

     StringBuffer sb = new StringBuffer(""); 
     String line = ""; 
     String NL = System.getProperty("line.separator"); 
     while ((line = in.readLine()) != null) { 
      sb.append(line + NL); 
     } 
     in.close(); 

     String result = sb.toString(); 
     Log.d("postMethodReturn", result); 
     return result; 
    } finally { 
     if (in != null) { 
      try { 
       in.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

的PHP代碼如下: -

<?php 
$un=$_POST['username']; 
$pw=$_POST['password']; 
//connect to the db 
$user = "xyz"; 
$pswd = "xyz"; 
$db = "mydb"; 
$host = "localhost"; 
$conn = mysql_connect($host, $user, $pswd); 
mysql_select_db($db); 
//run the query to search for the username and password the match 
$query = "SELECT * FROM mytable WHERE user = '$un' AND pass = '$pw'"; 
$result = mysql_query($query) or die("Unable to verify user because : " . mysql_error()); 
//this is where the actual verification happens 
if(mysql_num_rows($result) --> 0) 
echo 1; // for correct login response 
else 
echo 0; // for incorrect login response 
?> 

是否有任何錯誤該程序?我嘗試在活動代碼中記錄res(http響應)的中間值並導致execute post方法,但沒有記錄任何內容。嘗試將「localhost」更改爲「127.0.0.1」,並將其更改爲公開可用的虛擬主機,但所有數據庫環境均未成功。所有這些都在模擬器和公共主機上,也使用真實設備進行嘗試。從瀏覽器檢查時,服務器似乎正在運行。數據庫與值存在。所有運行的服務(apache,mysql)。

主要問題是沒有錯誤!任何建議發生了什麼問題? 找不到有相同問題的人。

+0

你能告訴打印結果嗎? –

+0

它總是給出輸出爲0(不正確的登錄響應),不幸的是沒有要求的日誌。 – gkris

+0

這告訴prblm是PHP功能不在android端...所以先檢查你的php連接 –

回答

1

問題是在PHP代碼中的-->。將其更改爲==>,一切正常!

相關問題