Advertisement
Guest User

Untitled

a guest
Nov 21st, 2014
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package posttest;
  7.  
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import java.io.OutputStream;
  11. import javax.microedition.io.Connector;
  12. import javax.microedition.io.HttpConnection;
  13. import javax.microedition.midlet.*;
  14.  
  15. /**
  16. * @author Jure
  17. */
  18. public class IMlet extends MIDlet {
  19.  
  20. private static final String CONNECTOR_EXT=";bearer_type=gprs0;access_point=m2m.027.simobil.si;username=simobil;password=internet;";
  21. private static final String url = "http://apiscada.voco.si/v1/feeds/122";
  22.  
  23. public void startApp() throws MIDletStateChangeException
  24. {
  25. HttpConnection http = null;
  26. OutputStream oStream=null;
  27. InputStream iStream=null;
  28. String data = "send=1234";
  29.  
  30. try
  31. {
  32. http=(HttpConnection)Connector.open(url + CONNECTOR_EXT);
  33. System.out.println("Connector");
  34. http.setRequestMethod(HttpConnection.POST);
  35. System.out.println("Post");
  36. http.setRequestProperty( "Content-Type", "text/csv" );
  37. http.setRequestProperty( "X-ApiKey:", "W382YBfl_vMcfoY3t064kE4zyBYtUXUC3v_-FUPOIVg" );
  38. http.setRequestProperty( "User-Agent", "CyBro-2" );
  39. http.setRequestProperty( "Content-Length", "9" );
  40. http.setRequestProperty( "Connection", "close" );
  41.  
  42.  
  43. oStream=http.openOutputStream();
  44. System.out.println("outstream opened");
  45. oStream.write(data.getBytes());
  46. System.out.println("data writen");
  47. //oStream.flush();
  48. System.out.println("data flushed");
  49. System.out.println("http: " + http.getResponseMessage() + " iStream: " + iStream);
  50. processServerResponse(http,iStream);
  51.  
  52. }
  53. catch(Exception ex)
  54. {
  55. System.out.println("Exception " + ex);
  56. destroyApp(true);
  57. }
  58.  
  59. finally
  60. {
  61. try {
  62. if(iStream!=null)
  63. iStream.close();
  64. if(oStream!=null)
  65. oStream.close();
  66. if(http!=null)
  67. http.close();
  68. } catch (IOException ex) {
  69. System.out.println("Exception " + ex);
  70. destroyApp(true);
  71. }
  72. };
  73.  
  74. }
  75.  
  76. public void processServerResponse(HttpConnection http,InputStream iStream) throws IOException
  77. {
  78. StringBuffer sb=null;
  79.  
  80. try {
  81. sb=new StringBuffer();
  82.  
  83. if (http.getResponseCode()== HttpConnection.HTTP_OK)
  84. {
  85. System.out.println("Conexion Post OK");
  86.  
  87. iStream=http.openInputStream();
  88. int ch;
  89. System.out.println( "Output : " );
  90. while((ch=iStream.read())!=-1)
  91. sb.append((char)ch);
  92. }
  93. else
  94. {
  95. System.out.println("Somthing was wrong: "+ http.getResponseCode());
  96. destroyApp(true);
  97. }
  98.  
  99.  
  100. }catch ( Exception ex ){
  101. System.out.println( "Http : ex : " + ex.getClass() + " : " + ex.getMessage() );
  102. ex.printStackTrace();
  103. }
  104.  
  105. System.out.println("message="+ sb);
  106.  
  107. }
  108.  
  109. public void pauseApp() {
  110. }
  111.  
  112. public void destroyApp(boolean unconditional)
  113. {
  114. notifyDestroyed();
  115. }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement