Friday, 28 August 2020

Simple java http get example java net http get


Here I am calling simple java http get method and passing parameter with url 

url suppose : https://jsonmock.anywebsite.com/api/movies/search/?Title=spiderman

here Title is parameter that I will pass to with my url

anywebsite is replaced with the name of code testing web site, hence this url will not work

but if I hit correct url in chrome (as it is simple get request no need of postman or insomnia tool)

it would return below result

{"page":1,"per_page":10,"total":13,"total_pages":2,"data":[{"Title":"Italian Spiderman","Year":2007,"imdbID":"tt2705436"},{"Title":"Superman, Spiderman or Batman","Year":2011,"imdbID":"tt2084949"},{"Title":"Spiderman","Year":1990,"imdbID":"tt0100669"},{"Title":"Spiderman","Year":2010,"imdbID":"tt1785572"},{"Title":"Fighting, Flying and Driving: The Stunts of Spiderman 3","Year":2007,"imdbID":"tt1132238"},{"Title":"Spiderman and Grandma","Year":2009,"imdbID":"tt1433184"},{"Title":"The Amazing Spiderman T4 Premiere Special","Year":2012,"imdbID":"tt2233044"},{"Title":"Amazing Spiderman Syndrome","Year":2012,"imdbID":"tt2586634"},{"Title":"Hollywood's Master Storytellers: Spiderman Live","Year":2006,"imdbID":"tt2158533"},{"Title":"Spiderman 5","Year":2008,"imdbID":"tt3696826"}]}

Data and   OuterObj is the class according to this json data type.


I have maven java project 

this code would work with this dependency

Please add this dependency in your pom.xml


<dependency>

<groupId>com.google.code.gson</groupId>

<artifactId>gson</artifactId>

<version>2.8.5</version>

</dependency>


In this code I haven't did null checking, please do null checking


package org.hacker.rank.test;


import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.Reader;

import java.net.MalformedURLException;

import java.net.URL;

import java.util.List;


import com.google.gson.Gson;


public class HttpGet {


public static void main(String[] args) {

func("spiderman");

}


static void func(String substr) {

try {

//calling url and passing substr as parameter

URL url = new URL("https://jsonmock.hackerrank.com/api/movies/search/?Title="+substr);

//get inputstream

InputStream input = url.openStream();

//taking it into reader 

Reader reader = new InputStreamReader(input, "UTF-8");

//converting reader into our desire object

//after this object we can use it according to us

                OuterObj outerObj = new Gson().fromJson(reader, OuterObj.class);

List<Data> data = outerObj.getData();

System.out.println(outerObj);

catch (MalformedURLException e) {

e.printStackTrace();

catch (IOException e) {

e.printStackTrace();

}

return;

}


}




class Data{

private String Title;

private int Year;

private String imdbID;

public Data() {

//empty constructor

}

public String getTitle() {

return Title;

}

public void setTitle(String title) {

Title = title;

}

public int getYear() {

return Year;

}

public void setYear(int year) {

Year = year;

}

public String getImdbID() {

return imdbID;

}

public void setImdbID(String imdbID) {

this.imdbID = imdbID;

}

@Override

public String toString() {

return "Title: "+Title+"     Year: "+Year+"     imdbID: "+imdbID;

}

}


class OuterObj{

private int page;

private int per_page;

private int total;

private List<Data> data;

public OuterObj() {

//empty constructor

}


public int getPage() {

return page;

}

public void setPage(int page) {

this.page = page;

}

public int getPer_page() {

return per_page;

}

public void setPer_page(int per_page) {

this.per_page = per_page;

}

public int getTotal() {

return total;

}

public void setTotal(int total) {

this.total = total;

}

public List<Data> getData() {

return data;

}

public void setData(List<Data> data) {

this.data = data;

}

@Override

public String toString() {

return "page: "+page+"     per_page: "+per_page+"     total: "+total+"     data: "+data;

}

}


I was failed to complete this task in stipulated time, hope it would help me or others in future.

Thanks