Создан новый API в Java. Я хочу использовать метод этого API, такой как post, get, но не может работать. Не могли бы вы дать мне подсказку, как использовать другой способ.
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();
}
}
});
Но я получаю следующие ошибки
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)
Пожалуйста, не могли бы вы дать мне подсказку? Могу ли я сделать HTTPRequest другим способом ??