How do I call an HTTP URL using Wi-Fi in J2ME code for BlackBerry 5.0 and higher?

I am calling a web service from BlackBerry using the J2ME code. When I try to open a connection using HttpConnection, it only checks the GPRS connection. Now I want to check the Wi-Fi connection and call the web service via Wi-Fi.

The following code is a section of my connection. How to change Wi-Fi code?

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;
}
+1
source share
2 answers

Check out this method:

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

source

+1
source

Creating Connections

Raphael's answer will certainly work if you know that you are only using Wi-Fi.

, BlackBerry OS 5.0 - 7.1, ConnectionFactory. , . () , , , .

,

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-, . , .

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

. Content-Length HTTP POST-. . .

Threading

, . , / . , .

0

All Articles