Press enter to see results or esc to cancel.


JAVA-Send URL HTTP Request and Read JSON Response

In this video I have shown how to send a URL HTTP request using JAVA.

For testing I used a site http://fixer.io/ through which we can send URL request and get JSON response of current currency rate.

To read json Response you will have to add java-jason.jar to class path.

1. The JSON Response through browser.


2. Formatted JSON response.

{
 "base": "EUR",
 "date": "2017-09-29",
 "rates": {
 "AUD": 1.5075,
 "BGN": 1.9558,
 "BRL": 3.7635,
 "CAD": 1.4687,
 "CHF": 1.1457,
 "CNY": 7.8534,
 "CZK": 25.981,
 "DKK": 7.4423,
 "GBP": 0.88178,
 "HKD": 9.2214,
 "HRK": 7.495,
 "HUF": 310.67,
 "IDR": 15889.0,
 "ILS": 4.1591,
 "INR": 77.069,
 "JPY": 132.82,
 "KRW": 1351.8,
 "MXN": 21.461,
 "MYR": 4.9827,
 "NOK": 9.4125,
 "NZD": 1.6354,
 "PHP": 60.05,
 "PLN": 4.3042,
 "RON": 4.5993,
 "RUB": 68.252,
 "SEK": 9.649,
 "SGD": 1.6031,
 "THB": 39.338,
 "TRY": 4.2013,
 "USD": 1.1806,
 "ZAR": 15.944
 }
}

3.Test_URL_Req.java without URL Parameters

package URL_Req;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.json.JSONObject;
public class Test_URL_Req {
public static void main(String[] args) {
try {
  String url = "http://api.fixer.io/latest";
  URL obj = new URL(url);
  HttpURLConnection con = (HttpURLConnection) obj.openConnection();
  int responseCode = con.getResponseCode();
  System.out.println("\nSending 'GET' request to URL : " + url);
  System.out.println("Response Code : " + responseCode);
  BufferedReader in =new BufferedReader(
  new InputStreamReader(con.getInputStream()));
  String inputLine;
  StringBuffer response = new StringBuffer();
   while ((inputLine = in.readLine()) != null) {
     response.append(inputLine);
   } in .close();
   //print in String
   // System.out.println(response.toString());
   JSONObject myresponse = new JSONObject(response.toString());
   System.out.println(myresponse);
   System.out.println("base -" + myresponse.getString("base"));
   System.out.println("date -" + myresponse.getString("date"));
   JSONObject rates_object = new JSONObject(myresponse.getJSONObject("rates").toString());
   System.out.println("rates -" + rates_object);
   System.out.println("AUD -" + rates_object.getDouble("AUD"));
   System.out.println("BGN -" + rates_object.getDouble("BGN"));
   System.out.println("BRL -" + rates_object.getDouble("BRL"));
    System.out.println("CAD -" + rates_object.getDouble("CAD"));
  } catch(Exception e) {
    System.out.println(e);
  }
 }
}

Console OutPut

Sending 'GET' request to URL : http://api.fixer.io/latest
Response Code : 200
{"base":"EUR","rates":{"CNY":7.8534,"JPY":132.82,"CZK":25.981,"RON":4.5993,"MXN":21.461,"CAD":1.4687,"ZAR":15.944,"AUD":1.5075,"NZD":1.6354,"GBP":0.88178,"ILS":4.1591,"NOK":9.4125,"CHF":1.1457,"RUB":68.252,"INR":77.069,"THB":39.338,"IDR":15889,"TRY":4.2013,"SGD":1.6031,"HKD":9.2214,"HRK":7.495,"DKK":7.4423,"SEK":9.649,"MYR":4.9827,"BRL":3.7635,"BGN":1.9558,"HUF":310.67,"PHP":60.05,"PLN":4.3042,"USD":1.1806,"KRW":1351.8},"date":"2017-09-29"}
base -EUR
date -2017-09-29
rates -{"JPY":132.82,"CNY":7.8534,"RON":4.5993,"CZK":25.981,"MXN":21.461,"CAD":1.4687,"NZD":1.6354,"AUD":1.5075,"ZAR":15.944,"GBP":0.88178,"NOK":9.4125,"ILS":4.1591,"CHF":1.1457,"RUB":68.252,"INR":77.069,"THB":39.338,"IDR":15889,"TRY":4.2013,"SGD":1.6031,"HKD":9.2214,"HRK":7.495,"DKK":7.4423,"SEK":9.649,"MYR":4.9827,"BRL":3.7635,"BGN":1.9558,"PHP":60.05,"HUF":310.67,"PLN":4.3042,"USD":1.1806,"KRW":1351.8}


AUD -1.5075
BGN -1.9558
BRL -3.7635
CAD -1.4687

4.Test_URL_Req.java with URL Parameters.

 String url = "http://api.fixer.io/latest?base=USD";

If you have more URL Parameters you can pass like,

 String url = "http://api.fixer.io/latest?base=USD&parameter1=value1&parameter2=value2";

Comments

Leave a Comment