DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Pull XML From Remote Host - Blackberry Development
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();
try
{
DataInputStream dis = new DataInputStream(mHttpConn.openInputStream());
int mBytes = dis.available();
ByteArrayOutputStream mByteArray = new ByteArrayOutputStream();
while ( 0 < mBytes )
{
byte[] response = new byte[mBytes];
dis.read(response);
mByteArray.write(response);
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
Dialog.alert(XmlString.toString());
}
});
}
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();
theApp.enterEventDispatcher();
}
public MyApp()
{
MyScreen screen = new MyScreen();
pushScreen(screen);
GetRemoteXML mXML = new GetRemoteXML("http://yourserver.com/yourxmlfile.xml");
mXML.start();
}
// If you get success , you'll see a dialog wiht xml string ... so, you must to parser it the way you want
}
@jacsdev




