nl.raja.niruraji.pa.PlayGround.java Source code

Java tutorial

Introduction

Here is the source code for nl.raja.niruraji.pa.PlayGround.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package nl.raja.niruraji.pa;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

/**
 * 
 * @author Rajaram Kaliyaperumal
 * @since 2016-05-09
 * @version 0.1
 */
public class PlayGround {

    public static void main(String[] args) {

        try {
            // create HTTP Client         
            HttpClient httpClient = HttpClientBuilder.create().build();
            String url = "http://buienradar.nl/Json/GetTwentyFourHourForecast?geolocationid=2751773";

            // Create new getRequest with below mentioned URL         
            HttpGet getRequest = new HttpGet(url);

            // Add additional header to getRequest which accepts application/xml data         
            //getRequest.addHeader("accept", "application/xml"); 

            // Execute your request and catch response         
            HttpResponse response = httpClient.execute(getRequest);

            // Check for HTTP response code: 200 = success         
            if (response.getStatusLine().getStatusCode() != 200) {
                throw new RuntimeException(
                        "Failed : HTTP error code : " + response.getStatusLine().getStatusCode());

            }

            String result = EntityUtils.toString(response.getEntity());
            JSONObject myObject = new JSONObject(result);

            // Get-Capture Complete application/xml body response

            BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));

            String output;

            System.out.println("============Output:============");

            // Simply iterate through XML response and show on console.

            while ((output = br.readLine()) != null) {

                System.out.println(output);

            }

        } catch (ClientProtocolException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

}