我正在尝试将我的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();
}
}
答案 0 :(得分:1)
我怀疑你是因为showMessageBox()
方法而得到这个异常,你最有可能尝试处理像Toast之类的UI。
如果是这种情况,那么在doInBackground(Void... params)
方法中,您无法调用showMessageBox()
,因为它在单独的线程上运行。
答案 1 :(得分:0)
这里你试图在后台线程的主线程上显示一些东西,尝试在RunonUiThread()中运行这段代码。
else {
showMessageBox("Email", "Email hatalı!",false);
login_task.cancel(true);
}