我目前正在研究Java应用程序项目。这是一个现有的应用程序,我被要求对其进行修改。他们说我必须制作一个调用另一个应用程序(调用是url)的Web服务,然后从我正在修改的应用程序的ini文件中向其传递数据。我对这种东西还很陌生,我真的在家中有人可以帮助我。因此,这是写入ini文件的代码:

               common.writeIniFileIdentify("PV-ID", PVIDNo);
               common.writeIniFileIdentify("PALMUS-ID", SerialNo);


然后将其转换为字符串:

               Properties p = new Properties();
               p.load(new FileInputStream("C:/PALMUS-PV/PVInfo.ini"));
               String pvid = p.getProperty("PV-ID");
               String palmusid = p.getProperty("PALMUS-ID");
               System.out.println(pvid);
               System.out.println(palmusid);

               this.sendPVDetails(pvid, palmusid); //this will pass the data to sendPVDetails method


这是我使用的HttpGet(在互联网上看到了):

public void sendPVDetails(String pvid, String palmusid) {
      try {

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet getRequest = new HttpGet(
                "https://url.of.the.another.application");

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("PV-ID", pvid);
        jsonObject.put("PALMUS-ID", palmusid);

        getRequest.addHeader("accept", "application/json");

        HttpResponse response = httpClient.execute(getRequest);

        if (response.getStatusLine().getStatusCode() != 200) {
            throw new RuntimeException("Failed : HTTP error code : "
               + response.getStatusLine().getStatusCode());
        }

        BufferedReader br = new BufferedReader(
                         new InputStreamReader((response.getEntity().getContent())));

        String output;
        System.out.println("Output from Server .... \n");
        while ((output = br.readLine()) != null) {
            System.out.println(output);
        }

        httpClient.getConnectionManager().shutdown();

      } catch (ClientProtocolException e) {

        e.printStackTrace();

      } catch (IOException e) {

        e.printStackTrace();
      }

    }


我对HttpGet的工作方式有些困惑,因为这是我第一次看到这种代码,而Java对我来说也是新的。他们说我必须使用“ return ResponseEntity”,但这仅用于控制器,而我使用的方法不是控制器。我真的希望有人可以指导我。先感谢您。

最佳答案

HTTP GET方法用于从给定URI检索内容。由于我们仅假设在URI上调用GET,因此不会涉及任何内容。为了将某些内容发布(发送)到给定的URI,您需要使用HTTP POST。您可以使用HttpPost以满足您的要求。Here是使用http客户端库的一个很好的示例。

09-30 17:15
查看更多