Advertisement
alishaik786

ConnectionThread.java

Jan 23rd, 2012
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. //=============================StartUp.java===============================
  2.  
  3. public class StartUp extends UiApplication
  4. {
  5. public static void main(String[]ali)
  6. {
  7. StartUp start=new StartUp();
  8. start.enterEventDispatcher();
  9. }
  10. public StartUp()
  11. {
  12. this.pushScreen(new LoadingScreen());
  13. }
  14.  
  15. public static void exceptionHandling(final String exception)
  16. {
  17. UiApplication.getUiApplication().invokeLater(new Runnable()
  18. {
  19. public void run()
  20. {
  21. Dialog.alert(exception);
  22. }
  23. });
  24. }
  25. }
  26.  
  27. //===================================LoadingScreen.java==============
  28.  
  29. public class LoadingScreen extends MainScreen implements FieldChangeListener
  30. {
  31. ButtonField click;
  32. public LoadingScreen()
  33. {
  34. setTitle("Loading Screen");
  35. createGUI();
  36. }
  37.  
  38. private void createGUI()
  39. {
  40. click=new ButtonField("click");
  41. click.setChangeListener(this);
  42. add(click);
  43. }
  44.  
  45. public void fieldChanged(Field field, int context)
  46. {
  47. if(field==click)
  48. {
  49. ConnectionThread thread=new ConnectionThread(this,"http://startlogic.totemcat.net/sika/WebServices/sikainfo.php?category=0");
  50. thread.start();
  51. }
  52. }
  53.  
  54. public boolean onMenu(int instance)
  55. {
  56. return true;
  57. }
  58.  
  59. public boolean onClose()
  60. {
  61. return super.onClose();
  62. }
  63. }
  64.  
  65. //==============================ConnectionThread.java=============
  66.  
  67.  
  68. public class ConnectionThread extends Thread
  69. {
  70. LoadingScreen loadingScreen;
  71. String url;
  72. HttpConnection httpConnection;
  73. InputStream inputStream;
  74. StringBuffer stringBuffer=new StringBuffer();
  75. public ConnectionThread(LoadingScreen loadingScreen, String url)
  76. {
  77. this.loadingScreen=loadingScreen;
  78. this.url=url;
  79. }
  80. public void run()
  81. {
  82. try
  83. {
  84. httpConnection=(HttpConnection) Connector.open(url+";interface=wifi");
  85. int response=httpConnection.getResponseCode();
  86. if(response==HttpConnection.HTTP_OK)
  87. {
  88. inputStream=httpConnection.openInputStream();
  89. int c;
  90. while((c=inputStream.read())!=-1)
  91. {
  92. stringBuffer.append((char)c);
  93. }
  94. callBack(stringBuffer.toString());
  95. }
  96. else
  97. {
  98. callBack("ERROR");
  99. }
  100. }
  101. catch (Exception e)
  102. {
  103. synchronized (UiApplication.getEventLock())
  104. {
  105. UiApplication.getUiApplication().popScreen(UiApplication.getUiApplication().getActiveScreen());
  106. StartUp.exceptionHandling(e.getMessage());
  107. }
  108. }
  109. finally
  110. {
  111. try
  112. {
  113. if(httpConnection!=null)
  114. {
  115. httpConnection.close();
  116. }
  117. if(inputStream!=null)
  118. {
  119. inputStream.close();
  120. }
  121. }
  122. catch (Exception e2)
  123. {}
  124. }
  125. }
  126. private void callBack(String response)
  127. {
  128. if(response.equals("ERROR"))
  129. {
  130. UiApplication.getUiApplication().popScreen(UiApplication.getUiApplication().getActiveScreen());
  131. StartUp.exceptionHandling("URL Not found");
  132. }
  133. else
  134. {
  135. try
  136. {
  137. System.out.println(response);
  138. //do what you want;
  139. }
  140. catch (Exception e)
  141. {
  142. StartUp.exceptionHandling("Data Not found");
  143. }
  144. }
  145. }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement