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

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

问题描述

我想向 google 购物 api 发送 HTTPS 获取请求,但是没有什么对我有用,例如,这是我目前正在尝试的:

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.

获得响应并检查它是否为空后,您将需要获取内容(即您的 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/HttpResponse.htmlhttp://developer.android.com/reference/org/apache/http/HttpEntity.htmlhttp://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);

完成!

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

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