Advertisement
alishaik786

Connection.java

Jan 23rd, 2012
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. public class ConnectionThread extends Thread
  2. {
  3. LoadingScreen loadingScreen;
  4. String url;
  5. HttpConnection httpConnection;
  6. InputStream inputStream;
  7. String id="0";
  8. StringBuffer stringBuffer=new StringBuffer();
  9. public ConnectionThread(LoadingScreen loadingScreen, String url)
  10. {
  11. this.loadingScreen=loadingScreen;
  12. this.url=url;
  13. }
  14. public void run()
  15. {
  16. try
  17. {
  18. httpConnection=(HttpConnection)Connector.open("http://startlogic.totemcat.net/sika/WebServices/sikainfo.php"+";interface=wifi");
  19. URLEncodedPostData oPostData = new URLEncodedPostData(URLEncodedPostData.DEFAULT_CHARSET, false);
  20.  
  21. oPostData.append("category",id);
  22. System.out.println("================"+oPostData.toString());
  23. httpConnection.setRequestMethod(HttpConnection.POST);
  24. httpConnection.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_TYPE, oPostData.getContentType());
  25. byte [] postBytes = oPostData.getBytes();
  26. httpConnection.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LENGTH, Integer.toString(postBytes.length));
  27. OutputStream strmOut = httpConnection.openOutputStream();
  28. strmOut.write(postBytes);
  29. strmOut.flush();
  30. strmOut.close();
  31.  
  32. int response=httpConnection.getResponseCode();
  33. if(response==HttpConnection.HTTP_OK)
  34. {
  35. inputStream = httpConnection.openInputStream();
  36. int c;
  37. while((c=inputStream.read())!=-1)
  38. {
  39. stringBuffer.append((char)c);
  40. }
  41. // System.out.println(">>>>>>>>>>>>>>>>>"+stringBuffer.toString());
  42. callBack(stringBuffer.toString());
  43.  
  44. }
  45. else
  46. {
  47. callBack("ERROR");
  48. }
  49. }
  50. catch (Exception e)
  51. {
  52. synchronized (UiApplication.getEventLock())
  53. {
  54. UiApplication.getUiApplication().popScreen(UiApplication.getUiApplication().getActiveScreen());
  55. StartUp.exceptionHandling(e.getMessage());
  56. }
  57. }
  58. finally
  59. {
  60. try
  61. {
  62. if(httpConnection!=null)
  63. {
  64. httpConnection.close();
  65. }
  66. if(inputStream!=null)
  67. {
  68. inputStream.close();
  69. }
  70. }
  71. catch (Exception e2)
  72. {}
  73. }
  74. }
  75. private void callBack(String response)
  76. {
  77. if(response.equals("ERROR"))
  78. {
  79. UiApplication.getUiApplication().popScreen(UiApplication.getUiApplication().getActiveScreen());
  80. StartUp.exceptionHandling("URL Not found");
  81. }
  82. else
  83. {
  84. try
  85. {
  86. System.out.println(response);
  87. //do what you want;
  88. }
  89. catch (Exception e)
  90. {
  91. StartUp.exceptionHandling("Data Not found");
  92. }
  93. }
  94. }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement