Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

How to use HTTP GET request in Java ME

From Wiki
Jump to: navigation, search

This example demonstrates how to establish a HttpConnection and use it to send a GET request to a web server.

Article Metadata
Code Example
Source file:
Tested with
SDK: Nokia SDK for Java 2.0, Nokia Asha SDK 1.0 (beta)
Devices(s): Nokia Asha 311, Nokia Asha 501
Created: senthilkumar05 (18 Dec 2007)
Updated: grift (03 Apr 2013)
Reviewed: skalogir (09 May 2013)
Last edited: hamishwillee (25 Jul 2013)

Code Example

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class HttpGET extends MIDlet implements CommandListener
* the default value for the url string is
* user can change it to some other urls within the application

private static String defaultURL = "";
// GUI component for user to enter web url
private Display myDisplay = null;
private Form mainScreen;
private TextField requestField;
// GUI component for displaying web page content
private Form resultScreen;
private StringItem resultField;
// the "send" button used on mainScreen
Command sendCommand = new Command("SEND", Command.OK, 1);
// the "back" button used on resultScreen
Command backCommand = new Command("BACK", Command.OK, 1);
public HttpGET()
// initializing the GUI components for entering web urls
myDisplay = Display.getDisplay(this);
mainScreen = new Form("Type in a URL:");
requestField = new TextField(null, defaultURL, 100, TextField.URL);
public void startApp()
public void pauseApp()
public void destroyApp(boolean unconditional)
public void commandAction(Command c, Displayable s)
// when user clicks on "send" button on mainScreen
if (c == sendCommand)
// retrieving the web url that user entered
new Thread() {
private String urlstring = requestField.getString();
public void run() {
// sending a GET request to web server
String resultstring = sendGetRequest(urlstring);
// displaying the page content retrieved from web server
resultScreen = new Form("GET Result:");
resultField = new StringItem(null, resultstring);
resultScreen.setCommandListener(new CommandListener() {
public void commandAction(Command c, Displayable s) {
else if (c == backCommand)
// do it all over again
// send a GET request to web server
public String sendGetRequest(String urlstring)
HttpConnection hc = null;
InputStream dis = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
/* openning up http connection with the web server
* when the connection is opened, the default request
* method is GET

hc = (HttpConnection);
// establishing input stream from the connection
dis = hc.openInputStream();
byte[] buf = new byte[512];
// reading the response from web server character by character
int red;
while ((red = != -1 ) {
baos.write(buf, 0, red);
catch (IOException ioe) {
return "ERROR";
} finally {
try { if(hc != null) hc.close(); } catch (IOException ignored) {}
try { if(dis != null) dis.close();} catch (IOException ignored) {}
return new String(baos.toByteArray());

Related Topics

This page was last modified on 25 July 2013, at 10:46.
217 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.


Thank you!

We appreciate your feedback.