2016-09-29 74 views
-1

我創建了一個登錄到MySQL數據庫的Android應用程序,但我沒有收到任何結果。 backgroundWorker.java(函數「onPostExecute」)中alert對話框中的'result'變量返回null,任何人都可以解決這個問題嗎?從Android應用程序登錄MySQL數據庫不成功

BackgroundWorker.java` 公共類BackgroundWorker的擴展

AsyncTask<String,String,String> { 
    Context context; 
    AlertDialog alertDialog; 
    BackgroundWorker(Context ctx) 
    { 
     context =ctx; 
    } 

    @Override 
    protected String doInBackground(String... params) { 
     String type=params[0]; 

     String login_url="192.168.10.9/login.php"; 

     if(type.equals("login")) 
     { 

      try { 
       String user_name = params[1]; 
       String password = params[2]; 
       URL url = new URL(login_url); 
       HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection(); 
       httpURLConnection.setRequestMethod("POST"); 
       httpURLConnection.setDoOutput(true); 
       httpURLConnection.setDoInput(true); 
       OutputStream outputStream = httpURLConnection.getOutputStream(); 
       BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8")); 
       String post_data = URLEncoder.encode("user_name","UTF-8")+"="+URLEncoder.encode(user_name,"UTF-8")+"&" 
         +URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8"); 
       bufferedWriter.write(post_data); 
       bufferedWriter.flush(); 
       bufferedWriter.close(); 
       outputStream.close(); 
       InputStream inputStream = httpURLConnection.getInputStream(); 
       BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1")); 
       String result=""; 
       String line=""; 
       while((line = bufferedReader.readLine())!= null) { 

        result += line; 
       } 
       bufferedReader.close(); 
       inputStream.close(); 
       httpURLConnection.disconnect(); 
       return result; 
      } catch (MalformedURLException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     return null; 
    } 
    @Override 
    protected void onPreExecute() { 
     alertDialog=new AlertDialog.Builder(context).create(); 
     alertDialog.setTitle("Login Status"); 

    } 

    @Override 
    protected void onPostExecute(String Result) { 
     Log.i("ok", "Result " + Result); 
     alertDialog.setMessage(Result); 
     alertDialog.show(); 

    } 

CheckConn.java

public class CheckConn extends AppCompatActivity { 
EditText user; 
EditText pass; 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.check_conn); 


    user = (EditText) findViewById(R.id.et_u); 
    pass = (EditText) findViewById(R.id.et_p); 
    Button press = (Button) findViewById(R.id.btn_login); 
    press.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      onLogin(); 
     } 
    }); 
} 

public void onLogin() 
{ 
    String username= user.getText().toString(); 
    String password=pass.getText().toString(); 
    String type="login"; 

    BackgroundWorker backgroundWorker=new BackgroundWorker(this); 

    backgroundWorker.execute(type,username,password); 
} 

}

connection.php

<?php 

$mysql_usernmae="root"; 
$mysql_password="*****"; 
$db="map"; 

$db= new mysqli('localhost',$mysql_usernmae,$mysql_password,$db); 

?> 

的login.php

<?php 
require "connection.php"; 
$user_name=$_POST["user_name"]; 
$user_pass=$_POST["password"]; 
$mysql_qry="select * from user_info where user_name like 
'$user_name' and user_password like 'user_pass';"; 
    $result=mysqli_query($db,$mysql_qry); 
    if (mysqli_num_rows($result) > 0) 
{ 
    echo "login Successsss"; 
    } 
else 
{ 
echo "faileddddd Booooo"; 
} 
?> 

`

+0

**切勿存儲純文本密碼!**請使用PHP的[內置函數](http://jayblanchard.net/proper_password_hashing_with_PHP.html)來處理密碼安全性。如果您使用的PHP版本低於5.5,則可以使用'password_hash()'[兼容包](https://github.com/ircmaxell/password_compat)。確保你*** [不要越獄密碼](http://stackoverflow.com/q/36628418/1011527)***或在哈希之前使用其他任何清理機制。這樣做*更改密碼並導致不必要的附加編碼。 –

+0

[Little Bobby](http://bobby-tables.com/)說*** [你的腳本存在SQL注入攻擊風險。](http://stackoverflow.com/questions/60174/how-can- ***)瞭解[MySQLi](http://php.net/manual)[準備](http://en.wikipedia.org/wiki/Prepared_statement)聲明/en/mysqli.quickstart.prepared-statements.php)。即使[轉義字符串](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string)是不安全的! [不相信嗎?](http://stackoverflow.com/q/38297105/1011527) –

+0

除了合理的其他評論,我懷疑你的代碼拋出並捕獲一個異常。這意味着有一個堆棧跟蹤的地方。 – f1sh

回答

-1

你爲什麼不先ejecute您的login.php文件中的瀏覽器,harcode爲user_name和user_pass,並檢查值如果連接在服務器端,

之後,你可以檢查你的用戶和通行證是否來自android應用程序,我必須假設你有一個數據庫wi在你正在查詢的表中有一些數據,還要檢查數據庫,表,查詢的名稱是否正確,如果查詢正常並且返回一些結果,你可以用MySQL工作臺檢查。

+0

是的,我檢查login.php文件與硬編碼值它工作正常,我也檢查查詢工作臺它laso工作正常,但它沒有通過Android應用 – Freaks

+0

好,你可以看到如果用戶和傳遞發送到PHP請求? –

+0

@保羅我已經解決了我的問題謝謝 這是一個例外 – Freaks

相關問題