Advertisement
alishaik786

XMLParsing.java

Jan 30th, 2012
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. //===========Startup.java file==============
  2.  
  3. import net.rim.device.api.lcdui.control.DirectionControl;
  4. import net.rim.device.api.ui.Ui;
  5. import net.rim.device.api.ui.UiApplication;
  6. import net.rim.device.api.ui.UiEngineInstance;
  7. import net.rim.device.api.ui.component.Dialog;
  8.  
  9. public class StartUp extends UiApplication
  10. {
  11. public static boolean isLandScape=false;
  12. public static UiEngineInstance uiEngineInstance;
  13. public static void main(String[]args)
  14. {
  15. StartUp start=new StartUp();
  16. start.enterEventDispatcher();
  17. }
  18. public StartUp()
  19. {
  20. uiEngineInstance=Ui.getUiEngineInstance();
  21. uiEngineInstance.setAcceptableDirections(DirectionControl.DIRECTION_LANDSCAPE);
  22. this.pushScreen(new LoadingScreen());
  23. }
  24.  
  25. public static void errorHandling(final String exception)
  26. {
  27. UiApplication.getUiApplication().invokeLater(new Runnable()
  28. {
  29. public void run()
  30. {
  31. Dialog.alert(exception);
  32. }
  33. });
  34. }
  35. }
  36. //===============================LoadingScreen.java;
  37.  
  38. public class LoadingScreen extends MainScreen implements FieldChangeListener
  39. {
  40. ButtonField click;
  41. public LoadingScreen()
  42. {
  43. setTitle("Loading Screen");
  44. createGUI();
  45. }
  46.  
  47. public void createGUI()
  48. {
  49. setTitle("Loading Screen");
  50. click=new ButtonField("Click");
  51. click.setChangeListener(this);
  52. add(click);
  53. }
  54.  
  55. public void fieldChanged(Field field, int context)
  56. {
  57. if(field==click)
  58. {
  59. String customerIdUserIdPassword="pari";
  60. String url="http://122.166.229.151:8080/imonitor/mobile/login.action?data=<imonitor><customerid>"+customerIdUserIdPassword+"</customerid><userid>"+customerIdUserIdPassword+"</userid><password>"+customerIdUserIdPassword+"</password></imonitor>";
  61. ConnectionThread thread=new ConnectionThread(this,url,1);
  62. thread.start();
  63. }
  64. }
  65.  
  66. public void getResponse(String result)
  67. {
  68. StartUp.errorHandling(result);
  69. }
  70. }
  71. //=======================ConnectionThread.java;
  72.  
  73. import java.io.ByteArrayInputStream;
  74. import java.io.InputStream;
  75.  
  76. import javax.microedition.io.Connector;
  77. import javax.microedition.io.HttpConnection;
  78.  
  79. import net.rim.device.api.ui.UiApplication;
  80. import net.rim.device.api.xml.jaxp.SAXParserImpl;
  81.  
  82.  
  83. public class ConnectionThread extends Thread
  84. {
  85. String url;
  86. HttpConnection httpConnection;
  87. InputStream inputStream;
  88. StringBuffer stringBuffer=new StringBuffer();
  89. LoadingScreen loadingScreen;
  90. int index;
  91. public ConnectionThread(LoadingScreen loadingScreen, String url,int index)
  92. {
  93. this.index=index;
  94. this.loadingScreen=loadingScreen;
  95. this.url=url;
  96. }
  97. public void run()
  98. {
  99. try
  100. {
  101. httpConnection=(HttpConnection)Connector.open(url+";interface=wifi");
  102. int response=httpConnection.getResponseCode();
  103. if(response==HttpConnection.HTTP_OK)
  104. {
  105. inputStream = httpConnection.openInputStream();
  106. byte data[];
  107. data=IOUtilities.streamToBytes(inputStream);
  108. String result=new String(data);
  109. callBack(result);
  110. }
  111. else
  112. {
  113. callBack("ERROR");
  114. }
  115. }
  116. catch (Exception e)
  117. {
  118. synchronized (UiApplication.getEventLock())
  119. {
  120. UiApplication.getUiApplication().popScreen(UiApplication.getUiApplication().getActiveScreen());
  121. StartUp.errorHandling(e.getMessage());
  122. }
  123. }
  124. finally
  125. {
  126. try
  127. {
  128. if(httpConnection!=null)
  129. {
  130. httpConnection.close();
  131. }
  132. if(inputStream!=null)
  133. {
  134. inputStream.close();
  135. }
  136. }
  137. catch (Exception e2)
  138. {}
  139. }
  140. }
  141.  
  142. private void callBack(String response)
  143. {
  144. if(response.equals("ERROR"))
  145. {
  146. UiApplication.getUiApplication().popScreen(UiApplication.getUiApplication().getActiveScreen());
  147. StartUp.errorHandling("Check the URL");
  148. }
  149. else
  150. {
  151. int i=response.indexOf("<status>")+8;
  152. int j=response.indexOf("</status>");
  153. loadingScreen.getResponse(response.substring(i, j));
  154. }
  155. }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement