1

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, I want to check the Wi-Fi connection and call a webservice through Wi-Fi.

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

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

How to achieve this?

cheliyan
  • 1,679
  • 3
  • 16
  • 22

1 Answers1

2

Instead of creating a new connection factory each time, create one just once and have it stored in a variable. You could create several factories as well. For instance, a factory that only makes connections via Wi-Fi would be something like this:

    ConnectionFactory wifiFactory = new ConnectionFactory();
    wifiFactory.setPreferredTransportTypes(new int[]{TransportInfo.TRANSPORT_TCP_WIFI});
Mister Smith
  • 27,417
  • 21
  • 110
  • 193
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – Raptor Aug 20 '13 at 08:42
  • 1
    @ShivanRaptor You are right. I've edited the question, the previous answer was provisional. – Mister Smith Aug 20 '13 at 10:35