got error with AsyncTask

北城余情 提交于 2020-01-05 10:32:30

问题


I already use AsyncTask, but I don't understand why I still got error when i tested on my device (OS 4.0) . my apk build in 2.3.3 . I think i made the codes wrong, but i don't know where is my mistake. Anyone please help me, thank you very much

login.java

package com.karismaelearning;

    public class Login extends Activity {
        public Koneksi linkurl;
        String SERVER_URL;
        private Button login, register, setting;
        private EditText username, password;
        public ProgressDialog progressDialog;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.login);

            setting = (Button)findViewById(R.id.bsetting);
            login = (Button) findViewById(R.id.login);
            register = (Button) findViewById(R.id.reg);
            username = (EditText) findViewById(R.id.uname);
            password = (EditText) findViewById(R.id.pass);

            setting.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Intent intentSet = new Intent(Login.this, UrlSetting.class);
                    startActivity(intentSet);
                }
            });

            register.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    Intent intentReg = new Intent(Login.this, Register.class);
                    startActivity(intentReg);
                }
            });

            login.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    new LoginTask().execute();
                }
            });

        }
        protected String tryLogin(String mUsername, String mPassword){           
          Log.d(" TryLoginCheck ","Here");
            HttpURLConnection connection;
           OutputStreamWriter request = null;

                URL url = null;
                String response = null;   
                String temp=null;
                String parameters = "username="+mUsername+"&password="+mPassword;   
                System.out.println("UserName"+mUsername+"\n"+"password"+mPassword);
                Log.d("Parameters",parameters);
                try{
                    linkurl = new Koneksi(this);
                    SERVER_URL = linkurl.getUrl();
                    SERVER_URL += "/mobile/Login.php";
                    url = new URL(SERVER_URL);
                    connection = (HttpURLConnection) url.openConnection();
                    connection.setDoOutput(true);
                    connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                    connection.setRequestMethod("POST");    

                    request = new OutputStreamWriter(connection.getOutputStream());
                    request.write(parameters);
                    request.flush();
                    request.close();            
                    String line = "";               
                    InputStreamReader isr = new InputStreamReader(connection.getInputStream());
                    BufferedReader reader = new BufferedReader(isr);
                    StringBuilder sb = new StringBuilder();
                    while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                    }
                    temp=sb.toString();
                    Log.d("Temp",temp);

                    response = sb.toString();
                    Log.d("Response",response);
                   Log.d("Sb Value",sb.toString());
                    isr.close();
                    reader.close();
                }
                catch(IOException e)    {
                    Toast.makeText(this,e.toString(),Toast.LENGTH_SHORT).show();
                }

                return response;
        }

        public class LoginTask extends AsyncTask<String, String, String> {

    String response = null;

    @Override
    protected void onPreExecute()
    {

    }
    @Override
    protected String doInBackground(String... arg0)
    {
     String mUsername = username.getText().toString();
            String mPassword = password.getText().toString();

            response = tryLogin(mUsername, mPassword).trim();
       return response;
    }
  protected void onPostExecute(String result){

      if(result==null)
      {
          Toast.makeText(Login.this,"result is null- an error occured",Toast.LENGTH_SHORT).show();
        } 
      else{

     Log.d("Check","Here");
        Log.d("Response",response);
     if(response.toLowerCase().contains("berhasil"))
            {
                String nama = username.getText().toString();
                Intent newIntent = new Intent(Login.this, MainPage.class);

                Bundle bundle = new Bundle();

                bundle.putString("nama", nama);

                newIntent.putExtras(bundle);
                startActivityForResult(newIntent, 0);
            }
            else
            {
                //Optional
                //Kalau bisa dibuat constant untuk menghindari salah penulisan
                String RoleError = "ROLE SALAH";
                String UserError = "USER SALAH";

                createDialog("Maaf", response.equals(RoleError) ? "Role Anda bukan Student!" : "Username Atau Password Salah!");
            }
      }
  }
        }
        private void createDialog(String title, String text) {
            AlertDialog ad = new AlertDialog.Builder(this)
            .setPositiveButton("Ok", null)
            .setTitle(title)
            .setMessage(text)
            .create();
            ad.show();
        }
    }

LogCat

06-10 16:50:00.082: D/TryLoginCheck(4534): Here
06-10 16:50:00.090: I/System.out(4534): UserName
06-10 16:50:00.090: I/System.out(4534): password
06-10 16:50:00.090: D/Parameters(4534): username=&password=
06-10 16:50:00.122: W/dalvikvm(4534): threadid=11: thread exiting with uncaught exception (group=0x40bd31f8)
06-10 16:50:00.129: E/AndroidRuntime(4534): FATAL EXCEPTION: AsyncTask #1
06-10 16:50:00.129: E/AndroidRuntime(4534): java.lang.RuntimeException: An error occured while executing doInBackground()
06-10 16:50:00.129: E/AndroidRuntime(4534):     at android.os.AsyncTask$3.done(AsyncTask.java:278)
06-10 16:50:00.129: E/AndroidRuntime(4534):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
06-10 16:50:00.129: E/AndroidRuntime(4534):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
06-10 16:50:00.129: E/AndroidRuntime(4534):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
06-10 16:50:00.129: E/AndroidRuntime(4534):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
06-10 16:50:00.129: E/AndroidRuntime(4534):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
06-10 16:50:00.129: E/AndroidRuntime(4534):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
06-10 16:50:00.129: E/AndroidRuntime(4534):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
06-10 16:50:00.129: E/AndroidRuntime(4534):     at java.lang.Thread.run(Thread.java:856)
06-10 16:50:00.129: E/AndroidRuntime(4534): Caused by: java.lang.NullPointerException
06-10 16:50:00.129: E/AndroidRuntime(4534):     at com.karismaelearning.Login$LoginTask.doInBackground(Login.java:151)
06-10 16:50:00.129: E/AndroidRuntime(4534):     at com.karismaelearning.Login$LoginTask.doInBackground(Login.java:1)
06-10 16:50:00.129: E/AndroidRuntime(4534):     at android.os.AsyncTask$2.call(AsyncTask.java:264)
06-10 16:50:00.129: E/AndroidRuntime(4534):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
06-10 16:50:00.129: E/AndroidRuntime(4534):     ... 5 more
06-10 16:50:05.192: D/OpenGLRenderer(4534): Flushing caches (mode 0)
06-10 16:50:06.309: D/OpenGLRenderer(4534): Flushing caches (mode 1)
06-10 16:50:07.536: I/Process(4534): Sending signal. PID: 4534 SIG: 9

回答1:


in tryLogin, when an Exception occurs you do:

 Toast.makeText(this,e.toString(),Toast.LENGTH_SHORT).show();

that's wrong. The Toast.show() has to run on the UI Thread.

in

tryLogin() {

  try {

  }  catch(IOException e)  {
       return null;
  }

}

and in

protected void onPostExecute(String result){

    if (result == null) {
     Toast.makeText(Login.this,"result is null- an error occured"),Toast.LENGTH_SHORT).show();
    } else {
       result = result.trim();
       // the other stuff
    }

 }

@Override
protected String doInBackground(String... arg0)
{
   String mUsername = username.getText().toString();
   String mPassword = password.getText().toString();          
   return tryLogin(mUsername, mPassword);
}



回答2:


To work out why the routine tryLogin isn't working, I suggest following @BlackBelt's suggestion except for tryLogin() use:

tryLogin() {

  try {

  }  catch(Throwable e)  {
       e.printStackTrace();
       Log.d("Error",e.getMessage());
       return null;
  }

}

and look at the error messages produced.




回答3:


Login request put in doInBackground() method. And handle the response in onPostExecute() method of AsyncTask




回答4:


The Toast won't work in the catch because the background thread can't access the user interface. However, the onPostExecute does run on the main thread, so it does have access to the user interface and toast will work from there.

So I'd define a new class to hold the result of the AsyncTask, and set it up so that the class can either represent success or an error message. Then get the onPostExecute to behave accordingly, depending on whether an error has occurred or not.




回答5:


If i understand properly, the error is due to Looper.prepare() is not being called manually or automatically from framework.

Try calling

super() 
in constructor of your
LoginTask
( From my understanding, When you extend AsyncTask and execute/start it from UI thread you wont get this exception because Looper will be prepared automatically by super class.)

or . A quick fix(Not recommended) : call Looper.prepare() in doInBackground

@Override
protected String doInBackground(String... arg0) {

    try {
        Looper.prepare();
    } catch (Exception e){
        // ignore. There can be only one Looper associated with thread
        // This happens when the current thread already has Looper associated with it
    }

    //TODO: Your original content of doInBackground(...)
}

p.s. If you are creating custom threads by extending Thread but not preparing Looper and using context of UI elements(For ex, To toast message) Android will throw Looper not prepared exception!



来源:https://stackoverflow.com/questions/17020399/got-error-with-asynctask

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!