Android Studio:如何在 HTTPS 页面上获取 JSON 文件

Android Studio: How can take JSON file on the HTTPS page

我使用此代码从 Internet 获取 XML 或 JSON,但它只能在 HTTP 上运行。所以它不能像 Google api

那样在 HTTPS 上工作

https://ajax.googleapis.com/ajax/services/search/news?v=1.0&q=barack%20obama

Error 405!

private String getXmlFromUrl(String urlString) {
    String xml = null;
    try {      
        DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(urlString);
        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        xml = EntityUtils.toString(httpEntity, HTTP.UTF_8);          
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    // return XML
    return xml;
}

您需要将请求方法更改为 GET。

您需要像这样更改您的代码: `

private String getXmlFromUrl(String urlString) {
String xml = null;
try {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    //url request is get
    HttpGet httpGet = new HttpGet(urlString);

    HttpResponse httpResponse = httpClient.execute(httpGet);
    HttpEntity httpEntity = httpResponse.getEntity();
    xml = EntityUtils.toString(httpEntity, HTTP.UTF_8);

} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
} catch (ClientProtocolException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
// return XML
return xml; }