Dennis K
Dennis K

Reputation: 1878

Getting POST data from HttpRequest (Android)

in my Android app I need to run an HTTP web server. Initial bringup seems ok - I can receive requests and process the URI string when I'm using a Browser to navigate to a page on that server (I see GET request with full URI with all parameters). However, if I'm using a form that submits request to a server:

<FORM action="http://192.168.1.107:8080/testaction" method="post">
    <INPUT type="text" name="textfield" /><BR>
    <INPUT type="submit" value="Send"/>
 </FORM>

I only get the action in the URI (like "/testaction"). I cannot figure out how to get the actual data of the POST request.

Here is the part that handles the request in java code:

public class HomePageHandler implements HttpRequestHandler {
...
        @Override
        public void handle(HttpRequest request, HttpResponse response, HttpContext httpContext) throws HttpException, IOException {

                String uriString = request.getRequestLine().getUri();
                Uri uri = Uri.parse(uriString);
...
}

I realize I could be doing something wrong in java and/or HTML form itself. Please advise on both.

Upvotes: 2

Views: 4404

Answers (2)

Venator85
Venator85

Reputation: 10335

To get the POST body, you can do this:

if (request instanceof HttpEntityEnclosingRequest) { //test if request is a POST
    HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
    String body = EntityUtils.toString(entity); //here you have the POST body
}

Upvotes: 4

Rahul garg
Rahul garg

Reputation: 9362

Refer here for implementing POST request in android..

To cross check it with browser..i can recommend you to look out in the Firebug's(on firefox,similar for others) console for ajax post request. or online tools like http://web-sniffer.net/ or http://hurl.it

Upvotes: 0

Related Questions