如何异步调用jersy post方法?

时间:2017-03-07 22:17:27

标签: java jersey

如何异步拨打电话(jersy)?我不想从此网址获得任何回复

public void callAPI(String url,int Id,long se_eln,String reportName String ,String startDate, String endDate){
                Map map= new HashMap();
                map.put("Id", Id);
                map.put("reportName",reportNameString);
                map.put("startDate", startDate);
                map.put("endDate", endDate);
                map.put("accountId", se_eln);
                try {
                    //System.out.println("calling post method");
                    String str = new ObjectMapper().writeValueAsString(dataMismatchMap);
    //
                    //PostMethod postMethod = new PostMethod(url);
                    PostMethod postMethod = new PostMethod(url);
                    RequestEntity requestEntity = new StringRequestEntity(str);
                    postMethod.setRequestEntity(requestEntity);
                    postMethod.setRequestHeader("Content-Type", "application/json;charset=utf-8");

                    HttpClient httpclient = new HttpClient();
                    int result = httpclient.executeMethod(postMethod);
                    //System.out.println("result is "+result);
                    webre
                } catch (MalformedURLException e) {
                    e.printStackTrace();

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

如何异步拨打电话(jersy)?我不想从这个网址得到任何回复

2 个答案:

答案 0 :(得分:0)

我认为有办法在apache http客户端进行异步调用,因为我可以看到你已经在使用它了。最简单的是,您可以将请求调用放在简单的线程中,让它执行。如果我发现通过http客户端进行异步调用将更新答案...

答案 1 :(得分:0)

这是for get,但你可以修改为使用post strong .rx()。get(); by rx()。post(...);

   rx.Observable<Response> observable = Rx.newClient(RxObservableInvoker.class)
//              .target("http://javaresteasydemo-ravikant.rhcloud.com/rest/hello/getDataNoZip/")
                .target("http://jerseyexample-ravikant.rhcloud.com/rest/jws/getDataAsClient")
                .register(JacksonFeature.class).request().header("key", "12345").rx().get();

        observable.subscribe(new Action1<Response>() {

            @Override
            public void call(Response response) {
                try {
                    System.out.println(" Inside call ");
                    // System.out.println(response.readEntity(List.class));
                    //List<java.util.LinkedHashMap> lst = response.readEntity(List.class);
                    ObjectMapper ob = new ObjectMapper();
                    List<User> pojos = ob.convertValue(response.readEntity(List.class), new TypeReference<List<User>>() {
                    });
                    for (User user : pojos) {
                        System.out.println(user.getPost());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                System.exit(0);
            }

        });