Palaniraja
Palaniraja

Reputation: 233

Can't handle the thread in webservice method calling?

i have to write above code in my button click event. webservice working fine.webservice method return a String. thatString is equal to success go to next layout.it's working fine. but else part not working. toast make a exception

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




        }
    });

Upvotes: 1

Views: 199

Answers (1)

Mohsin Naeem
Mohsin Naeem

Reputation: 12642

You are accessing UI from back ground thread. To modife UI from Thread Use like this..

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

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

        }
    });

And do not catch (Exception e). It is bad programming practice. :)

Upvotes: 1

Related Questions