在Web服务方法调用中无法处理线程?

时间:2012-08-04 14:09:16

标签: android multithreading ksoap2

我必须在我的按钮点击事件中写上面的代码。 webservice工作正常.webservice方法返回一个String。 thatString等于成功转到下一个layout.it工作正常。但其他部分不起作用。吐司异常

final SoapSerializationEnvelope envelope1 = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope1.setOutputSoapObject(request1);

          //msg.setText("hi");
            envelope1.dotNet = true;


            final Thread webser=new Thread(){
                public void run()
                {
                    try {
                        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
                        System.out.println("four and Object value is : " +androidHttpTransport);
                        System.out.println("four and URL : " +URL);
                        //this is the actual part that will call the webservice

                        androidHttpTransport.call(SOAP_ACTION, envelope1);

                        System.out.println("four a");
                        // Get the SoapResult from the envelope body.   

                        SoapObject result1 = (SoapObject)envelope1.bodyIn;



                        if(result1 != null)
                        {
                              //Get the first property and change the label text


                            status=result1.getProperty(0).toString();
                            if(status.equalsIgnoreCase("success"))
                            {

                                Intent home=new Intent(LoginActivity.this,MainActivity.class);
                                startActivity(home);

                            }
                            else                                

                            {  
                                Thread.sleep(1000);


                            Toast.makeText(LoginActivity.this,"Enter Valid Username/Password", Toast.LENGTH_LONG).show();
                             }


                        }


                        else
                        {

                             System.out.println("nodata");
                        }
                  } catch (Exception e) {
                        e.printStackTrace();
                        System.out.println("Exception" +e);
                  }
                }


            };
            webser.start();  




        }
    });

1 个答案:

答案 0 :(得分:1)

您正在从后台主题访问UI。要从Thread修改用户界面,请像这样使用..

LoginActivity.this.runOnUiThread(new run Runnable() {

        @Override
        public void run() {
            Toast.makeText(LoginActivity.this,"Enter Valid Username/Password", Toast.LENGTH_LONG).show();

        }
    });

不要抓住(Exception e)。这是糟糕的编程习惯。 :)