웹 사이트에서 데이터를 요청할 수있는 샘플 코드가 있는데, 내가 얻은 응답은 횡설수설적인 것으로 판명되었습니다.JSON 응답을 JSON 객체로 변환합니다.
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 NetClientGet
{
public static void main(String[] args)
{
try
{
URL url = new URL("http://fids.changiairport.com/webfids/fidsp/get_flightinfo_cache.php?d=0&type=pa&lang=en");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
if (conn.getResponseCode() != 200)
{
throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
}
System.out.println("the connection content type : " + conn.getContentType());
// convert the input stream to JSON
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null)
{
System.out.println(output);
}
conn.disconnect();
} catch (MalformedURLException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
어떻게 읽을 JSON 개체에의 InputStream를 변환 않습니다. 몇 가지 질문을 찾았지만 이미 응답을하고 분석하려고합니다.
대신 conn.getOutputStream()을 사용 하시겠습니까? – softwarebear