0

我正在尝试将我的 Web 服务与 android 中的 AsyncTask 类连接这是我得到的错误我Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 无法弄清楚我的问题是什么。这是我的AsyncTask

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

}
4

2 回答 2

1

好吧,我怀疑您之所以遇到此异常,是因为showMessageBox()您最有可能尝试处理诸如 Toast 之类的 UI 的方法。如果是这种情况,那么在doInBackground(Void... params)方法中您不能调用showMessageBox(),因为它在单独的线程上运行。

于 2013-03-23T19:28:46.227 回答
0

在这里,您尝试从后台线程在主线程上显示某些内容,尝试在 RunonUiThread() 中运行此代码。

else {
   showMessageBox("Email", "Email hatalı!",false);
   login_task.cancel(true);
}
于 2013-03-23T19:28:19.150 回答