Java'da dinlenme hizmeti araması yapmaya çalışıyorum. Web ve dinlenme hizmetinde yeniyim. Yanıt olarak jsonu döndüren dinlenme servisim var. Aşağıdaki koda sahibim ama eksik olduğunu düşünüyorum çünkü json kullanarak çıktının nasıl işleneceğini bilmiyorum.Java'da Rest çağrısının bir parçası olarak JSON yanıtı alma
public static void main(String[] args) {
try {
URL url = new URL("http://xyz.com:7000/test/db-api/processor");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("PUT");
connection.setRequestProperty("Content-Type", "application/json");
OutputStream os = connection.getOutputStream();
//how do I get json object and print it as string
os.flush();
connection.getResponseCode();
connection.disconnect();
} catch(Exception e) {
throw new RuntimeException(e);
}
}
Lütfen yardım edin. Dinlenme hizmetleri ve json için yeniyim. Şimdiden çok teşekkürler.
OutputStream os = conn.getOutputStream();
os.write(input.getBytes()); // The input you need to pass to the webservice
os.flush();
...
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream()))); // Getting the response from the webservice
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output); // Instead of this, you could append all your response to a StringBuffer and use `toString()` to get the entire JSON response as a String.
// This string json response can be parsed using any json library. Eg. GSON from Google.
}
webservices isabet üzerinde daha net fikir sahibi olmak
this göz at: Bu yana
, bu hayatınızı kolaylaştırır. Bir HTTP isteği gerçekleştirecek, HTTP yanıtını seçtiğiniz bir nesne türüne dönüştürecek ve bu nesneyi döndürecektir. https://spring.io/blog/2009/03/27/rest-in-spring-3-resttemplate –