Advertisement
alishaik786

ConnectionThreadOne.java

Jan 30th, 2012
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. import java.io.InputStream;
  2. import java.io.OutputStream;
  3.  
  4. import javax.microedition.io.Connector;
  5. import javax.microedition.io.HttpConnection;
  6.  
  7. import net.rim.blackberry.api.browser.URLEncodedPostData;
  8. import net.rim.device.api.database.Row;
  9. import net.rim.device.api.database.Statement;
  10. import net.rim.device.api.io.http.HttpProtocolConstants;
  11. import net.rim.device.api.ui.UiApplication;
  12.  
  13.  
  14. public class ConnectionThread extends Thread
  15. {
  16. String url;
  17. HttpConnection httpConnection;
  18. InputStream inputStream;
  19. StringBuffer stringBuffer=new StringBuffer();
  20. public ConnectionThread(String url)//Send your url as perameter;
  21. {
  22. this.url=url;
  23. }
  24. public void run()
  25. {
  26. try
  27. {
  28. httpConnection=(HttpConnection)Connector.open(url+";interface=wifi");
  29. int response=httpConnection.getResponseCode();
  30. if(response==HttpConnection.HTTP_OK)
  31. {
  32. inputStream = httpConnection.openInputStream();
  33. int c;
  34. while((c=inputStream.read())!=-1)
  35. {
  36. stringBuffer.append((char)c);
  37. }
  38. // System.out.println("See the data in console:==============="+stringBuffer.toString());
  39. callBack(stringBuffer.toString());//Here you get String Format;
  40. }
  41. else
  42. {
  43. callBack("ERROR");
  44. }
  45. }
  46. catch (Exception e)
  47. {
  48. synchronized (UiApplication.getEventLock())
  49. {
  50. UiApplication.getUiApplication().popScreen(UiApplication.getUiApplication().getActiveScreen());
  51. //Put the Exception Dialog box;
  52. }
  53. }
  54. finally
  55. {
  56. try
  57. {
  58. if(httpConnection!=null)
  59. {
  60. httpConnection.close();
  61. }
  62. if(inputStream!=null)
  63. {
  64. inputStream.close();
  65. }
  66. }
  67. catch (Exception e2)
  68. {}
  69. }
  70. }
  71.  
  72. private void callBack(String response)
  73. {
  74. if(response.equals("ERROR"))
  75. {
  76. UiApplication.getUiApplication().popScreen(UiApplication.getUiApplication().getActiveScreen());
  77. // Put an alert here that "URL Not found";
  78. }
  79. else
  80. {
  81. try
  82. {
  83. System.out.println(response);
  84. //do what you want; Means XML parsing or JSON parsing;
  85. }
  86. catch (Exception e)
  87. {
  88. // Put an alert here that "Data Not found";
  89. }
  90. }
  91. }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement