2014-01-26 45 views
0

我想從本地訪問數據MySQL數據庫。我使用PHP的XAMP。首先我創建了一個index.php文件,它保存在C:\ xampp \ htdocs \ sample \ index.php.my中,android代碼保存在C:\ Documents and Settings \ SUHAIL \ workspace \ PHPMYSQL中。沒有訪問來自數據庫的模擬器數據(沒有編譯錯誤)。我的完整代碼如下。ANDROID + MYSQL + PHP數據訪問

的index.php(DB名稱:臨時 表名:表1) 在這裏輸入的代碼

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
</head> 
<body> 
<?php 
$con=mysqli_connect("localhost","root","","temp"); 
if (mysqli_connect_errno($con)) 
{ 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 
$username = $_GET['username']; 
$password = $_GET['password']; 
$result = mysqli_query($con,"SELECT roll FROM table1 where username='$username' and password='$password'"); 
$row = mysqli_fetch_array($result); 
$data = $row[0]; 
if($data){ 
echo $data; 
} 
mysqli_close($con); 
?> 
</body> 
</html> 
--------------------------------------------------------------------- 
SignActyivity.java 
--------------------------------------------------------------------- 

package com.example.phpmysql; 

import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.io.OutputStreamWriter; 
import java.net.URI; 
import java.net.URL; 
import java.net.URLConnection; 
import java.net.URLEncoder; 

import org.apache.http.HttpResponse; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.impl.client.DefaultHttpClient; 

import android.content.Context; 
import android.os.AsyncTask; 
import android.widget.TextView; 

public class SigninActivity extends AsyncTask<String,Void,String>{ 

    private TextView statusField,roleField; 

    public Context context; 
    private int byGetOrPost = 0; 
    //flag 0 means get and 1 means post.(By default it is get.) 
    public SigninActivity(Context context,TextView statusField, 
    TextView roleField,int flag) { 
     this.context = context; 
     this.statusField = statusField; 
     this.roleField = roleField; 
     byGetOrPost = flag; 
    } 

    protected void onPreExecute(){ 

    } 
    @Override 
    protected String doInBackground(String... arg0) { 
     if(byGetOrPost == 0){ //means by Get Method 
     try{ 
      String username = (String)arg0[0]; 
      String password = (String)arg0[1]; 
      String link = "http://10.0.2.2/sample/login.php?username=" 
      +username+"&password="+password; 
      //public URL url = new URL(link); 
      HttpClient client = new DefaultHttpClient(); 
      HttpGet request = new HttpGet(); 
      request.setURI(new URI(link)); 
      HttpResponse response = client.execute(request); 
      BufferedReader in = new BufferedReader 
      (new InputStreamReader(response.getEntity().getContent())); 

      StringBuffer sb = new StringBuffer(""); 
      String line=""; 
      while ((line = in.readLine()) != null) { 
       sb.append(line); 
       break; 
      } 
      in.close(); 
      return sb.toString(); 
     }catch(Exception e){ 
     return new String("Exception: " + e.getMessage()); 
     } 
     } 
     else{ 
     try{ 
      String username = (String)arg0[0]; 
      String password = (String)arg0[1]; 
      String link="http://10.0.2.2/sample/index.php"; 
      String data = URLEncoder.encode("username", "UTF-8") 
      + "=" + URLEncoder.encode(username, "UTF-8"); 
      data += "&" + URLEncoder.encode("password", "UTF-8") 
      + "=" + URLEncoder.encode(password, "UTF-8"); 
      URL url = new URL(link); 
      URLConnection conn = url.openConnection(); 
      conn.setDoOutput(true); 
      OutputStreamWriter wr = new OutputStreamWriter 
      (conn.getOutputStream()); 
      wr.write(data); 
      wr.flush(); 
      BufferedReader reader = new BufferedReader 
      (new InputStreamReader(conn.getInputStream())); 
      StringBuilder sb = new StringBuilder(); 
      String line = null; 
      // Read Server Response 
      while((line = reader.readLine()) != null) 
      { 
       sb.append(line); 
       break; 
      } 
      return sb.toString(); 
     }catch(Exception e){ 
      return new String("Exception: " + e.getMessage()); 
     } 
     } 
    } 
    @Override 
    protected void onPostExecute(String result){ 
     this.statusField.setText("Login Successful jjjj"); 
     this.roleField.setText(result); 
    } 
} 
------------------------------------------------------------------------------------------- 
MainActivity.java 
-------------------------------------------------------------------------------------------- 

package com.example.phpmysql; 


import android.app.Activity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.TextView; 

public class MainActivity extends Activity { 

    private EditText usernameField,passwordField; 
    private TextView status,role,method; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     usernameField = (EditText)findViewById(R.id.editText1); 
     passwordField = (EditText)findViewById(R.id.editText2); 
     status = (TextView)findViewById(R.id.textView6); 
     role = (TextView)findViewById(R.id.textView7); 
     method = (TextView)findViewById(R.id.textView9); 
    } 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 
    public void login(View view){ 
     String username = usernameField.getText().toString(); 
     String password = passwordField.getText().toString(); 
     method.setText("Get Method"); 
     new SigninActivity(this,status,role,0).execute(username,password); 

    } 
    public void loginPost(View view){ 
     String username = usernameField.getText().toString(); 
     String password = passwordField.getText().toString(); 
     method.setText("Post Method"); 
     new SigninActivity(this,status,role,1).execute(username,password); 

    } 

} 
+0

請幫我... –

回答

0

我想不出任何好的理由做你正在做的事情,但你的問題如下所示 -

Localhost是指本地設備,在您的電腦上,它是您的電腦...和在Android上...這是您的Android設備。

即使您正在仿真器上運行,該仿真器也會設置爲像真實設備一樣運行,但它並不知道計算機的其他部分。你需要做的是建立一個適當的網絡服務和訪問你的數據庫的方式,或正確使用設備上的SQLite數據庫

另外一個注意,我可以看到你是一個noob,但最好張貼只有您認爲會導致問題的部分代碼以及他們記錄的任何錯誤。如果有人問你,只能添加其他部分 - 沒有人想花時間試圖弄清楚你的代碼混亂,尤其是如果它看起來像你還沒有試圖自己修復/縮小問題

+0

謝謝尼克卡多佐先生。我想要做的是在android.when中設置登錄頁面。當我輸入用戶名,密碼並按下提交按鈕時,應顯示特定用戶的卷號。用戶的詳細信息存儲在名爲temp.MYSQL的數據庫中,包含單個表tabl1(用戶名,密碼,滾動)。但提交時不顯示滾動。沒有編譯錯誤。 –

+0

好的,現在爲什麼這需要在你的電腦上的mysql而不是你的android上的sqlite?如果沒有很好的理由,也許你應該看看Content Provider教程。它會整理並縮短代碼的其餘部分 –

+0

如果這指出你在正確的方向,請現在將其標記爲已接受的答案,謝謝 –