I am working on Parsing JSON data from a website then put in into a string.
When I tried it in my laptop using home internet, I could run it without any errors.
But when I tried to retype the code in my local PC in our office (corporate PC) then ran it, I am getting java.UnknownHostException
error.
Here is my code snippet:
` try {
String url = "https://jsonplaceholder.typicode.com/posts/1";
URL obj = new URL(url);
System.out.println(obj);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// optional default is GET
con.setRequestMethod("GET");
// add request header
con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 5.1; rv:19.0) Gecko/20100101 Firefox/19.0");
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
String final_response = response.toString();
System.out.println(final_response);
} catch (Exception e) {
// TODO: handle exception
System.out.println(e);
}`
Please do note that we have proxy.
Thank you to those who will answer.