0 Replies - 899 Views - Last Post: 04 November 2011 - 12:19 PM

#1 AMZDeCoder  Icon User is offline

  • D.I.C Head

Reputation: 24
  • View blog
  • Posts: 102
  • Joined: 04-November 11

Connecting to internet in Blackberry.

Posted 04 November 2011 - 12:19 PM

Hi All,

Is there any code snippet which shows exactly how we can connect to the internet from a blackberry phone using J2ME ?
Following is my code which works very finely on Nokia phones with GPRS connectivity, but fails to work on blackberry even though I have blackberry services enabled.
package internet;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

/**
 *
 * @author ameya
 */
public class InternetConnector {

    public static String getDataFromGPRS(String url, final String data[]) {
        if (data != null && data.length > 0) {
            StringBuffer appendParams = new StringBuffer("");
            for (int i = 0; i < data.length - 1; i += 2) {
                appendParams.append(data[i]);
                appendParams.append("=");
                appendParams.append(data[i + 1]);
                appendParams.append("&");
            }
            String finalURLParams = appendParams.toString().substring(0, appendParams.length() - 1);
            url = url + "?" + finalURLParams;
        }

        System.out.println("The URL is: " + url);
        try {
            return getDataFromURL(url);
        } catch (Exception e) {
            return "ERROR: " + e.getMessage();
        }
    }

    private static String getDataFromURL(String url) throws IOException, Exception {
        HttpConnection connection = null;
        InputStream is = null;
        OutputStream os = null;
        StringBuffer stringBuffer = new StringBuffer();


        try {
            connection = (HttpConnection) Connector.open(url);
            connection.setRequestMethod(HttpConnection.GET);
            connection.setRequestProperty("IF-Modified-Since", "20 Jan 2001 16:19:14 GMT");
            connection.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Confirguration/CLDC-1.0");
            connection.setRequestProperty("Content-Language", "en-CA");
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            os = connection.openOutputStream();
            is = connection.openDataInputStream();
            int ch;
            while ((ch = is.read()) != -1) {
                stringBuffer.append((char) ch);
            }
            System.out.println(stringBuffer.toString());
            return stringBuffer.toString().trim();
        } catch (Exception ex) {
            throw ex;
        } finally {
            if (is != null) {
                is.close();
            }
            if (os != null) {
                os.close();
            }
            if (connection != null) {
                connection.close();
            }
        }

    }
}



Is This A Good Question/Topic? 0
  • +

Page 1 of 1