Android的 - 发送HTTPS Get请求 [英] Android - Sending HTTPS Get Request

查看:176
本文介绍了Android的 - 发送HTTPS Get请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想送一个HTTPS GET请求到谷歌购物API,但是没有什么很为我工作,例如这里就是我想要的那一刻:

I would like to send a HTTPS Get Request to the google shopping api however nothing is quite working for me, for example here is what I'm trying at the moment:

try {        
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet();
        request.setURI(new URI("https://www.googleapis.com/shopping/search/v1/public/products/?key={my_key}&country=&q=t-shirts&alt=json&rankByrelevancy="));
        HttpResponse response = client.execute(request);
    } catch (URISyntaxException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }   
    return response;
}    

如果任何人有关于如何改进这个或更换,请让我知道,在此先感谢任何建议。

If anyone has any suggestions on how to improve this or replace it please let me know, thanks in advance.

推荐答案

您应该得到一个编译错误。

You should be getting a compile error.

这是正确的版本:

HttpResponse response = null;
try {        
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet();
        request.setURI(new URI("https://www.googleapis.com/shopping/search/v1/public/products/?key={my_key}&country=&q=t-shirts&alt=json&rankByrelevancy="));
        response = client.execute(request);
    } catch (URISyntaxException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }   
    return response;
}

因此​​,如果现在你有你的回应将返回为空的错误。

Therefore now if you have an error your response will be returned as null.

一旦你的回应,并检查它是否为null,你想要得到的内容(即你的JSON)。

Once you have the response and checked it for null, you'll want to get the content (i.e. your JSON).

<一个href="http://developer.android.com/reference/org/apache/http/Htt$p$psponse.html">http://developer.android.com/reference/org/apache/http/Htt$p$psponse.html  <一href="http://developer.android.com/reference/org/apache/http/HttpEntity.html">http://developer.android.com/reference/org/apache/http/HttpEntity.html  <一href="http://developer.android.com/reference/java/io/InputStream.html">http://developer.android.com/reference/java/io/InputStream.html

response.getEntity().getContent();

这给你一个InputStream的工作。如果你想将它转换为字符串,你会做以下或等价的:

This gives you an InputStream to work with. If you want to convert this to a string you'd do the below or equivalent:

<一个href="http://www.mkyong.com/java/how-to-convert-inputstream-to-string-in-java/">http://www.mkyong.com/java/how-to-convert-inputstream-to-string-in-java/

public static String convertStreamToString(InputStream inputStream) throws IOException {
        if (inputStream != null) {
            Writer writer = new StringWriter();

            char[] buffer = new char[1024];
            try {
                Reader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"),1024);
                int n;
                while ((n = reader.read(buffer)) != -1) {
                    writer.write(buffer, 0, n);
                }
            } finally {
                inputStream.close();
            }
            return writer.toString();
        } else {
            return "";
        }
    }

当你有这样的字符串,你需要从它创建一个JSONObject的:

When you have this string you need to create a JSONObject from it:

<一个href="http://developer.android.com/reference/org/json/JSONObject.html">http://developer.android.com/reference/org/json/JSONObject.html

JSONObject json = new JSONObject(inputStreamAsString);

完成!

这篇关于Android的 - 发送HTTPS Get请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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