HTTP POST Data reading in Wifily+Aurdino

I face a strange problem, that i am not able to decode the data from http post message from my andriod .

we are sending data, - device + command as part of HTTP POST Header + Data

Android code
void sendSignal(String device, String cmd)
{
HttpParams httpParameters = new BasicHttpParams();
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.1.7:80");
//HttpGet httpget = new HttpGet("http://192.168.1.3/?LED=Fowd+ device ");
HttpResponse response = null;
try
{
// Add your data
Log.d("myapp", "works till here. 2");
//Execute HTTP Post Request
List nameValuePairs = new ArrayList(2);
nameValuePairs.add(new BasicNameValuePair("devicename", device));
nameValuePairs.add(new BasicNameValuePair("command", cmd));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
Log.d("myapp", "devicename " + device);
Log.d("myapp", "command " + cmd);
Log.d("myapp", "httppost " + httppost);
response = httpclient.execute(httppost);
Log.d("myapp", "works till here. 3");
String responseBody = EntityUtils.toString(response.getEntity());
Log.d("myapp", "response " + responseBody);
} catch (ClientProtocolException e11) {
// TODO Auto-generated catch block
e11.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
}
}

but when we take the wifily webserver example, it receive the http post, but i am not able to extract the http post content...

Webserver example of wifily

