Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package posttest;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import javax.microedition.io.Connector;
- import javax.microedition.io.HttpConnection;
- import javax.microedition.midlet.*;
- /**
- * @author Jure
- */
- public class IMlet extends MIDlet {
- private static final String CONNECTOR_EXT=";bearer_type=gprs0;access_point=m2m.027.simobil.si;username=simobil;password=internet;";
- private static final String url = "http://apiscada.voco.si/v1/feeds/122";
- public void startApp() throws MIDletStateChangeException
- {
- HttpConnection http = null;
- OutputStream oStream=null;
- InputStream iStream=null;
- String data = "send=1234";
- try
- {
- http=(HttpConnection)Connector.open(url + CONNECTOR_EXT);
- System.out.println("Connector");
- http.setRequestMethod(HttpConnection.POST);
- System.out.println("Post");
- http.setRequestProperty( "Content-Type", "text/csv" );
- http.setRequestProperty( "X-ApiKey:", "W382YBfl_vMcfoY3t064kE4zyBYtUXUC3v_-FUPOIVg" );
- http.setRequestProperty( "User-Agent", "CyBro-2" );
- http.setRequestProperty( "Content-Length", "9" );
- http.setRequestProperty( "Connection", "close" );
- oStream=http.openOutputStream();
- System.out.println("outstream opened");
- oStream.write(data.getBytes());
- System.out.println("data writen");
- //oStream.flush();
- System.out.println("data flushed");
- System.out.println("http: " + http.getResponseMessage() + " iStream: " + iStream);
- processServerResponse(http,iStream);
- }
- catch(Exception ex)
- {
- System.out.println("Exception " + ex);
- destroyApp(true);
- }
- finally
- {
- try {
- if(iStream!=null)
- iStream.close();
- if(oStream!=null)
- oStream.close();
- if(http!=null)
- http.close();
- } catch (IOException ex) {
- System.out.println("Exception " + ex);
- destroyApp(true);
- }
- };
- }
- public void processServerResponse(HttpConnection http,InputStream iStream) throws IOException
- {
- StringBuffer sb=null;
- try {
- sb=new StringBuffer();
- if (http.getResponseCode()== HttpConnection.HTTP_OK)
- {
- System.out.println("Conexion Post OK");
- iStream=http.openInputStream();
- int ch;
- System.out.println( "Output : " );
- while((ch=iStream.read())!=-1)
- sb.append((char)ch);
- }
- else
- {
- System.out.println("Somthing was wrong: "+ http.getResponseCode());
- destroyApp(true);
- }
- }catch ( Exception ex ){
- System.out.println( "Http : ex : " + ex.getClass() + " : " + ex.getMessage() );
- ex.printStackTrace();
- }
- System.out.println("message="+ sb);
- }
- public void pauseApp() {
- }
- public void destroyApp(boolean unconditional)
- {
- notifyDestroyed();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement