使用标题android将示例网址转换为http请求?

时间:2015-03-10 08:38:24

标签: android http-headers httprequest

我想将给定的url转换为android

中的HTTP Request

卷曲-v “https://cdws.us-east-1.amazonaws.com/drive/v1/nodes?filters=kind:FILE” - 头“授权:持票人 AtzA的| IQEBLjAsAhQ5zx7pKp9PCgCy6T1JkQjHHOEzpwIUQM“

我介绍了一些方法,但所有的方法都是通过解释来做的。

1 个答案:

答案 0 :(得分:2)

        public static final String URL = "https://cdws.us-east-1.amazonaws.com/drive/v1/nodes?filters=kind:FILE";


        HttpClient httpclient = new DefaultHttpClient();
        HttpGet request = new HttpGet(URL);
        String result = null;
        request.addHeader("Authorization", "Bearer " + authcode);
        //auth code is the code u get by Login on amazon link is http://login.amazon.com/android
        try {
            HttpResponse httpResponse = httpclient.execute(request);
            HttpEntity entity = httpResponse.getEntity();

            if (entity != null) {

                // A Simple JSON Response Read
                InputStream instream = entity.getContent();
                result = convertStreamToString(instream);
                // now you have the string representation of the HTML
                // request
                Log.d("RESPONSE: ", result);
                instream.close();

            }

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        private static String convertStreamToString(InputStream is) {

    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return sb.toString();
}