void loop() {
Client client = server.available();
if (client) {
// an http request ends with a blank line
boolean current_line_is_blank = true;
while (client.connected()) {
if (client.available()) {
char type = client.read();
// if we've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so we can send a reply
if (type == '\n' && current_line_is_blank) {

// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();

// output the value of each analog input pin
for (int i = 0; i < 6; i++) {
client.print("analog input ");
client.print(i);
client.print(" is ");
client.print(analogRead(i));
client.println("
");
}
break;
}
if (type == '\n') {
// we're starting a new line
current_line_is_blank = true;
} else if (type != '\r') {
// we've gotten a character on the current line
current_line_is_blank = false;
}
}
}

if i need to extract http Post data, from where we can read... appreciate the help on this

The POST data is after the blank line in the request.

 // so we can send a reply
   if (type == '\n' && current_line_is_blank) { 
   // this is where the POST data is.
      while(client.available() {
         Serial.write(client.read());
      }

i change the getting of POST Data after blank line, but still i am not able to get the data

But POST Data is as below...how to extract the data after device name and command

0000 50 4f 53 54 20 2f 20 48 54 54 50 2f 31 2e 31 0d POST / H TTP/1.1.
0010 0a 43 6f 6e 74 65 6e 74 2d 4c 65 6e 67 74 68 3a .Content -Length:
0020 20 33 33 0d 0a 43 6f 6e 74 65 6e 74 2d 54 79 70 33..Con tent-Typ
0030 65 3a 20 61 70 70 6c 69 63 61 74 69 6f 6e 2f 78 e: appli cation/x
0040 2d 77 77 77 2d 66 6f 72 6d 2d 75 72 6c 65 6e 63 -www-for m-urlenc
0050 6f 64 65 64 0d 0a 48 6f 73 74 3a 20 31 39 32 2e oded..Ho st: 192.
0060 31 36 38 2e 31 2e 37 3a 38 30 0d 0a 43 6f 6e 6e 168.1.7: 80..Conn
0070 65 63 74 69 6f 6e 3a 20 4b 65 65 70 2d 41 6c 69 ection: Keep-Ali
0080 76 65 0d 0a 55 73 65 72 2d 41 67 65 6e 74 3a 20 ve..User -Agent:
0090 41 70 61 63 68 65 2d 48 74 74 70 43 6c 69 65 6e Apache-H ttpClien
00a0 74 2f 55 4e 41 56 41 49 4c 41 42 4c 45 20 28 6a t/UNAVAI LABLE (j
00b0 61 76 61 20 31 2e 34 29 0d 0a 45 78 70 65 63 74 ava 1.4) ..Expect
00c0 3a 20 31 30 30 2d 43 6f 6e 74 69 6e 75 65 0d 0a : 100-Co ntinue..
00d0 0d 0a 64 65 76 69 63 65 6e 61 6d 65 3d 53 6f 6e ..device name=Son
00e0 79 5f 52 4d 2d 36 38 37 43 26 63 6f 6d 6d 61 6e y_RM-687 C&comman
00f0 64 3d 31 d=1

The blank line is the double CR-LF. I highlighted it in your output below.

00c0 3a 20 31 30 30 2d 43 6f 6e 74 69 6e 75 65 0d 0a : 100-Co ntinue..
00d0 0d 0a 64 65 76 69 63 65 6e 61 6d 65 3d 53 6f 6e ..device name=Son
00e0 79 5f 52 4d 2d 36 38 37 43 26 63 6f 6d 6d 61 6e y_RM-687 C&comman
00f0 64 3d 31 d=1

The POST data is after that. In this case, it appears to be
devicename=Sony_RM-687C
&
command=1

edit: Removed extra spaces from the POST data

Sorry, but it didnt work....

I tryed the changes, but i can get only upto Post

if (c == '\n' && current_line_is_blank) {
for (int i=1;i<20;i++){
if (client.available())
{
char t = client.read();
Serial.print(t);
}
}

but cant get o/p in serial monitor....
IP: 192.168.1.7
POST / HTTP/1.1
Content-Length: 33
Content-Type: application/x-www-form-urlencoded
Host: 192.168.1.7:80
Connection: Keep-Alive
User-Agent: Apache-HttpClient/UNAVAILABLE (java 1.4)
Expect: 100-Continue

can u please share the code sample if any of you have parsed the http post

        for (int i=1;i<20;i++){

You test 20 times if there is a character available to read. If there is, you read and print it. Since the test is fast, and the arrival of data is not, you will find that there is nothing to read 19+ times out of 20.

This is not the way to read the server response.

@Paul, got it..but still i remove the iteration, and read until the client is available, still couldnt able to get data

and read until the client is available

You mean, I hope, read until the client (data) ISN'T available.

Yes... but i can get only junk characters, not the data
Content-Length: 33
Content-Type: application/x-www-form-urlencoded
Host: 192.168.1.7:80
Connection: Keep-Alive
User-Agent: Apache-HttpClient/UNAVAILABLE (java 1.4)
Expect: 100-Continue
ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ

Yes... but i can get only junk characters, not the data

You need to post your code. The "junk" characters are 255/-1 (nothing available to read when read() was called).

Have you read about the 605 bug? Have you applied the patch?

Core code of android emulator

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.1.7:80");
HttpResponse response = null;
try
{
// Add your data
Log.d("myapp", "works till here. 2");
//Execute HTTP Post Request
List nameValuePairs = new ArrayList(2);
nameValuePairs.add(new BasicNameValuePair("devicename",device));
nameValuePairs.add(new BasicNameValuePair("command",cmd));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
Log.d("myapp", "devicename" + device);
Log.d("myapp", "command " + cmd);
Log.d("myapp", "httppost " + httppost);
response = httpclient.execute(httppost);
Log.d("myapp", "works till here. 3");
String responseBody = EntityUtils.toString(response.getEntity());
Log.d("myapp", "response " + responseBody);
} catch (ClientProtocolException e11) {
// TODO Auto-generated catch block
e11.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
}
}

Aurdino wifily webserver code
/*

  • Web Server
  • (Based on Ethernet's WebServer Example)
  • A simple web server that shows the value of the analog input pins.
    */

#include "WiFly.h"
#include "Credentials.h"

Server server(80);

void setup() {
WiFly.begin();

if (!WiFly.join(ssid, passphrase)) {
while (1) {
// Hang on failure.
}
}

Serial.begin(9600);
Serial.print("IP: ");
Serial.println(WiFly.ip());

server.begin();
}

void loop() {
Client client = server.available();
if (client) {
// an http request ends with a blank line
boolean current_line_is_blank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
// if we've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so we can send a reply
if (c == '\n' && current_line_is_blank) {
while(!client.available())
{
char t = client.read();
Serial.print(t);
}

// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();

// output the value of each analog input pin
for (int i = 0; i < 6; i++) {
client.print("analog input ");
client.print(i);
client.print(" is ");
client.print(analogRead(i));
client.println("
");
}
break;
}
if (c == '\n') {
// we're starting a new line
current_line_is_blank = true;
Serial.print(c);
} else if (c != '\r') {
Serial.print(c);
// we've gotten a character on the current line
current_line_is_blank = false;
}
}
}
// give the web browser time to receive the data
delay(100);
client.stop();
}
}

This is the bad line:
while(!client.available())

This should display your POST data. It did for me.

      if (c == '\n' && current_line_is_blank) {
          while(client.available())
          {
              char t = client.read();
              Serial.print(t);
          }    
         // rest of code
      }

I don't have a wifi unit to test a full sketch.