Reputation: 470
Created new API in java. I want to use this API's method like post,get methods but could'nt work.Could you give me hint to use other way to do it.
URI url = URI.create("http://localhost:8080/departure/");
System.out.println("koraylikchi");
HttpPost httpPost = new HttpPost(url);
JSONObject jsonObject = new JSONObject();
jsonObject.put("departDate",dateChooser.getPromptText());
jsonObject.put("time",timeField.getText());
jsonObject.put("flight",flightField.getText());
jsonObject.put("destination",destField.getText());
jsonObject.put("statusTime",statusTimeField.getText());
jsonObject.put("terminal",terminalField.getText());
try {
StringEntity se = new StringEntity(jsonObject.toString());
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));
httpPost.setEntity(se);
HttpClient client = new HttpClient();
int response = client.executeMethod((HttpMethod) httpPost);
System.out.println(response);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
But I get following errors
Exception in thread "JavaFX Application Thread" java.lang.ClassCastException: org.apache.http.client.methods.HttpPost cannot be cast to org.apache.commons.httpclient.HttpMethod
at controllers.AddDialogDepatureController.lambda$onClick$7(AddDialogDepatureController.java:190)
at controllers.AddDialogDepatureController$$Lambda$413/1162975584.handle(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8216)
at javafx.scene.control.Button.fire(Button.java:185)
Please could you give me hint? Can I do HTTPRequest in other way??
Upvotes: 3
Views: 1075
Reputation: 2119
Try below:
HttpClient client = HttpClientBuilder.create().build();
HttpPost postRequest = new HttpPost("http://localhost:8080/departure/");
JSONObject jsonObject = new JSONObject();
jsonObject.put("departDate",dateChooser.getPromptText());
jsonObject.put("time",timeField.getText());
jsonObject.put("flight",flightField.getText());
jsonObject.put("destination",destField.getText());
jsonObject.put("statusTime",statusTimeField.getText());
jsonObject.put("terminal",terminalField.getText());
StringEntity se = new StringEntity(jsonObject.toString());
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));
postRequest.setEntity(se);
HttpResponse response = client.execute(postRequest);
Upvotes: 2