Вот пример из хелпа API
Я его немного изменил и добавил кое-что своё (тут мидлет и класс GetHttp):
| Код | /* * Midlet.java * * Created on 20 Ноябрь 2006 г., 11:54 */
import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import javax.microedition.io.*; import java.io.*;
/** * * @author javastic * @version */ public class Midlet extends MIDlet { private Form f; public void startApp() { f = new Form("RSS from Vingrad J2ME"); Display.getDisplay(this).setCurrent(f); new Thread(new GetHttp( f )).start(); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } }
class GetHttp implements Runnable { private Form f; GetHttp(Form f) { this.f = f; } public void run() { HttpConnection c = null; InputStream is = null; OutputStream os = null; int rc; String url = "http://forum.vingrad.ru/rss/forums/114.xml";
try { c = (HttpConnection)Connector.open(url);
// Set the request method and headers c.setRequestMethod(HttpConnection.GET);
// Getting the output stream may flush the headers os = c.openOutputStream();
rc = c.getResponseCode(); if (rc != HttpConnection.HTTP_OK) { throw new IOException("HTTP response code: " + rc); }
is = c.openInputStream(); int ch; StringBuffer sb = new StringBuffer(); while ((ch = is.read()) != -1) { sb.append( (char)ch ); } f.append( sb.toString() );
} catch (IOException ioe ) { ioe.printStackTrace(); } catch (ClassCastException e) { e.printStackTrace(); } finally { if (is != null) try { is.close(); } catch (IOException ex) { ex.printStackTrace(); } if (os != null) try { os.close(); } catch (IOException ex) { ex.printStackTrace(); } if (c != null) try { c.close(); } catch (IOException ex) { ex.printStackTrace(); } } } }
|
|