我有一个api,内容如下:

给定一个典型的HTML形式,如下所示:

 <form method="post" action="/api">
       <input type="hidden" name="action" value="login" />
      <input type="hidden" name="username" value="testuser" />
      <input type="hidden" name="password" value="123" />
 </form>


提交上面的HTML表单后,下面显示了一个示例
可以使用HTTP协议发送信息:

 POST /api HTTP/1.0
 Content-Type: application/x-www-form-urlencoded
 Content-Length: 43
 action=login&username=testuser&password=123


系统将发送回如下所示的答复:

HTTP/1.0 200 OK
Content-Type: application/json
{ 'session_token':'a1234aa334567432bccdd001f123450abcedfa0b' }


谁能指导我如何使用android java从发布响应中检索会话令牌?

编辑:(这是我的一些代码,用于发送和检索请求和响应。)

  HttpPost httpost = new HttpPost("url of server");
        List <NameValuePair> nvps = new ArrayList <NameValuePair>();
        nvps.add(new BasicNameValuePair("username","testuser" ));
        nvps.add(new BasicNameValuePair("password", "1234567" ));
        httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
        HttpResponse response = getResponse(httpost);

public HttpResponse getResponse(HttpPost httpPost) throws Exception {
    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpResponse response = httpclient.execute(httpPost);
    return response;
}

最佳答案

我是Android API的新手,但建议以下几点:

选项1:
Header[] headers = httpResponse.getAllHeaders();

选项2:
Header[] headers = httpResponse.getHeaders( "session_token_name_here" );

遍历headers数组以查找所需的标头,例如session_token

关于java - 如何从httpResponse检索 session token ?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8305858/

10-12 15:49