2

位置座標(緯度、経度)をMySQL DBのテーブルに保存し、次のような方法でURLをクエリします: http: //locationstore.com/postlocation.php? latitude = var1&longitude =var210秒ごと。PHPスクリプトは魅力のように機能します。デバイスで座標を取得することも問題ありません。しかし、サーバーにリクエストを送信するのは難しい作業です。私のコードは次のようになります:

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

2 に答える 2

1

私はそれをテストするためにこのコードを実行していませんが、私はこの呼び出しに疑いを持っています:

        ffd.destroy();

APIドキュメントによると:

宛先を閉じます。このメソッドは、すべての未処理のメッセージをキャンセルし、それらのメッセージへのすべての応答(存在する場合)を破棄し、すべての着信メッセージの配信を一時停止し、この宛先のメッセージの今後の受信をブロックします。このメソッドは、永続化可能なアウトバウンドキューとインバウンドキューも破棄します。DestinationがPushAPIを使用する場合、このメソッドは関連するプッシュサブスクリプションの登録を解除します。このメソッドは、アプリケーションの削除中にのみ呼び出す必要があります。

したがって、最初のリクエストが成功し(少なくとも時々)、後続のリクエストが失敗する場合は、への呼び出しを削除してみますdestroy()

こちらのBlackBerryドキュメントの例をご覧ください

于 2012-09-24T06:21:38.877 に答える
1

さて、ようやく元気に走らせました。問題はトランスポートの選択にありました。この例では、(とりわけ)WAP2がデバイスで使用可能なトランスポートとして配信されましたが、ネットワーク診断ツールを実行すると、BISのみが使用可能であることが示されました。また、URLの最後に追加する必要のある接続パラメーター(; deviceside = false; ConnectionUID = GPMDSEU01; ConnectionType = mds-public)も提供されました。コードは次のようになりました。

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
        }

@Nateにご協力いただき、ありがとうございます。

于 2012-09-25T10:56:58.087 に答える