2016-01-24 62 views
1

我正在創建AndroidPHPMySQL登錄應用程序將檢查MySQL服務器的用戶名和密碼。我從here未能創建Android PHP MySQL登錄應用程序

我在MySQL檢查名稱和密碼,並將它與哪些用戶類型匹配的教程,但我去首頁活動仍然無法,因爲它顯示無效的用戶名或密碼。

private void login(final String username, final String password) { 

     class LoginAsync extends AsyncTask<String, Void, String> { 

      private Dialog loadingDialog; 

      @Override 
      protected void onPreExecute() { 
       super.onPreExecute(); 
       loadingDialog = ProgressDialog.show(MainActivity.this, "Please wait", "Loading..."); 
      } 

      @Override 
      protected String doInBackground(String... params) { 
       InputStream is = null; 
       List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
       nameValuePairs.add(new BasicNameValuePair("username", username)); 
       nameValuePairs.add(new BasicNameValuePair("password", password)); 
       String result = null; 

       try{ 
        HttpClient httpClient = new DefaultHttpClient(); 
        HttpPost httpPost = new HttpPost(
          "http://192.168.1.7:80/Android/CRUD/login.php"); 
        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

        HttpResponse response = httpClient.execute(httpPost); 

        HttpEntity entity = response.getEntity(); 

        is = entity.getContent(); 

        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8); 
        StringBuilder sb = new StringBuilder(); 

        String line = null; 
        while ((line = reader.readLine()) != null) 
        { 
         sb.append(line + "\n"); 
        } 
        result = sb.toString(); 
       } catch (ClientProtocolException e) { 
        e.printStackTrace(); 
       } catch (UnsupportedEncodingException e) { 
        e.printStackTrace(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
       return result; 
      } 

      @Override 
      protected void onPostExecute(String result){ 
       String s = result.trim(); 
       loadingDialog.dismiss(); 
       if(s.equalsIgnoreCase("success")){ 
        Intent intent = new Intent(MainActivity.this, HomePage.class); 
        //intent.putExtra(USER_NAME, username); 
        finish(); 
        startActivity(intent); 
       }else { 
        Toast.makeText(getApplicationContext(), "Invalid User Name or Password", Toast.LENGTH_LONG).show(); 
       } 
      } 
     } 

     LoginAsync la = new LoginAsync(); 
     la.execute(username, password); 

    } 

的login.php

<? php 
require_once("dbConnect.php"); 

$con = mysqli_connect(HOST,USER,PASS,DB); 

$username = $_POST['name']; 
$password = $_POST['password']; 

$sql = "select * from users where name='$username' and password='$password'"; 

$res = mysqli_query($con,$sql); 

$check = mysqli_fetch_array($res); 

if(isset($check)){ 
echo 'success'; 
}else{ 
echo 'failure'; 
} 

mysqli_close($con); 
?> 
+0

使用json編碼,因爲在你的代碼中可能會返回一些無效字符 – Rasel

+0

@Rasel如何編碼? – Hoo

+0

嘗試使用fiddler2來捕獲HTTP請求來分析 –

回答

0
$username = $_POST['name']; 

改變到這一個:在日誌第一

$username = $_POST['username']; 
+0

嘗試過,沒有運氣:( – Hoo

0

檢查響應..

protected void onPostExecute(String result){ 
      String s = result.trim(); 
      Log.d("response: ", s); 
     } 
+0

它顯示我所有的php – Hoo

+0

所以問題是在你的php – Rasel

+0

如果你使用本地主機+模擬器使用url ... http://10.0.2.2:8080/或json編碼你的迴應 –