`
Код: Выделить всё
package somePackage;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import java.net.URI;
public class Httpfetcher {
public static void main(String[] args) {
String urlString = "https://something";
try {
URL url = new URI(urlString).toURL();
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
if (connection.getResponseCode() != 200) {
System.out.println("Error: Unable to fetch data from NIST beacon.");
return;
}
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder responseBuilder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
responseBuilder.append(line);
}
reader.close();
String response = responseBuilder.toString();
JSONParser parser = new JSONParser();
JSONObject jsonResponse = (JSONObject) parser.parse(response);
JSONObject keyValue = (JSONObject) jsonResponse.get("someKey");
String outputValue = (String) keyValue.get("outputValue");
System.out.println("Output Value: " + outputValue);
} catch (Exception e) {
e.printStackTrace();
}
}
}`
Иногда при отладке программы, имеющей http API в коде VS, я перехожу к некоторому файлу встроенного класса Loader.class и выбрасываю исключение ClassNotFoundException для файла, который я пытаюсь бежать. Я проверил переменную среды classpath на своем устройстве, и она содержит все библиотеки и сам файл.
Однако в Eclipse она работает нормально. Проверка URL-адреса с помощью команды curl в командной утилите Windows также дает адекватный результат.
Подробнее здесь: https://stackoverflow.com/questions/792 ... -out-print
Мобильная версия