×
Namespaces

Variants
Actions
Revision as of 01:08, 9 April 2013 by cadlg (Talk | contribs)

How to use HTTP GET request in Java ME

From Nokia Developer 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: Media:HttpGET.zip
Tested with
SDK: Nokia SDK for Java 2.0
Devices(s): Nokia Asha 311
Compatibility
Platform(s): Series 40 DP 2.0 and later
Nokia Asha
Nokia Asha Platform 1.0
Series 40
Series 40 DP 2.0
Article
Created: senthilkumar05 (18 Dec 2007)
Updated: grift (03 Apr 2013)
Last edited: cadlg (09 Apr 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());
}
}

Related Topics

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

×