1

I have a webservice call which is called every 10 seconds and should update a TextView with the webservice reply(or atleast show a toast message every 10 seconds)

But the UI is not getting updated at all.

Please find the code below.

public class MessagesRequestActivity extends Activity  {
    /** Called when the activity is first created. */
    String currentMsg="Default";
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //Calling the webservice
        getMessage();
    }
    public void getMessage(){

        try
        {
        SoapObject request = new SoapObject("http://tempuri.org/", "getMessage");

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);

        //Web method call
        HttpTransportSE androidHttpTransport = new HttpTransportSE("http://192.168.4.50/WebService.asmx");
        androidHttpTransport.call("http://tempuri.org/"+ "getMessage", envelope);
        //get the response
        SoapPrimitive response = (SoapPrimitive)envelope.getResponse();

        //the response object can be retrieved by its name: result.getProperty("objectName");
        String message = (String)response.toString();
        Toast.makeText(this, message, Toast.LENGTH_LONG).show();

        }
        catch (Exception e)
        {
        e.printStackTrace();
        }
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        }
}
Chandra Eskay
  • 2,163
  • 9
  • 38
  • 58
  • 1
    Use AsyncTask - http://developer.android.com/reference/android/os/AsyncTask.html – Ron Mar 08 '13 at 15:55
  • 2
    You are performing network operation on Ui thread try using Asynctask or a separate thread and you can use handler to show toast in worker thread – Pragnani Mar 08 '13 at 15:56
  • 2
    Or use Context.runOnUiThread() for the 2 UI update statements – Ron Mar 08 '13 at 15:58
  • @userSeven7s : Do you have sample on AsyncTask. I was not able to understand the progress bar sample. It is confusing. – Chandra Eskay Mar 08 '13 at 16:01
  • @Chandu-Indyaah Its simple.. Create the 'ProgressDialog' in `onPreExecute` and dismiss in `onPostExecute`.. Just google.. you'll find ample of examples.. this is too common.. – Ron Mar 10 '13 at 18:03

2 Answers2

4

As everyone has mentioned, you're making network calls in the UI thread and performing Thread.Sleep() which freezes your UI.

I'd try something like this AsyncHttpClient class, it has all the functionality you need, you'll have to perform your UI updates in the callback.

http://loopj.com/android-async-http/

Badr Ghatasheh
  • 968
  • 2
  • 7
  • 19
1

Here is an example of an AsyncTask

public class TalkToServer extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
    super.onPreExecute();
}

@Override
protected void onProgressUpdate(String... values) {
    super.onProgressUpdate(values);

}

@Override
protected String doInBackground(String... params) {
//do your work here
    return something;
}

@Override
protected void onPostExecute(String result) {
    super.onPostExecute(result);
       // do something with data here-display it or send to mainactivity
}

Then you can access by calling

TalksToServer varName = new TalkToServer(); //pass parameters if you need to the constructor
varName.execute();

Async Docs Progress Dialog Example

You don't want to do network stuff or call sleep on the UI thread. If it is an inner class then you will have access to your member variables of the outer class. Otherwise, create a contructor in the AsyncTask class to pass context if you want to update from onPostExecute or other methods besids doInBackground().

Community
  • 1
  • 1
codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • This looks fine but how do I call this task repeatedly from UI Thread. – Chandra Eskay Mar 08 '13 at 16:35
  • You would just have to create a new `Instance` of it and call `execute()` again. I guess I didn't see the "every 10 seconds" part. You could use a `Handler` to create a new instance and run it – codeMagic Mar 08 '13 at 16:51