Gemalto is now part of the Thales Group, find out more.

You are here

Midlet-Servlet | Thales IoT Developer Community

foxmain

October 9, 2014 - 5:39am, 1860 views

Hello,

I'm trying to send an string from the TC65 to Servlet in order to receive the same info to test the app
The problem is I don't receive anything.It would be great if someone can put me in the right direction
Thanks in advance.

//Midlet Code

HttpConnection http = null;
OutputStream oStream=null;
InputStream iStream=null;
String data = "send=123";
String url="http://xxx.xxx.xxx.xx:8080/test3";
String connProfile=";bearer_type=gprs;access_point=wap.gprs.unifon.com.ar;username=wap;password=wap;";

try{

http=(HttpConnection)Connector.open(url+connProfile);

http.setRequestMethod(HttpConnection.POST);

http.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded" );

getConnectionInformation(http);

oStream=http.openOutputStream();

oStream.write(data.getBytes());
oStream.flush();

processServerResponse(http,iStream);

}catch(Exception e){
System.out.println();
}

finally{
if(iStream!=null)
iStream.close();
if(oStream!=null)
oStream.close();
if(http!=null)
http.close();
};

}

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);
}

}catch ( Exception ex ){
System.out.println( "Http : ex : " + ex.getClass() + " : " + ex.getMessage() );
ex.printStackTrace();
}

System.out.println("message="+ sb);

}

};

};

}

//Servlet Code: Example 1

package test3;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class test3 extends HttpServlet

{

public void doPost(HttpServletRequest req,
HttpServletResponse resp)
throws ServletException, IOException
{
doGetOrPost(req,resp);
}
public void doGet(HttpServletRequest req,
HttpServletResponse resp)
throws ServletException, IOException
{

doGetOrPost(req,resp);
}

private void doGetOrPost(HttpServletRequest req,HttpServletResponse resp)throws IOException{

DataInputStream dis = new DataInputStream(req.getInputStream());
String str=dis.readUTF();
dis.close();
ByteArrayOutputStream baos=new ByteArrayOutputStream();
DataOutputStream dos=new DataOutputStream(baos);
dos.writeUTF(str);
dos.flush();
dos.close();
byte[] b= baos.toByteArray();
resp.setContentType("application/octect-stream");
ServletOutputStream so=resp.getOutputStream();
so.write(b);
so.close();
}

}