【发布时间】:2017-02-21 23:58:58
【问题描述】:
我目前正在处理一个 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',但它只是用于控制器,我使用的方法不是控制器。我真的希望有人可以指导我。先感谢您。
【问题讨论】:
标签: java web-services maven httpclient http-get