1

Eclipse の Blackberry プロジェクトから Web サービスに接続しようとしています。URLConnector の私のコードは次のとおりです

import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import net.rim.device.api.ui.component.Dialog;


public class URLConnector 
{
HttpConnection con = null; 
InputStream is = null; 

public URLConnector()   {
    try 
    {
    Dialog.inform("1"); 
    String url = new String("https://webserviceBlahBlah");

    Dialog.inform(con.toString()); 
        int responseCode = con.getResponseCode(); 
        Dialog.inform("3"); 
        if (responseCode != HttpConnection.HTTP_OK) { 
            Dialog.inform("3.5"); 
            System.out.println(responseCode); 
        } 
        is = con.openInputStream(); 
        byte[] responseData = new byte[10000]; 
        int length = 0; 
        Dialog.inform("4"); 
        StringBuffer rawResponse = new StringBuffer(); 
        while (-1 != (length = is.read(responseData))) { 
            Dialog.inform("5"); 
            rawResponse.append(new String(responseData, 0, length)); 
        } 
        final String result = rawResponse.toString(); 
        System.out.println(result); 
        Dialog.inform("6"); 
    } 
    catch (Exception ex) 
    { 
        Dialog.inform("ex.getMessage()"); 
        System.out.println(ex.getMessage()); 
    } 
    finally 
    { 
        try { 
            is.close(); 
            is = null; 
            con.close(); 
            con = null; 
            Dialog.inform("8"); 
        } 
        catch(Exception e){
            Dialog.inform("e"); 
        } 
    } 
}

アプリケーションが con.getReponse() 呼び出しでハングします。これは、blackberry 9800 シミュレーターで実行されています。私は非常に立ち往生しているので、どんな助けも大歓迎です

4

3 に答える 3

2

getResponseCode()は、接続を確立した後に取得した応答を読み取るだけなので、スタックする原因にはなりません。私の推測では、実際にぶら下がっている接続の試みです。5.0以降で開発している場合は、ConnectionFactoryクラスを調べて、接続が正しく作成されていることを確認してください。

于 2011-06-10T13:05:04.640 に答える
0

URL の最後に ;deviceside=true を追加してみてはいかがでしょうか。私の場合は助かりました。

于 2011-08-17T00:10:31.940 に答える
0

ジェームス、あなたが MDS シミュレーターをオンにした後、接続を開かなかったために自分にも問題があることに気付いたと思いますか?

とにかく、最終的な実用的な解決策を提供するために:

    HttpConnection con = null;
    InputStream is = null;
    try {
        System.out.println("1");
        String url = new String("http://myserver.com/index.html");

        //I added these two lines to your code and tested it with my own server and got a 200 RC.
        net.rim.device.api.io.transport.ConnectionFactory cf = new net.rim.device.api.io.transport.ConnectionFactory();
        con = (HttpConnection)cf.getConnection(url).getConnection();

        System.out.println(con.toString());
        int responseCode = con.getResponseCode();
        System.out.println("3 responseCode = " + responseCode);
        if (responseCode != HttpConnection.HTTP_OK) {
            System.out.println("3.5");
            System.out.println(responseCode);
        }

また、BBJava 5.0 マイナスをサポートする必要がある場合は、http ://www.versatilemonkey.com/HttpConnectionFactory.java を参照してください。完全ではありませんが、5.0 より前のプラットフォームの接続を作成することから始めるのに適しています。

于 2011-06-10T15:27:15.427 に答える