android登录未知错误

时间:2014-10-11 11:15:11

标签: java php android login

我试图在我的应用中创建一个登录, 但每次我登录时都会出现未知错误,

我的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>

1 个答案:

答案 0 :(得分: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;
        }