2014-10-11 67 views
0

我嘗試在我的應用程序創建一個登錄, 但每次我登錄總是未知的錯誤,的Android登錄未知錯誤

在我logcat的沒有錯誤代碼, 和我的logcat停在d/ZZZ:OnCreate中LOGIN,

任何人都可以幫助我解決這個問題。

這是我的代碼

public class LoginTask extends AsyncTask <String, Void, Integer> { 
    private final String LOG_TAG = LoginTask.class.getSimpleName(); 
    private String cookiesave, user, pwd; 
    private final String USERNAME = "txtUser"; 
    private final String PASSWORD = "txtPassword"; 
    private final String HTTP_URL = "http://10.0.2.2/gcm/index.php"; 
    private final int UNKNOWN_ERROR = 0; 
    private final int INVALID_LOGIN = 1; 
    private final int SUCCESS_LOGIN = 2; 
    private final int SERVER_ERROR = 5; 
    private Context mContext; 
    private TextView tvErr; 
    private ProgressDialog pDialog; 
    SharedPreferences preferences; 

    public LoginTask (Context context, TextView tv){ 
     mContext = context; 
     tvErr = tv; 
    } 

    @Override 
    protected void onPreExecute() { 
     pDialog = new ProgressDialog(mContext); 
     pDialog.setTitle("Signing in"); 
     pDialog.setMessage("Please Wait..."); 
     pDialog.setCancelable(false); 
     pDialog.setIndeterminate(false); 
     pDialog.show(); 
    } 

    @Override 
    protected Integer doInBackground(String[] params) { 
     // Untuk penyimpanan lokal (username, cookie, dsb) 
     preferences = mContext.getSharedPreferences(Config.PREFERENCES, Context.MODE_PRIVATE); 
     user = params[0]; 
     pwd = params[1]; 

     // Koneksi ke server 
     DefaultHttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost(HTTP_URL); 
     try { 
      List<NameValuePair> loginList = new ArrayList<NameValuePair>(2); 
      loginList.add(new BasicNameValuePair(USERNAME, user)); 
      loginList.add(new BasicNameValuePair(PASSWORD, pwd)); 
      httppost.setEntity(new UrlEncodedFormEntity(loginList)); 
      // Execute HTTP Post Request 
      HttpResponse response = httpclient.execute(httppost); 
      StatusLine statusLine = response.getStatusLine(); 
      Log.d("ZZZ", "ONCREATE LOGIN"); 
      int statusCode = statusLine.getStatusCode(); 
      if (statusCode == 200) { 
       List<Cookie> cookies = httpclient.getCookieStore().getCookies(); 
       Log.d("ZZZ", "STATUS CODE 200"); 
       if (cookies.isEmpty()){ 
        Log.d("ZZZ", "COOKIE EMPTY"); 
        return INVALID_LOGIN; 
       } 
       else{ 
        for (int i = 0; i < cookies.size(); i++) { 
         Cookie cookie = cookies.get(i); 
         if(cookie.getName().equals("session_id")){ 
          cookiesave = cookie.getValue(); 
          Log.d(LOG_TAG, "cookie: " + cookiesave.toString()); 
         } 

        } 
        if(cookiesave.equals(null)) return INVALID_LOGIN; 
        return SUCCESS_LOGIN; 
       } 
      } else if(statusCode == 500) return SERVER_ERROR; 
      else return UNKNOWN_ERROR; 
     } catch (ClientProtocolException e){ 
      e.printStackTrace(); 
      Log.d("ZZZ", "UNKNOWN ERROR 1"); 
      return UNKNOWN_ERROR; 
     } catch (IOException e){ 
      e.printStackTrace(); 
      Log.d("ZZZ", "UNKNOWN ERROR 2"); 
      return UNKNOWN_ERROR; 
     } finally { 
      httpclient.getConnectionManager().shutdown(); 
     } 
    } 

