Search code examples
androidjsonandroidhttpclient

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


I use this code to get XML or JSON from Internet but its just working on HTTP. So It's not working on HTTPS like Google api

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

Solution

  • you need to change your code like this: `

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