Ahh, long time since I was on pposting end :D
Ok here it goes. The code bellow show mobile application that takes string from user and past it on to servlet. Servlet read it add extra data on front of message and send it back to mobile device that display this new message.
My problem is the red marked part. I'm able to read correct length of sent bytes from server, I'm able to store it in byte array but can not convert back to string.
application system calls will be like this
before transmit name=Peter
length = 23
reading data
Received data = [B@1cb37664
Length 23
str = [B@1cb37664
server calls follows
Print income data: Peter
Response: Received name : 'Peter' length bytes = 23
Any suggestions?
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;
public class GetNpost extends MIDlet implements CommandListener, Runnable
{
private Display display;
private Form fmMain;
private Alert alError;
private Command cmPOST;
private Command cmEXIT;
private TextField tfName;
private StringItem response;
private String errorMsg = null;
//private Thread t;
private String name;
private void initialize()
{
display = Display.getDisplay(this);
cmPOST = new Command("POST", Command.SCREEN, 2);
cmEXIT = new Command("EXIT", Command.EXIT, 1);
tfName = new TextField("Name:", "", 10, TextField.ANY);
response = new StringItem("Response: ", "");
fmMain = new Form("Post Connection");
fmMain.append(tfName);
fmMain.append(response);
fmMain.addCommand(cmEXIT);
fmMain.addCommand(cmPOST);
fmMain.setCommandListener(this);
}
public void startApp()
{
initialize();
//display = Display.getDisplay(this);
display.setCurrent(fmMain);
}
public void pauseApp() {}
public void destroyApp(boolean unconditional){}
public void commandAction(Command c, Displayable s)
{
if(c == cmPOST)
{
name = tfName.getString();
try
{
Thread t = new Thread(this);
t.start();
}
catch(Exception e)
{
e.printStackTrace();
}
}
else if(c == cmEXIT)
{
destroyApp(false);
notifyDestroyed();
}
}
public void run()
{
HttpConnection http = null;
OutputStream oStrm = null;
DataInputStream iStrm = null;
boolean ret = false;
String url = "http://localhost:8080/mobile/postServlet.jsp"; // local Tomcat
try
{
http = (HttpConnection)Connector.open(url);
http.setRequestMethod(HttpConnection.POST);
http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
byte data[] = ("name=" + tfName.getString()).getBytes();
System.out.println("before transmit " + new String(data));
oStrm = http.openOutputStream();
oStrm.write(data);
oStrm.close();
oStrm = null;
iStrm = http.openDataInputStream();
ret = processServerResponce(http, iStrm);
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
finally
{
try{
if(iStrm != null) iStrm.close();
if(oStrm != null) oStrm.close();
if(http != null) http.close();
}
catch(IOException ioe){}
}
if(ret == false)
{
showAlert(errorMsg);
}
}
private boolean processServerResponce(HttpConnection http, DataInputStream dStrm) throws IOException
{
errorMsg = null;
if(http.getResponseCode() == HttpConnection.HTTP_OK)
{
int length = (int) http.getLength();
System.out.println("length = " + length);
String str;
if(length != -1)
{
System.out.println("reading data");
byte servletData[] = new byte[length];
//iStrm.read(servletData);
//DataInputStream dis = http.openDataInputStream();
dStrm.readFully(servletData);
System.out.println("Received data = " + servletData + "\nLength " + servletData.length);
str = servletData.toString();
System.out.println("str = " + str);
}
else
{
ByteArrayOutputStream bStrm = new ByteArrayOutputStream();
int ch;
while((ch = dStrm.read()) != -1)
{
bStrm.write(ch);
}
str = new String(bStrm.toByteArray());
bStrm.close();
}
response.setText(str);
return true;
}
else
{
errorMsg = new String(http.getResponseMessage());
return false;
}
}
private void showAlert(String msg)
{
System.out.println(msg);
alError = new Alert("Error", msg, null, AlertType.ERROR);
alError.setTimeout(Alert.FOREVER);
display.setCurrent(alError, fmMain);
}
}
Servlet
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
public class PostServlet extends HttpServlet
{
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String name = request.getParameter("name");
System.out.println("Print income data: " + name);
String message = "Received name : '" + name + "'";
response.setContentType("text/plain");
response.setContentLength(message.length());
System.out.println("Response: " + message + " length bytes = " + message.length() );
PrintWriter out = response.getWriter();
out.println(message);
}
}