2

I want to store position coords (latitude, longitude) in a table in my MySQL DB querying a url in a way similar to this one: http://locationstore.com/postlocation.php?latitude=var1&longitude=var2 every ten seconds. PHP script works like a charm. Getting the coords in the device ain't no problem either. But making the request to the server is being a hard one. My code goes like this:

public class LocationHTTPSender extends Thread {
    for (;;) {
        try { 
            //fetch latest coordinates
            coords = this.coords();
            //reset url
            this.url="http://locationstore.com/postlocation.php";
            //              create uri
            uri = URI.create(this.url);

            FireAndForgetDestination ffd = null;

            ffd = (FireAndForgetDestination) DestinationFactory.getSenderDestination
                    ("MyContext", uri);
            if(ffd == null)
            {
                ffd = DestinationFactory.createFireAndForgetDestination
                                  (new Context("MyContext"), uri);
            }

            ByteMessage myMsg = ffd.createByteMessage();
            myMsg.setStringPayload("doesnt matter");
            ((HttpMessage) myMsg).setMethod(HttpMessage.POST);

            ((HttpMessage) myMsg).setQueryParam("latitude", coords[0]);
            ((HttpMessage) myMsg).setQueryParam("longitude", coords[1]);
            ((HttpMessage) myMsg).setQueryParam("user", "1");
            int i = ffd.sendNoResponse(myMsg);


            ffd.destroy();
            System.out.println("Lets sleep for a while..");
            Thread.sleep(10000);
            System.out.println("woke up");

        } catch (Exception e) {
            // TODO Auto-generated catch block
            System.out.println("Exception message: " + e.toString());
            e.printStackTrace();
        }       
}
Michael Donohue
  • 11,776
  • 5
  • 31
  • 44
roymcclure
  • 404
  • 3
  • 12
  • Funny thing is, when I run (or debug) SOMETIMES the first petition will reach the server and work properly. Not the next ones. – roymcclure Sep 23 '12 at 20:16
  • 1
    What symptom are you seeing? Error messages in your println? Exceptions thrown? Please try to be specific. Thanks. – Nate Sep 23 '12 at 22:31
  • No symptoms whatsoever, @Nate. It sleeps and wakes and won't throw a single exception. And the weirdest thing is, as I commented, that first petition will sometimes reach the server, but the following ones won't. It's driving me nuts. – roymcclure Sep 24 '12 at 04:47

2 Answers2

1

I haven't run this code to test it, but I would be suspicious of this call:

        ffd.destroy();

According to the API docs:

Closes the destination. This method cancels all outstanding messages, discards all responses to those messages (if any), suspends delivery of all incoming messages, and blocks any future receipt of messages for this Destination. This method also destroys any persistable outbound and inbound queues. If Destination uses the Push API, this method will unregister associated push subscriptions. This method should be called only during the removal of an application.

So, if you're seeing the first request succeed (at least sometimes), and subsequent requests fail, I would try removing that call to destroy().

See the BlackBerry docs example for this here

Nate
  • 31,017
  • 13
  • 83
  • 207
  • Thank you Nate. That is precisely the example that I followed but the behavior is just the same. I added the ffd.destroy() just for the sake of trying something. I wish I could provide more information. – roymcclure Sep 24 '12 at 10:56
  • @user1688591, so even if you remove the call to `destroy()`, you see the exact same behaviour? Also, have you tried replacing `FireAndForgetDestination` with something like [BlockingSenderDestination](http://www.blackberry.com/developers/docs/6.0.0api/net/rim/device/api/io/messaging/BlockingSenderDestination.html) or `NonBlockingSenderDestination`? I know that ultimately, you don't care about the response, but for debugging purposes, you might have a better chance of catching errors if you try with those other alternatives. – Nate Sep 24 '12 at 20:24
  • @user1688591, also, per the blackberry examples, you are *reusing* your destination objects, but since you're having trouble with the second, third, etc. transmissions, I would try recreating a **new** destination each time (just for debugging). It should just be a performance hit, and you don't seem to have any login credentials that you need for this connection anyway. So, instead of calling `getSenderDestination()`, try using `createFireAndForgetDestination()` **every time**, and see if that helps. Also, is it possible that you use the `"MyContext"` context elsewhere in your code? – Nate Sep 24 '12 at 20:39
1

Ok so I finally got it running cheerfully. The problem was with the transport selection; even though this example delivered WAP2 (among others) as an available transport in my device, running the network diagnostics tool showed only BIS as available. It also gave me the connection parameters that I needed to append at the end of the URL (;deviceside=false;ConnectionUID=GPMDSEU01;ConnectionType=mds-public). The code ended up like this:

for (;;) {
        try {


            coords.refreshCoordinates();

            this.defaultUrl();
            this.setUrl(stringFuncs.replaceAll(this.getUrl(), "%latitude%", coords.getLatitude() + ""));
            this.setUrl(stringFuncs.replaceAll(this.getUrl(), "%longitude%", coords.getLongitude() + ""));


            cd = cf.getConnection(this.getUrl());

            if (cd != null) {

                try {

                    HttpConnection hc = (HttpConnection)cd.getConnection();
                    final int i = hc.getResponseCode();
                    hc.close();

                } catch (Exception e) {

                }

            }               



            //dormir
            Thread.sleep(15000);
        } catch (Exception e) {

        } finally {

            //cerrar conexiones

            //poner objetos a null
        }

Thanks for your help @Nate, it's been very much appreciated.

roymcclure
  • 404
  • 3
  • 12
  • Nice work. You can also **accept** your own answer as the solution, by clicking the little "V" icon next to your answer. That way, we all know that the problem has been solved. Thanks! – Nate Sep 25 '12 at 21:01