android - 无法处理 webservice 方法调用中的线程?

标签 android multithreading ksoap2

我必须在我的按钮点击事件中编写上面的代码。网络服务工作正常。网络服务方法返回一个字符串。 thatString 等于成功转到下一个布局。它工作正常。但其他部分不起作用。 toast 抛出异常

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




        }
    });

最佳答案

您正在从后台线程访问UI。从 Thread 修改 UI 像这样使用..

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

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

        }
    });

并且不要捕获(Exception e)。这是糟糕的编程习惯。 :)

关于android - 无法处理 webservice 方法调用中的线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11809276/

相关文章:

blackberry - 黑莓手机上不能使用 SOAP

java - 如何绕过 Java 中的 SSL 证书发出 HTTPS SOAP 请求

java - getExternalStorageDirectory 和 getExternalStoragePublicDirectory 之间的区别?

iphone - 从后台线程访问实例变量

java - 谁能给我一个显示 SimpleDateFormat 线程不安全的例子?

c++ - 如何动态加载 C++ 对象并通过包装器接口(interface)使用它们

c# - Ksoap2 : Calling DateTime datatype on . NET Web 服务

android - SearchView 占用了新 ActionBarCompat 中的所有空间

android - 制作一个 Android Studio JNI 静态库以供 JNI 读取

android - 单击时使用Thread.sleep时无法使用setBackgroundResource()