public class HttpConnector implements Runnable
{
private final static String URL = "http://www.google.it";
private Display display = null;
private Form f = null;
public HttpConnector(Display display)
{
super();
this.display = display;
}
public void run()
{
HttpConnection conn = null;
InputStream is = null;
int rc = -1;
try
{
conn = (HttpConnection)Connector.open(URL);
rc = conn.getResponseCode();
if (rc == HttpConnection.HTTP_OK)
{
int c;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
is = conn.openInputStream();
while ((c = is.read()) != -1)
{
baos.write(c);
}
String s = new String(baos.toByteArray());
baos.close();
is.close();
conn.close();
displayResult(s);
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
private void displayResult(String sb)
{
f = new Form("Result:");
f.append(sb);
display.setCurrent(f);
}
private void displayResult(StringBuffer sb)
{
f = new Form("Result:");
f.append(sb.toString());
display.setCurrent(f);
}
}