充分利用的Htt prequest POST数据(Android版) [英] Getting POST data from HttpRequest (Android)

查看:363
本文介绍了充分利用的Htt prequest POST数据(Android版)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Andr​​oid应用我需要运行一个HTTP Web服务器。初始bringup似乎确定 - 我可以接受请求,当我使用的浏览器导航到该服务器(我看到完整的URI的GET请求的所有参数),在页面处理的URI字符串。
不过,如果我使用的请求提交到服务器的表单:

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>

我只得到了URI的动作(如/ testaction)。我无法弄清楚如何获得POST请求的实际数据。

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

下面是处理在Java code请求的一部分:

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

我知道我可以做一些错误的Ja​​va和/或HTML表单本身。请两个指教。

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

推荐答案

要得到这个职位的身体,你可以这样做:

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
}

这篇关于充分利用的Htt prequest POST数据(Android版)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