【问题标题】:Android Studio: How can take JSON file on the HTTPS pageAndroid Studio:如何在 HTTPS 页面上获取 JSON 文件
【发布时间】:2015-12-04 21:20:04
【问题描述】:

我使用此代码从 Internet 获取 XML 或 JSON,但它仅适用于 HTTP。所以它不适用于像 Google api 这样的 HTTPS

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

错误 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;
}

【问题讨论】:

    标签: android json androidhttpclient


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      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; } 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-11-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-30
        相关资源
        最近更新 更多