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());
}
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.
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
ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
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();
}
}