简体   繁体   中英

RuntimeException in AsyncTask class in Android

I am trying to connect my web service with AsyncTask class in android This is the error i am getting Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() I can not figure out what my problem is here. This is my AsyncTask class

private class LoginTask extends AsyncTask<Void, Void, Void>{
    private String user_email1=user_email.getText().toString(); 
    private String user_password1=user_password.getText().toString();
    private String returnedJson;
    @Override
    protected Void doInBackground(Void... params) {


        if(isEmailValid(user_email1)){
            String authString=user_email1+"::"+user_password1;
            try {
                ConnectService con = new ConnectService("http://xx.xx.xx.xx:9000/loginUser",EncryptInfo.encrypt(authString));
                returnedJson = con.getCatJsonData();

            } catch (Exception e) {
                System.out.println("Encryption ERROR : "+e.getMessage());
            }
        }
        else{
            showMessageBox("Email", "Email hatalı!",false);
            login_task.cancel(true);
        }
        return null;
    }


    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        user_email1=user_email.getText().toString(); 
        user_password1=user_password.getText().toString();
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);

        JSONObject json;
        try {
            json = new JSONObject(returnedJson);
            JSONObject loginJson=json.getJSONObject("myHashMap");
            if(loginJson.getString("responsestatus").equals("OK")){
                login_editor.putString("useremail", user_email1);
                login_editor.putString("userpassword", user_password1);
                if(!login_editor.commit()){
                    showMessageBox("LOGIN HATASI!", "Giriş işlemi gerçekleşirken hata oluştu. Lütfen tekrar deneyin.", true);
                }
                else{
                    Toast.makeText(LoginActivity.this, "login başarılı", Toast.LENGTH_SHORT).show();
                    //ÜRÜN ARAMA ACTİVİTESİNE GEÇ***********    
                }
            }
            else{
                if(loginJson.getString("errorcode").equals("8")){
                    showMessageBox("LOGIN HATASI!", "Email ve password alanları boş.", true);
                }
                else if(loginJson.getString("errorcode").equals("9")){
                    showMessageBox("LOGIN HATASI!", "Geçersiz Email adresi.", true);
                }
                else if(loginJson.getString("errorcode").equals("10")){
                    showMessageBox("LOGIN HATASI!", "Şifre minimum 6 karakter olmalı.", true);                      
                }
                else if(loginJson.getString("errorcode").equals("11")){
                    showMessageBox("LOGIN HATASI!", "Girilen hesap sistemde kayıtlı değil.", true);
                }
                else if(loginJson.getString("errorcode").equals("12")){
                    showMessageBox("LOGIN HATASI!", "Şifre hatalı.", true);
                }
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }


    protected boolean isEmailValid(CharSequence email) {
        return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
    }

}

Well I suspect your are getting this exception because of showMessageBox() method in which you are most probably trying to deal with UI like Toast or something. If this is the case then in doInBackground(Void... params) method you can't call showMessageBox() because it runs on separate thread.

here you are trying to show something on Main thread from the background thread, try to run this code in RunonUiThread().

else {
   showMessageBox("Email", "Email hatalı!",false);
   login_task.cancel(true);
}

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