Android - 发送HTTPS获取请求

Sam*_*son 15 https android get http httpclient

我想向谷歌购物api发送一个HTTPS获取请求,但是没有什么对我有用,例如这里是我正在尝试的内容:

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;
}    
Run Code Online (Sandbox Code Playgroud)

如果有人对如何改进或更换它有任何建议,请提前通知我.

Blu*_*ell 40

您应该收到编译错误.

这是正确的版本:

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;
}
Run Code Online (Sandbox Code Playgroud)

因此,现在如果您有错误,您的响应将返回null.

获得响应并将其检查为null后,您将需要获取内容(即您的JSON).

http://developer.android.com/reference/org/apache/http/HttpResponse.html http://developer.android.com/reference/org/apache/http/HttpEntity.html http://developer.android. COM /参考/ JAVA/IO/InputStream.html

response.getEntity().getContent();
Run Code Online (Sandbox Code Playgroud)

这为您提供了一个可以使用的InputStream.如果您想将其转换为字符串,您可以执行以下操作或等效:

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 "";
        }
    }
Run Code Online (Sandbox Code Playgroud)

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

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

JSONObject json = new JSONObject(inputStreamAsString);
Run Code Online (Sandbox Code Playgroud)

完成!


Hel*_*ail 6

你有没有把它添加到你的清单中

<uses-permission android:name="android.permission.INTERNET" />
Run Code Online (Sandbox Code Playgroud)