• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

J2ME Http Connection don't receive parameters

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 856
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Saloon Keeper
Posts: 28319
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
this llama doesn't want your drama, he just wants this tiny ad for his mama
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic