59 lines
1.4 KiB
Java
59 lines
1.4 KiB
Java
|
|
package mmk;
|
||
|
|
|
||
|
|
import java.io.BufferedReader;
|
||
|
|
import java.io.IOException;
|
||
|
|
import java.io.InputStreamReader;
|
||
|
|
import java.net.HttpURLConnection;
|
||
|
|
import java.net.MalformedURLException;
|
||
|
|
import java.net.URL;
|
||
|
|
|
||
|
|
public class JsonConn2 {
|
||
|
|
|
||
|
|
public static void main(String[] args) {
|
||
|
|
|
||
|
|
HttpURLConnection conn = null;
|
||
|
|
BufferedReader reader;
|
||
|
|
String line;
|
||
|
|
StringBuffer responceContent = new StringBuffer();
|
||
|
|
|
||
|
|
try {
|
||
|
|
URL url = new URL("https://random-word-api.herokuapp.com/word?number=10");
|
||
|
|
|
||
|
|
conn = (HttpURLConnection) url.openConnection();
|
||
|
|
conn.setRequestMethod("GET");
|
||
|
|
conn.setConnectTimeout(5000);
|
||
|
|
conn.setReadTimeout(5000);
|
||
|
|
|
||
|
|
int status = conn.getResponseCode();
|
||
|
|
|
||
|
|
if(status > 299) {
|
||
|
|
reader = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
|
||
|
|
while((line = reader.readLine()) != null) {
|
||
|
|
responceContent.append(line);
|
||
|
|
}
|
||
|
|
reader.close();
|
||
|
|
}
|
||
|
|
else {
|
||
|
|
reader = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
|
||
|
|
while((line = reader.readLine()) != null) {
|
||
|
|
responceContent.append(line);
|
||
|
|
}
|
||
|
|
reader.close();
|
||
|
|
}
|
||
|
|
|
||
|
|
System.out.println(responceContent.toString());
|
||
|
|
|
||
|
|
} catch (MalformedURLException e) {
|
||
|
|
// TODO Auto-generated catch block
|
||
|
|
e.printStackTrace();
|
||
|
|
} catch (IOException e) {
|
||
|
|
// TODO Auto-generated catch block
|
||
|
|
e.printStackTrace();
|
||
|
|
}
|
||
|
|
finally {
|
||
|
|
conn.disconnect();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|