Android的 - 发送HTTPS Get请求Android、HTTPS、Get

2023-09-12 09:26:32 作者:酷girl.

我想送一个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).

http://developer.android.com/reference/org/apache/http/Htt$p$psponse.html  http://developer.android.com/reference/org/apache/http/HttpEntity.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:

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:

http://developer.android.com/reference/org/json/JSONObject.html

JSONObject json = new JSONObject(inputStreamAsString);

完成!