How to use HTTP GET request in Java ME
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: Media:HttpGET.zip
Tested with
SDK: Nokia SDK for Java 2.0, Nokia Asha SDK 1.0 (beta)
Devices(s): Nokia Asha 311, Nokia Asha 501
Compatibility
Platform(s): Series 40 DP 2.0 and later, Nokia Asha Platform 1.0
Article
Created: senthilkumar05
(18 Dec 2007)
Updated: grift
(03 Apr 2013)
Reviewed: skalogir
(09 May 2013)
Last edited: skalogir
(13 May 2013)
Code Example
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;
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 = "http://www.google.co.uk/robots.txt";
// 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);
mainScreen.append(requestField);
mainScreen.addCommand(sendCommand);
mainScreen.setCommandListener(this);
}
public void startApp()
{
myDisplay.setCurrent(mainScreen);
}
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.append(resultField);
resultScreen.addCommand(backCommand);
resultScreen.setCommandListener(new CommandListener() {
public void commandAction(Command c, Displayable s) {
notifyDestroyed();
}
});
myDisplay.setCurrent(resultScreen);
}
}.start();
}
else if (c == backCommand)
{
// do it all over again
requestField.setString(defaultURL);
myDisplay.setCurrent(mainScreen);
}
}
// send a GET request to web server
public String sendGetRequest(String urlstring)
{
HttpConnection hc = null;
InputStream dis = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try
{
/* openning up http connection with the web server
* when the connection is opened, the default request
* method is GET
*/
hc = (HttpConnection)
Connector.open(urlstring);
// 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 = dis.read(buf)) != -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());
}
}


Contents
Grift - Updated to work with Nokia 2.0 SDK
I tried to use this code with the latest version of the Nokia 2.0 SDK code and found that there were compile-time errors when I tried to create a project for the code. I have updated the code and created a sample project.grift 00:32, 3 April 2013 (EEST)
Hamishwillee - Thanks for the upload and updates
Hi Grift
Thanks for adding the example code and fixing this. Can you please confirm what devices you tested this on? I will then update the article metadata appropriately.
FYI, I also added an update timestamp to the articlemetadata. The update is for major revisions and changes to the article, and its important to include the timestamp so that people know that the article is current at the date you put in, not just when it was created. It is usual also to update the ArticleMetadata with the SDK, devices tested and versions so that people also know it works on current devices. Sometimes you'll come across an old article that you don't need to update but which works on current devices - in this case you can do the same updates to articlemedatadata but instead update the "reviewed-by" and "reviewed-timestamp" fields (again, so people know the article is still valid)
Thanks very much!
regards
Hhamishwillee 01:37, 3 April 2013 (EEST)
Grift -
The GET and POST examples I updated were both tested on the Nokia Asha 311 through RDA.
Thanks for your advice on updating articles. Now I have some idea of what I am doing, there's a few more broken examples I have come across that I will look at when I get time :)grift 00:11, 4 April 2013 (EEST)
Hamishwillee - Thanks
Hi Grift
Any improvements you can do would be great - feel free to message me (or email) to review
We're trying to formalise the format of "How To" articles to some extent - for an idea of structure see How to detect if an app is running in Kid’s Corner. The idea is to make it easier for people to understand what they need to include in a "good" FAQ - e.g. remembering import/using statements, and special capabilities or signing required, providing code example etc. It is not worth retro-fitting this to older articles systematically, but if you're there anyway, might be worth assessing if using this structure makes sense. Most important thing is that the code works :-)
For general advice you might want to check out Help:Wiki Article Review Checklist
PS, I added teh Asha 311 to metadata as shown.Regards H
hamishwillee 01:28, 4 April 2013 (EEST)