Skip to main content

Getting answer from server using HttpConnection

2 replies [Last post]
Joined: 2009-11-23

Hi people,

I'm using this code in my midlet:

httpc = (HttpConnection), Connector.READ_WRITE);
httpc.setRequestProperty("User-Agent","Profile/MIDP- 1.0 Configuration/CLDC-1.0");
httpc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
dos = httpc.openDataOutputStream();
byte[] argBytes = str_post.getBytes();
response = "";
dis = new DataInputStream(httpc.openInputStream());
int ch;
long lon = httpc.getLength();
if (lon != -1) {
for (int i = 0; i < lon; i++) {
if ((ch = != -1) {
response += String.valueOf((char) ch);
} else {
while ((ch = != -1) {
response += String.valueOf((char) ch);

The str_url is the url in the server when there is a .php.

The .php receive the post and send a response.

This code works fine when I run the.php since localhost, I mean, since an apache server in my computer. But when the .php is in a free server in the internet like x10hosting or lycos, is not working becouse the midlet don't get any response from the server, the httpc.getLength() is always -1. An the midlet doesn't show any error, It's like the .php is not running.

Please, somebody help me, because I've been looking for a long time.

Message was edited by: laura_isabel

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2009-11-23

The problem was that in the server some letters of the url was in capital, and I think linux servers are case sensitive. I was sending to a url without capitals.

Joined: 2010-02-17

So something must be different on your local server opposed to the internet server.

My guess is, the internet server is sending the response in "chunks".
So it does not sent the content length, and you receive it -1 for the length.

That it normal, the http protocol allows the server to do it.
You need to read from the input stream never the less, until you don't get any data.
You should look into example code, like apps/demos/src/example/http in the j2me sdk folder


Karsten Meier