0

Hi I have the following cURL

curl -X GET --header "api-key: sOmE-API-kEy" "https://api.data.gov.sg/v1/environment/uv-index?date=2016-06-01"

which I got from the website that provides data for smart nation their website returns proper json data using their forms but i would like to do this via code using an HttpURLConnection so far I am following the code references i found online but it's not working and I couldnt see what's wrong, i get the NetworkOnMainThreadException which I solved by implementing it as an AsyncTask but when I step in android studio debug its now returning with an FileNotFoundException. i also tried using the TrustedManager etc suggested below and it still has the same error. one thing i noticed is when I go to the website and click on the button and look at it in firebug the following is the one that is processessed ...

https://apiconsole-prod.apigee.net/smartdocs/v1/sendrequest?targeturl=https%3A%2F%2Fapi.data.gov.sg%2Fv1%2Fenvironment%2Fuv-index%3Fdate%3D2016-06-01&_=1469245322558

but if i type directly from the browser the response has something like this... steps.oauth.v2.FailedToResolveAPIKey%22%7D%7D%7D", "responseStatusCode": "401"

    URL url = new URL("https://api.data.gov.sg/v1/environment/uv-index?date=2016-06-01");
    urlConn = (HttpURLConnection)url.openConnection();
    urlConn.setRequestMethod("GET");
    urlConn.setDoInput(true);
    urlConn.setDoOutput(true);
    urlConn.setRequestProperty("Content-Type", "application/json");
    urlConn.setRequestProperty("Accept", "application/json");
    urlConn.setRequestProperty("api-key", "sOmE-API-kEy");

    in = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
    String line;
    StringBuilder buffer = new StringBuilder();
    while ((line = in.readLine()) != null) {
        buffer.append(line).append('\n');
    }
    // Chomp the last newline
    buffer.deleteCharAt(buffer.length() - 1);

**note apikey renamed for privacy

so in short looking at the header data from firebug how to i complete my http code implementation

Accept  
*/*
Accept-Encoding 
gzip, deflate, br
Accept-Language 
en-US,en;q=0.5
DNT 
1
Host    
apiconsole-prod.apigee.net
Origin  
https://developers.data.gov.sg
Referer 
https://developers.data.gov.sg/datagovsg-apis/apis/get/environment/uv-index
User-Agent  
Mozilla/5.0 (Windows NT 10.0; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0
api-key: sOmE-API-kEy
debonaire
  • 259
  • 3
  • 10

2 Answers2

0

You must disable Certificate Validation SSL

    TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }
            public void checkClientTrusted(X509Certificate[] certs, String authType) {
            }
            public void checkServerTrusted(X509Certificate[] certs, String authType) {
            }
        }
    };

    // Install the all-trusting trust manager
    SSLContext sc = SSLContext.getInstance("SSL");
    sc.init(null, trustAllCerts, new java.security.SecureRandom());
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

    // Create all-trusting host name verifier
    HostnameVerifier allHostsValid = new HostnameVerifier() {
        public boolean verify(String hostname, SSLSession session) {
            return true;
        }
    };

    // Install the all-trusting host verifier
    HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);

    URL url = new URL("https://www.nakov.com:2083/");
    URLConnection con = url.openConnection();
    Reader reader = new InputStreamReader(con.getInputStream());
    while (true) {
        int ch = reader.read();
        if (ch==-1) {
            break;
        }
        System.out.print((char)ch);
    }
Quynh Nguyen
  • 2,773
  • 2
  • 12
  • 25
0

Solved with the following...

You should probably have urlConn.setDoOutput(false); for a GET request – Yuri Schimke 3 mins ago

Thanks so much for all the suggestions!

debonaire
  • 259
  • 3
  • 10