java - Java 中的 HttpRequest 发布

标签 java json apache javafx httprequest

在 Java 中创建了新的 API。我想使用此 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 吗??

最佳答案

尝试以下:

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);

关于java - Java 中的 HttpRequest 发布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54166968/

相关文章:

PHP 在 Ubuntu 12.04 上从 5.3 升级到 php 5.6

java - 如何解析带有过滤器的Api

java - 如何下载 Netbeans 6.9 IDE 的源代码

java - 使用java的Hashtable互斥

json - 当我使用 swift 发布 json 时如何解析 http 代码 412 和 200?

wordpress - www.example.com 不重定向到 HTTPS

php - 在 Mac OS X 10.8.3 上激活 Apache/PHP

java - Android Java 套接字与 Socket.IO 通信

android - 我应该选择什么——JSON 还是 SQLite?

python - 从 Morningstar 抓取财务数据