Untitled
unknown
plain_text
5 years ago
2.0 kB
9
Indexable
public String reviewMovie(String serviceUrl, String movieId, String review) throws JsonProcessingException, UnsupportedEncodingException, IOException
{
String url = serviceUrl + "/" + movieId;
// tworzymy obiekt klienta
HttpClient client = HttpClientBuilder.create().build();
// mówimy, że chcemy wysłać żadanie POST
HttpPost post = new HttpPost(url);
// ustawiamy nagłówek content-type
post.setHeader("Content-type", "text/plain");
// serializujemy zapodaną w argumencie piosenkę do JSONa
ObjectMapper mapper = new ObjectMapper();
String newReview = mapper.writeValueAsString(review);
// ustawiamy JSONa jako ciało żądania
post.setEntity(new StringEntity(review));
// wysyłamy żądanie. odpowiedź trafia do reqResult
HttpResponse reqResult = client.execute(post);
BufferedReader rd = new BufferedReader(
new InputStreamReader(reqResult.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
// z reqResult pobieramy nagłówek Location
return result.toString();
}
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
String movieId = fieldId.getText();
try {
String review = fieldReview.getText();
String result = reviewMovie("http://localhost:8080/SentAnalysisService/webresources/generic/movieReview", movieId, review);
JOptionPane.showMessageDialog(null, result);
fieldId.setText("");
fieldReview.setText("");
} catch (IOException ex) {
Logger.getLogger(InformationWindow.class.getName()).log(Level.SEVERE, null, ex);
}
}
Editor is loading...