    @Override 
    protected void onPostExecute(Integer s) { 
     pDialog.dismiss(); 
     switch (s){ 
      case SUCCESS_LOGIN:{ 
       SharedPreferences.Editor editor = preferences.edit(); 
       editor.putString(Config.SESSION,cookiesave); 
       editor.putString(Config.USERNAME,user); 
       editor.commit(); 

       Intent in = new Intent(mContext, HomeActivity.class); 
       mContext.startActivity(in); 
       ((Activity)mContext).finish(); 
       break; 
      } 
      case INVALID_LOGIN: { 
       tvErr.setText("Username atau password salah"); 
       break; 
      } 
      case UNKNOWN_ERROR: { 
       tvErr.setText("Unknown error"); 
       break; 
      } 
      case SERVER_ERROR: { 
       tvErr.setText("Server sedang bermasalah, harap coba lagi"); 
       break; 
      } 
      default: { 
       tvErr.setText("Unknown error"); 
       break; 
      } 
     } 
    } 

} 

,這我的PHP代碼

<?php 
    require_once('loader.php'); 
?> 
<!DOCTYPE html> 
<html> 
    <head> 
     <title></title> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    </head> 
    <body> 
    <?php 
     /* These are our valid username and passwords */ 

     if(isset($_POST['txtUser']) && isset($_POST['txtPassword'])){ 
     $userid = $_POST['txtUser']; 
     $password = $_POST['txtPassword']; 
     $query="SELECT username, password FROM Login WHERE username='".$userid."' AND password='".$password."'"; 
     if(!$hasil=mysql_query($query)) 
     { 
      echo mysql_error(); 
      return 0; 
     } 
     $ada_baris=mysql_num_rows($hasil); 
     if($ada_baris>=1) 
     { 
      $session_id=bin2hex(openssl_random_pseudo_bytes(8)); 
      echo $session_id; 
      $query="INSERT INTO tblsession VALUES ('$session_id','$userid', NOW())"; 
      if(mysql_query($query)){ 
      setcookie('session_id',$session_id); 
      } 
     } 
     } 
     if(isset($_COOKIE['session_id'])){ 
      echo "<a href='logout.php'>Logout</a>"; 
     } 
     else{ 
     ?> 
     <form method='POST' action='index.php'> 
      <table> 
      <tr> 
       <td>Username</td> 
       <td><input type='text' name='txtUser' maxlength=10></td> 
      </tr> 
      <tr> 
       <td>Password</td> 
       <td><input type='password' name='txtPassword' maxlength=20></td> 
      </tr> 
      </table> 
      <input type=submit value='Login'> 
     </form> 

     <?php 
     } 
     ?> 

    </body> 
</html> 
+0

嗯。這是非常奇怪的腳本。但無論如何,我發現腥是httppost實體和表單實體名稱不一樣。也許使用請求,而不是後 – Rico 2014-10-11 12:17:03

+0

loginList.add(新的BasicNameValuePair(USERNAME,用戶)); loginList.add(new BasicNameValuePair(PASSWORD,pwd));應該是txtUser和txtPassword也許o.O – Rico 2014-10-11 12:18:02

+0

在移動網站上使用它非常困難:/ comments發送/刪除,甚至不想這樣做 – Rico 2014-10-11 12:20:42

回答

0

變量聲明應該在課堂上和變量初始化完成應在構造函數中完成。

public class LoginTask extends AsyncTask <String, Void, Integer> { 
     private final String LOG_TAG ; 
     private String cookiesave, user, pwd; 
     private final String USERNAME; 
     private final String PASSWORD; 
     private final String HTTP_URL; 
     private final int UNKNOWN_ERROR; 
     private final int INVALID_LOGIN; 
     private final int SUCCESS_LOGIN; 
     private final int SERVER_ERROR 
     private Context mContext; 
     private TextView tvErr; 
     private ProgressDialog pDialog; 
     SharedPreferences preferences; 

     public LoginTask (Context context, TextView tv){ 
      mContext = context; 
      tvErr = tv; 
      LOG_TAG = LoginTask.class.getSimpleName(); 
      USERNAME = "txtUser"; 
      PASSWORD = "txtPassword"; 
      HTTP_URL = "http://10.0.2.2/gcm/index.php"; 
      UNKNOWN_ERROR = 0; 
      INVALID_LOGIN = 1; 
      SUCCESS_LOGIN = 2; 
      SERVER_ERROR = 5; 
     } 
+0

Nah。那沒有必要 – Rico 2014-10-11 12:31:01