Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.InputStream;
- import java.io.OutputStream;
- import javax.microedition.io.Connector;
- import javax.microedition.io.HttpConnection;
- import net.rim.blackberry.api.browser.URLEncodedPostData;
- import net.rim.device.api.database.Row;
- import net.rim.device.api.database.Statement;
- import net.rim.device.api.io.http.HttpProtocolConstants;
- import net.rim.device.api.ui.UiApplication;
- public class ConnectionThread extends Thread
- {
- String url;
- HttpConnection httpConnection;
- InputStream inputStream;
- StringBuffer stringBuffer=new StringBuffer();
- public ConnectionThread(String url)//Send your url as perameter;
- {
- this.url=url;
- }
- public void run()
- {
- try
- {
- httpConnection=(HttpConnection)Connector.open(url+";interface=wifi");
- int response=httpConnection.getResponseCode();
- if(response==HttpConnection.HTTP_OK)
- {
- inputStream = httpConnection.openInputStream();
- int c;
- while((c=inputStream.read())!=-1)
- {
- stringBuffer.append((char)c);
- }
- // System.out.println("See the data in console:==============="+stringBuffer.toString());
- callBack(stringBuffer.toString());//Here you get String Format;
- }
- else
- {
- callBack("ERROR");
- }
- }
- catch (Exception e)
- {
- synchronized (UiApplication.getEventLock())
- {
- UiApplication.getUiApplication().popScreen(UiApplication.getUiApplication().getActiveScreen());
- //Put the Exception Dialog box;
- }
- }
- finally
- {
- try
- {
- if(httpConnection!=null)
- {
- httpConnection.close();
- }
- if(inputStream!=null)
- {
- inputStream.close();
- }
- }
- catch (Exception e2)
- {}
- }
- }
- private void callBack(String response)
- {
- if(response.equals("ERROR"))
- {
- UiApplication.getUiApplication().popScreen(UiApplication.getUiApplication().getActiveScreen());
- // Put an alert here that "URL Not found";
- }
- else
- {
- try
- {
- System.out.println(response);
- //do what you want; Means XML parsing or JSON parsing;
- }
- catch (Exception e)
- {
- // Put an alert here that "Data Not found";
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment