「java调第三方接口案例」java三种接口设计

博主:adminadmin 2023-01-18 15:54:06 349

今天给各位分享java调第三方接口案例的知识,其中也会对java三种接口设计进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

JAVA调第三方接口返回XML文件用httpclient实现求大神给指示(新号就5分全给了)要一个工具类,一个用例

以下是一个http接口调用的例子:区别是返回的json数据,xml数据也是一样的,解析下xml数据就可以了,希望对你有帮助

public String getOrderCount() {

String mobile = ServletActionContext.getRequest().getParameter("mobile");

String urlStr = "";

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;

}

java调用第三方接口

参数本身是无顺序的,header在前用于完成握手,完成握手后发送request信息在后。参数名字和文档必须一模一样。这个是websocket协议

java如何使用http方式调用第三方接口?最好有代码~谢谢

星号是IP地址和端口号

public class HttpUtil {

private final static Log log = LogFactory.getLog(HttpUtil.class);

public static String doHttpOutput(String outputStr,String method) throws Exception {

Map map = new HashMap();

String URL = "http://****/interface/http.php" ;

String result = "";

InputStream is = null;

int len = 0;

int tmp = 0;

OutputStream output = null;

BufferedOutputStream objOutput = null;

String charSet = "gbk";

System.out.println("URL of fpcy request");

System.out.println("=============================");

System.out.println(URL);

System.out.println("=============================");

HttpURLConnection con = getConnection(URL);

try {

output = con.getOutputStream();

objOutput = new BufferedOutputStream(output);

objOutput.write(outputStr.getBytes(charSet));

objOutput.flush();

output.close();

objOutput.close();

int responseCode = con.getResponseCode();

if (responseCode == 200) {

is = con.getInputStream();

int dataLen = is.available();

int retry = 5;

while (dataLen == 0 retry 0) {

try {

Thread.sleep(100);

} catch (InterruptedException e) {

}

dataLen = is.available();

retry--;

log.info("未获取到任何数据,尝试重试,当前剩余次数" + retry);

}

log.info("获取到报文单位数据长度:" + dataLen);

byte[] bytes = new byte[dataLen];

while ((tmp = is.read()) != -1) {

bytes[len++] = (byte) tmp;

if (len == dataLen) {

dataLen = bytes.length + dataLen;

byte[] newbytes = new byte[dataLen];

for (int i = 0; i bytes.length; i++) {

newbytes[i] = bytes[i];

}

bytes = newbytes;

}

}

result = new String(bytes, 0, len, charSet);

} else {

String responseMsg = "调用接口失败,返回错误信息:" + con.getResponseMessage() + "(" + responseCode + ")";

System.out.println(responseMsg);

throw new Exception(responseMsg);

}

} catch (IOException e2) {

log.error(e2.getMessage(), e2);

throw new Exception("接口连接超时!,请检查网络");

}

con.disconnect();

System.out.println("=============================");

System.out.println("Contents of fpcy response");

System.out.println("=============================");

System.out.println(result);

Thread.sleep(1000);

return result;

}

private static HttpURLConnection getConnection(String URL) throws Exception {

Map map = new HashMap();

int rTimeout = 15000;

int cTimeout = 15000;

String method = "";

method = "POST";

boolean useCache = false;

useCache = false;

HttpURLConnection con = null;

try {

con = (HttpURLConnection) new URL(URL).openConnection();

} catch (IOException e) {

log.error(e.getMessage(), e);

throw new Exception("URL不合法!");

}

try {

con.setRequestMethod(method);

} catch (ProtocolException e) {

log.error(e.getMessage(), e);

throw new Exception("通信协议不合法!");

}

con.setConnectTimeout(cTimeout);

con.setReadTimeout(rTimeout);

con.setUseCaches(useCache);

con.setDoInput(true);

con.setDoOutput(true);

log.info("当前连接信息: URL:" + URL + "," + "Method:" + method

+ ",ReadTimeout:" + rTimeout + ",ConnectTimeOut:" + cTimeout

+ ",UseCaches:" + useCache);

return con;

}

public static void main(String[] args) throws Exception {

String xml="?xml version=\"1.0\" encoding=\"GBK\" ?documenttxcode101/txcodenetnumber100001/netnumber........./document";

response=HttpUtil.doHttpOutput(xml, "post");

JSONObject json= JSONObject.parseObject(response);

String retcode=json.getString("retcode");

调用这个类就能获得返回的参数。。over.

}

}

}

关于java调第三方接口案例和java三种接口设计的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。