关于android:如何发送HTTP POST请求和接收响应?

How to send HTTP POST request and receive response?

我将创建可与CommuniGate Pro服务器配合使用的移动应用程序。

例如,我需要进行以下Android客户端C-CGP Server S对话,并获取XIMSS.nonce节点值:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
C:GET /ximsslogin/ HTTP/1.1
  Host: myserver.com
  Content-Type: text/xml
  Content-Length: 42

  <XIMSS><listFeatures id="list" /><XIMSS>

S:HTTP/1.1 200 OK
  Content-Length: 231
  Connection: keep-alive
  Content-Type: text/xml;charset=utf-8
  Server: CommuniGatePro/5.3

  <XIMSS><nonce>2C3E575E5498CE63574D40F18D00C873</nonce><language>german</language><response id="s"/></XIMSS>

例如,在ActionScript 3.0中,它看起来是这样的:

1
2
3
4
5
6
7
8
var loader:Loader = new Loader();
loader.addEventListener(Event.COMPLETE, completeHandler);
var urlRequest:URLRequest = new URLRequest(...);
urlRequest.method = ...;
urlRequest.data = ...;
loader.load(urlRequest);

private function completeHandler(...):void { ... };

在Java for Android 2.1中看起来如何?


正如Schnapple所说,您的问题似乎很广泛,难以理解和理解。

这是一些用于发送HTTP POST并从服务器获取响应的通用代码,尽管这可能会有所帮助。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public String postPage(String url, File data, boolean returnAddr) {

    ret = null;

    httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109);

    httpPost = new HttpPost(url);
    response = null;

    FileEntity tmp = null;      

    tmp = new FileEntity(data,"UTF-8");

    httpPost.setEntity(tmp);

    try {
        response = httpClient.execute(httpPost,localContext);
    } catch (ClientProtocolException e) {
        System.out.println("HTTPHelp : ClientProtocolException :"+e);
    } catch (IOException e) {
        System.out.println("HTTPHelp : IOException :"+e);
    }
            ret = response.getStatusLine().toString();

            return ret;
}