HTTP Connection thread doesn't update result inside TimerTask - BlackBerry Java

199 views Asked by At

I'm doing a http call with kSoap within a TimerTask so I can update the data something like every five minutes. After getting the data from a web service I provide them to an interface via the function procecssData(). This works out perfectly for the first time, but although the timer is firing every time the data stays the same. So in fact, my UI is being drawn every five minutes but it always uses the data from the first http call. Does someone have an idea why this might happen? Seems to me that the variables inside the httpCall() function are not being updated.

public class ConnectionThread extends Thread {

SoapObject request;
SoapObject result;
SoapSerializationEnvelope envelope;

String[][] resultArray;
int resultLength;

public ConnectionThread(ConnectionCallback conCallback) {

    callbackObj = conCallback;

    refreshTask = new TimerTask() {
        public void run() {
            httpCall();
        }
    };

    new Timer().schedule(refreshTask, 0, 50000);
}

public void httpCall() {

    request = new SoapObject(serviceNamespace, methodName);
    result = null;

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

    http = new HttpTransport(serviceUrl);

    try {
        http.call(soapAction, envelope);
        result = (SoapObject) envelope.getResponse();
        resultLength = result.getPropertyCount();
    } catch (final InterruptedIOException ex) {
        Dialog.alert("No Internet Connection!");
        _isConnected = false;
    }
    // some other catch blocks
    finally {
        http.reset();
    }

    resultArray = new String[resultLength][resultLength * 8];
    // put result into own Stringarray

    if (_isConnected) {
        callbackObj.processData(resultArray, resultLength);
    }
}
}

Any help would be soo appreciated! :) Cheers, Musipoo

1

There are 1 answers

3
Mister Smith On

In the first place I'd advise you not to extend Thread unless you need to override custom threading behavior (that is often not the case and it would be too scary a thing to do). Instead, the recommended approach is to implement a Runnable and pass it to the Thread constructor. In JavaSE they have introduced a new Executors framework that gets rid of instantiating threads the old way. With timers it's similar but here you implement a TimerTask which is pretty much a Runnable (inherits from it), and then you schedule it.

Another issue with your code is that it starts a thread from within a the constructor, which is a dangerous thing to do, because every new instance created will spawn a new thread (the one asociated with the Timer). This is considered an antipattern. Never do this, and if you do, please document it and make sure everyone using this class will know about it. (More info here). Also it is confusing that a class extending Thread launches a Timer in its constructor, and it doesn't even override run (what is the inheritance for?).

These are just tips to make your code clearer and a bit safer, but that won't fix your problem. I think the problem might be in that you are updating the result variable but you are not putting it into resultArray (or maybe you ommited that code?).