0

我正在尝试做一个 HTTP Post,当我知道应该在异步任务或处理程序中使用耗时的任务时,我尝试使用处理程序对其进行编码,但我得到一个处理程序未捕获的异常,无法确定在哪里我错了。下面是我的代码和日志跟踪。

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

    }



}

}

这是我的原木猫:

在此处输入图像描述

4

1 回答 1

2

问题是您正在调用:

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

来自 run() 方法中的工作线程。你不应该这样做。您只能从 Main(UI) 线程使用 UI 进行操作。

解决方案是使用runOnUiThread()AsyncTask或在 Handler 中显示 Toast。

于 2013-03-21T10:29:37.930 回答