How to call HTTP URL using wifi in J2ME code for BlackBerry 5.0 and above?

后端 未结 2 1167
梦毁少年i
梦毁少年i 2020-12-21 18:59

I am calling a web service from BlackBerry using J2ME code. When I try to open a connection using HttpConnection, it is checking only the GPRS connection. Now

相关标签:
2条回答
  • 2020-12-21 19:37

    Check this way:

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

    source

    0 讨论(0)
  • 2020-12-21 19:59

    Making Connections

    Rafael's answer will certainly work if you know you'll only be using Wi-Fi.

    However, if you only need to support BlackBerry OS 5.0 - 7.1, I would recommend that you do use the ConnectionFactory. Normally, you will not limit your code to only using one transport. You'll normally support (almost) any transport the device has, but you may want to code your app to choose certain transports first.

    For example,

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

    will choose the Wi-Fi transport if Wi-Fi is available, but use the GPRS connection if it isn't. I think this is generally considered best practice for the 5.0+ devices.

    Request Properties

    This code

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

    is not right. Content-Length should be the size, in bytes, of your HTTP POST parameters. See an example here.

    Threading

    Remember that making network connections is slow. Do not block the user interface by running this code on the main/UI thread. Put your code into a background thread to keep the UI responsive while you request remote content.

    0 讨论(0)
提交回复
热议问题