简体   繁体   中英

android login unknown error

im trying to create a login in my app, but everytime i login always unknown error,

no error code in my logcat, and my logcat stopped at D/ZZZ﹕ ONCREATE LOGIN,

can anyone help me solving this problem.

this my code

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;
            }
        }
    }

}

and this my php code

<?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>

variable declaration should be done in class and initialization of variables should be done in constructors.

 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;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM