0

I am trying to do a HTTP Post and as I came to know that time consuming tasks should be used within a Async Task or a Handler, I tried to code it using a Handler but I am getting an Handler uncaught exception, Unable to determine where I am going wrong. Below is my code and the log trace.

           @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    Button Bt = (Button)findViewById(R.id.button1);
    Button Btx = (Button)findViewById(R.id.button2);




    et = (EditText)findViewById(R.id.editText1);
    etp = (EditText)findViewById(R.id.editText2);

    Bt.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            dialog = ProgressDialog.show(MainActivity.this,"Loading", "Please Wait...");

            h = new Handler(){

                @Override
                public void handleMessage(Message msg) {
                    super.handleMessage(msg);
                    dialog.dismiss(); 
                }
            };


            new Thread(){

                @Override
                public void run() {
                    super.run();

                    Connection();

                    try {
                        Thread.sleep(3000);
                        h.sendEmptyMessage(0); 
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }

            }.start();

        }
    });










   // Btx.setOnClickListener(Sig);




}





    private View.OnClickListener Sig =new View.OnClickListener() {
        public void onClick(View v){


            Intent Sign = new Intent(MainActivity.this,Signup.class);
            startActivity(Sign);

        }
        };









    public  void Connection(){
    String is = null;
    String X = et.getText().toString();
    String Y = etp.getText().toString();

    if(X.length()>0 && Y.length()>0){
        A = X;
        B = Y; 
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://animsinc.com/query.php"); 
        try {
           List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
           nameValuePairs.add(new BasicNameValuePair("username", X));
           nameValuePairs.add(new BasicNameValuePair("password",Y));
           httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
           httpclient.execute(httppost);
        //   et.setText(""); // clear text box
        //   etp.setText(""); // clear text box

        HttpResponse responce = httpclient.execute(httppost);
        HttpEntity entity = responce.getEntity();
        is = EntityUtils.toString(entity);
       // in = entity.getContent();
      //  Log.e("post responce----->", "" + is);
        Toast.makeText(this,""+is, Toast.LENGTH_LONG).show();

         } catch (ClientProtocolException e) {
             // TODO Auto-generated catch block
         }catch (UnsupportedEncodingException e) {
            e.printStackTrace();
         }catch (IOException e) {
             // TODO Auto-generated catch block
         }




    } else{
            // display message if text fields are empty
             Toast.makeText(getBaseContext(),"All field are required",Toast.LENGTH_SHORT).show();
         }
    if(is != null){
        Toast.makeText(this,""+is, Toast.LENGTH_LONG).show();

    }



}

}

This is my Log Cat:

enter image description here

Skynet
  • 7,820
  • 5
  • 44
  • 80
  • Possible duplicate of http://stackoverflow.com/questions/3875184/cant-create-handler-inside-thread-that-has-not-called-looper-prepare logcat is just the same. – sandrstar Mar 21 '13 at 10:26

1 Answers1

2

Problem is that you are calling:

Toast.makeText(this,""+is, Toast.LENGTH_LONG).show();

from worker Thread inside run() method. You shouldn't do it. You can manipulate with UI only from Main(UI) Thread.

Solution is to use runOnUiThread() or AsyncTask or show Toast in Handler.

Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106