【问题标题】:HttpRequest Post in JavaJava 中的 HttpRequest 发布
【发布时间】:2019-01-13 07:41:43
【问题描述】:

在 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 吗??

【问题讨论】:

    标签: java json apache javafx httprequest


    【解决方案1】:

    试试下面:

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

    【讨论】:

    • 感谢您的回答但是当我尝试实现您的代码时,它会在代码的第一行给出提示 CloseableHttpClient client = HttpClientBuilder.create().build();
    • @JahongirSabirov CloseableHttpClient 是一个实现 HttpClient 的类。你试过了吗?让我知道您现在遇到的错误。
    • 是的,先生,它以某种方式工作,但我仍然无法获得发布 httpRequest 的结果。我的意思是我的 javaFX 应用程序一直在加载。服务器中没有任何反应
    • @AdityaNarayanDixt 获取此请求的请求是什么?
    猜你喜欢
    • 2011-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-15
    • 1970-01-01
    • 2011-10-03
    • 2015-04-20
    相关资源
    最近更新 更多