【问题标题】:How to call HTTP URL using wifi in J2ME code for BlackBerry 5.0 and above?如何在 BlackBerry 5.0 及更高版本的 J2ME 代码中使用 wifi 调用 HTTP URL?
【发布时间】:2013-08-10 10:52:19
【问题描述】:

我正在使用 J2ME 代码从 BlackBerry 调用 Web 服务。当我尝试使用HttpConnection 打开连接时,它只检查 GPRS 连接。现在,我想检查 Wi-Fi 连接并通过 Wi-Fi 调用 Web 服务。

以下代码是我的连接部分。如何更改 Wi-Fi 连接的代码?

public boolean HttpUrl() 
{
    HttpConnection conn = null;
    OutputStream out = null;
    String url = "http://www.google.com";
    try 
    {
        conn = (HttpConnection) new ConnectionFactory().getConnection(url).getConnection();
        if (conn != null) 
        {

            conn.setRequestMethod(HttpConnection.POST);
            conn.setRequestProperty("Content-Length", "application/x-www-form-urlencoded");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.0");


        }
    } 
    catch (Exception e) 
    {
        return false;
    } 
    finally 
    {
        try 
        {
            out.close();
        } 
        catch (Exception e2) 
        {
        }
    }

    //Only if exception occurs, we close the connection.
    //Otherwise the caller should close the connection himself.
    try 
    {
        conn.close();
    } 
    catch (Exception e1)
    {
    }
    return true;
}

【问题讨论】:

    标签: http blackberry java-me wifi


    【解决方案1】:

    这样检查:

    HttpConnection conn = null;
    String URL = "http://www.myServer.com/myContent;deviceside=true;interface=wifi";
    conn = (HttpConnection)Connector.open(URL);
    

    source

    【讨论】:

    • 据我了解,BlackBerry 的建议是仅编码“;interface=wifi”。有人告诉我添加“;deviceside=true”不是必需的,可能会混淆连接选择逻辑。也就是说,内特的回答也是我推荐的方法。
    【解决方案2】:

    建立联系

    Rafael's answer will certainly work 如果您知道您将使用 Wi-Fi。

    但是,如果您只需要支持 BlackBerry OS 5.0 - 7.1,我建议您使用ConnectionFactory通常,您不会将代码限制为仅使用一个传输You'll normally support (almost) any transport the device has,但您可能希望对您的应用程序进行编码以首先选择某些传输方式。

    例如,

    class ConnectionThread extends Thread
    {
        public void run()
        {
            ConnectionFactory connFact = new ConnectionFactory();
            connFact.setPreferredTransportTypes(new int[] { 
                    TransportInfo.TRANSPORT_TCP_WIFI,
                    TransportInfo.TRANSPORT_BIS_B,
                    TransportInfo.TRANSPORT_MDS,
                    TransportInfo.TRANSPORT_TCP_CELLULAR
            });
            ConnectionDescriptor connDesc;
            connDesc = connFact.getConnection("http://www.google.com");
            if (connDesc != null)
            {
                HttpConnection httpConn;
                httpConn = (HttpConnection)connDesc.getConnection();
                try
                {
                    // TODO: set httpConn request method and properties here!
                    final int iResponseCode = httpConn.getResponseCode();
                    UiApplication.getUiApplication().invokeLater(new Runnable()
                    {
                        public void run()
                        {
                            Dialog.alert("Response code: " + 
                                    Integer.toString(iResponseCode));
                        }
                    });
                } 
                catch (IOException e) 
                {
                    System.err.println("Caught IOException: " 
                            + e.getMessage());
                }
            }
        }
    }    
    

    如果 Wi-Fi 可用,将选择 Wi-Fi 传输,但如果不可用,则使用 GPRS 连接。我认为对于 5.0+ 设备,这通常被认为是 best practice

    请求属性

    这段代码

    conn.setRequestProperty("Content-Length", "application/x-www-form-urlencoded");
    

    不对。 Content-Length 应该是您的 HTTP POST 参数的大小(以字节为单位)。 See an example here.

    线程

    请记住,建立网络连接很慢。不要通过在主/UI 线程上运行此代码来阻塞用户界面。将您的代码放入后台线程,以在您请求远程内容时保持 UI 响应。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多