JAVA调第三方接口返回XML文件用httpclient实现求大神给指示(新号就5分全给了)要一个工具类,一个用例
1个回答
展开全部
以下是一个http接口调用的例子:区别是返回的json数据,xml数据也是一样的,解析下xml数据就可以了,希望对你有帮助
public String getOrderCount() {
String mobile = ServletActionContext.getRequest().getParameter("mobile");
String urlStr = "http://127.0.0.1/OderServer/orderCount.htm";
urlStr += "?telphone=" + mobile + "&endTime=" + DateUtil.getCurrentDay("yyyyMMdd") + "&timeLength=2";
URL url = null;
HttpURLConnection httpurlconnection = null;
try {
url = new URL(urlStr);
// 以post方式请求
httpurlconnection = (HttpURLConnection) url.openConnection();
httpurlconnection.setDoOutput(true);
httpurlconnection.setRequestMethod("POST");
httpurlconnection.getOutputStream().flush();
httpurlconnection.getOutputStream().close();
// 获取响应代码
//int code = httpurlconnection.getResponseCode();
//System.out.println("code " + code);
// 获取页面内容
java.io.InputStream in = httpurlconnection.getInputStream();
java.io.BufferedReader breader = new BufferedReader(
new InputStreamReader(in, "utf-8"));
StringBuffer result = new StringBuffer();
String str = breader.readLine();
while (str != null) {
result.append(str);
str = breader.readLine();
}
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/json;charset=utf-8");
response.getWriter().print(result);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (httpurlconnection != null)
httpurlconnection.disconnect();
}
return null;
}
public String getOrderCount() {
String mobile = ServletActionContext.getRequest().getParameter("mobile");
String urlStr = "http://127.0.0.1/OderServer/orderCount.htm";
urlStr += "?telphone=" + mobile + "&endTime=" + DateUtil.getCurrentDay("yyyyMMdd") + "&timeLength=2";
URL url = null;
HttpURLConnection httpurlconnection = null;
try {
url = new URL(urlStr);
// 以post方式请求
httpurlconnection = (HttpURLConnection) url.openConnection();
httpurlconnection.setDoOutput(true);
httpurlconnection.setRequestMethod("POST");
httpurlconnection.getOutputStream().flush();
httpurlconnection.getOutputStream().close();
// 获取响应代码
//int code = httpurlconnection.getResponseCode();
//System.out.println("code " + code);
// 获取页面内容
java.io.InputStream in = httpurlconnection.getInputStream();
java.io.BufferedReader breader = new BufferedReader(
new InputStreamReader(in, "utf-8"));
StringBuffer result = new StringBuffer();
String str = breader.readLine();
while (str != null) {
result.append(str);
str = breader.readLine();
}
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/json;charset=utf-8");
response.getWriter().print(result);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (httpurlconnection != null)
httpurlconnection.disconnect();
}
return null;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询