Android - 如何使用授权标题发布

时间:2015-03-26 05:12:46

标签: android model-view-controller

我发布了授权标题,代码如

public static String doPost(String url, List<NameValuePair> params) {
    /* 建立HTTPPost对象 */
    HttpPost httpRequest = new HttpPost(url);
    httpRequest.setHeader("Authorization", "Bearer " + token);
    httpRequest.setHeader("Content-type", "application/json");

    String strResult = "doPostError";
    try {
        /* 添加请求参数到请求对象 */
        httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));

        HttpClient httpClient = getHttpClient();
        /* 发送请求并等待响应 */
        HttpResponse httpResponse = httpClient.execute(httpRequest);
        /* 若状态码为200 ok */
        if (httpResponse.getStatusLine().getStatusCode() == 200) {

            InputStream is = httpResponse.getEntity().getContent();//获取内容
            final String result = streamToStr(is);
            /* 读返回数据 */
            strResult = EntityUtils.toString(httpResponse.getEntity());
        } else {
            strResult = "Error Response: "
                    + httpResponse.getStatusLine().toString();
        }
    } catch (ClientProtocolException e) {
        strResult = e.getMessage().toString();
        e.printStackTrace();
    } catch (IOException e) {
        strResult = e.getMessage().toString();
        e.printStackTrace();
    } catch (Exception e) {
        strResult = e.getMessage().toString();
        e.printStackTrace();
    }
    Log.v("Cloud Service", strResult);
    return strResult;
}

服务器端是web api.I发布请求

final String api = "api/RealTime/GetDtu";
                List<NameValuePair> list=new ArrayList<NameValuePair>();
                list.add(new BasicNameValuePair("PhoneId", station.getPhoneId()));

但服务器没有收到任何json参数.Web api只有一个参数是PhoneId。为什么我调用web api但参数为null ??

1 个答案:

答案 0 :(得分:0)

您在请求中设置的实体将作为POST参数传输到服务器。您设置的唯一参数是phoneId。那么你为什么还会得到更多呢?如果你想要更多,你需要设置它们。