• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

J2ME Http Connection don't receive parameters

 
Diogo Gonzaga
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been developed an application in j2me that send parameters (via POST) to a servlet. But these parameters don't have being recognized by this one (servlet).

J2me code:

HttpConnection http = null;
OutputStream os = null;
InputStream in = null;
boolean ret = false;

String url = "http://localhost:8080/corej2me1/Teste.do";

try {

http = (HttpConnection) Connector.open(url, Connector.READ_WRITE);
http.setRequestMethod(HttpConnection.POST);

http.setRequestProperty("User-Agent",
"Profile/MIDP-1.0 Confirguration/CLDC-1.0");
http.setRequestProperty("Content-Language", "en-CA");
// send input
String str = "name=182016";
http.setRequestProperty("Content-Length", "" + str.length());
http.setRequestProperty("Connection", "close");
http.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");

os = http.openOutputStream();
byte postmsg[] = str.getBytes();
for (int i = 0; i < postmsg.length; i++) {
os.write(postmsg[i]);
}
os.flush();

os.close();


SERVLET CODE:

public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {

// InputStream s = req.getInputStream();
// BufferedReader bis = new BufferedReader(new InputStreamReader(s));
//
// String str;
// while ((str = bis.readLine()) != null) {
// System.out.println(str);
// }

Enumeration enume = req.getHeaderNames();
while (enume.hasMoreElements()) {
String str2 = enume.nextElement().toString();
System.out.println(str2 + " : " + req.getHeader(str2));
}
//
// Enumeration enume2 = req.getParameterNames();
// while(enume2.hasMoreElements()) {
// String str2 = enume2.nextElement().toString();
// System.out.println(str + " : " + req.getParameter(str));
// }
//
System.out.println(req.getParameter("name"));

}

can anybody help me to solve this problem?

Thanks a lot.
 
Amandeep Singh
Ranch Hand
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what do you mean by don't have being recognized ?
 
Diogo Gonzaga
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I try to get parameter "name" as req.getRequestParameter("name") I don't see any result, just null.

When I try to read by InputStream I get the full string (i.e name=diogo).

With method get I receive it correctly.

Whey is it happen??
 
Tim Holloway
Saloon Keeper
Posts: 18359
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
request.getParameter only works on HTTP GET. It obtains its data from the URL parameters.

An HTTP POST stream can actually have any form you want, but the form for data POSTED from an HTML file is something like a 2-line arrangement where one line is the parameter name and the following line is its value.

An HTTP server working with a mobile device/J2ME works exactly the same as it does with any other sort of client, so probably the Servlets and JSPs forum is better to ask about detals like this.

However, this is the place to ask about mobile-specific data formats like WAP.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic