Over a million developers have joined DZone.

Pull XML From Remote Host - Blackberry Development

DZone's Guide to

Pull XML From Remote Host - Blackberry Development

Free Resource
Hi, i use this class for pull XML from remote host

package com.jacsdev.test

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.IOException;
import javax.microedition.io.HttpConnection;
import net.rim.device.api.io.transport.ConnectionDescriptor;
import net.rim.device.api.io.transport.ConnectionFactory;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;

class GetRemoteXML extends Thread
	public String myUrl = "";
	public GetRemoteXML(String url) {
		myUrl = url;
    public void run()
          ConnectionFactory mCFactory = new ConnectionFactory();
          ConnectionDescriptor connDesc;
          connDesc = mCFactory.getConnection(myUrl);
          if (connDesc != null)
              HttpConnection mHttpConn;
              mHttpConn = (HttpConnection)connDesc.getConnection();
            	  DataInputStream dis = new DataInputStream(mHttpConn.openInputStream()); 
            	  int mBytes = dis.available();
            	  ByteArrayOutputStream mByteArray = new ByteArrayOutputStream();
            	  while ( 0 < mBytes  ) 
            		  byte[] response = new byte[mBytes];
            		  mBytes = dis.available();
            	  // This String contains the xml
            	  final String XmlString = mByteArray.toString();
                  UiApplication.getUiApplication().invokeLater(new Runnable()
                      public void run()
                    	  // Here show an alert on mainscreen with xml content as string
               catch (IOException e) 
            	 System.out.println("IOException: " + e.toString());
                 System.err.println("Caught IOException: " + e.getMessage());

So, here the way i call it

package com.jacsdev.test;

import net.rim.device.api.ui.UiApplication;

public class MyApp extends UiApplication
    public static void main(String[] args)
        MyApp theApp = new MyApp();       
    public MyApp()
    	MyScreen screen = new MyScreen();
        GetRemoteXML  mXML = new GetRemoteXML("http://yourserver.com/yourxmlfile.xml");
    // If  you get success , you'll see a dialog wiht xml string ... so, you must to parser it the way you want


Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}