java调用接口api并返回数据
get方法
import com.alibaba.fastjson.jsonobject; import edu.zhku.fire_ant_project.config.wxconstant; import org.apache.http.httpresponse; import org.apache.http.httpstatus; import org.apache.http.client.httpclient; import org.apache.http.client.methods.httpget; import org.apache.http.client.methods.httppost; import org.apache.http.entity.stringentity; import org.apache.http.impl.client.httpclients; import org.apache.http.util.entityutils; public class httpcallotherinterfaceutils { public static void main(string args[]) { httpclient client = httpclients.createdefault(); // 要调用的接口方法 string url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+ wxconstant.appid +"&secret="+wxconstant.secret; httpget httpget=new httpget(url); jsonobject jsonobject = null; try { httpresponse res = client.execute(httpget); if (res.getstatusline().getstatuscode() == httpstatus.sc_ok) { // 返回json格式: jsonobject = jsonobject.parseobject(entityutils.tostring(res.getentity())); system.out.println(jsonobject); } } catch (exception e) { system.out.println("服务间接口调用出错!"); e.printstacktrace(); } } }
post方法
import com.alibaba.fastjson.jsonobject; import org.apache.http.httpresponse; import org.apache.http.httpstatus; import org.apache.http.client.httpclient; import org.apache.http.client.methods.httppost; import org.apache.http.entity.stringentity; import org.apache.http.impl.client.httpclients; import org.apache.http.util.entityutils; public class httpcallotherinterfaceutils { public static string callotherinterface(jsonobject jsonparam, string port, string posturl) { httpclient client = httpclients.createdefault(); // 要调用的接口方法 string url = "http://localhost:" + port + posturl; httppost post = new httppost(url); jsonobject jsonobject = null; try { stringentity s = new stringentity(jsonparam.tostring(), "utf-8"); //s.setcontentencoding("utf-8");//此处测试发现不能单独设置字符串实体的编码,否则出错!应该在创建对象时创建 s.setcontenttype("application/json"); post.setentity(s); post.addheader("content-type", "application/json;charset=utf-8"); httpresponse res = client.execute(post); if (res.getstatusline().getstatuscode() == httpstatus.sc_ok) { // 返回json格式: jsonobject = jsonobject.parseobject(entityutils.tostring(res.getentity())); } } catch (exception e) { system.out.println("服务间接口调用出错!"); e.printstacktrace(); //throw new runtimeexception(e); } return jsonobject.tostring(); } }
java跨服务调用接口
java程序跨服务调用接口,通常可以使用以下方式:
- restful api:通过http协议进行通信,使用restful api调用其他服务的接口。
- rpc:使用远程过程调用(rpc)框架,如dubbo、grpc等,通过序列化和反序列化技术实现跨服务调用。
- 消息队列:使用消息队列,如kafka、rabbitmq等,服务之间通过消息队列进行异步通信。
- http客户端:使用java内置的http客户端,如httpurlconnection、apache httpclient等,通过http协议调用其他服务的接口。
无论使用哪种方式,都需要了解其他服务的接口定义和调用方式,以及网络通信的安全性和稳定性等方面的考虑。同时,需要注意接口版本的兼容性和错误处理等问题。
这里提供一个使用java内置的httpurlconnection进行post请求的示例代码:
import java.io.bufferedreader; import java.io.dataoutputstream; import java.io.ioexception; import java.io.inputstreamreader; import java.net.httpurlconnection; import java.net.url; public class httppostexample { private static final string post_url = "http://engine-server-host:port/api/client/data/push"; private static final string user_agent = "mozilla/5.0"; public static void main(string[] args) throws ioexception { url obj = new url(post_url); httpurlconnection con = (httpurlconnection) obj.openconnection(); // 添加请求头 con.setrequestmethod("post"); con.setrequestproperty("user-agent", user_agent); con.setrequestproperty("accept-language", "en-us,en;q=0.5"); // 设置post请求体 string postbody = "your_post_body_here"; con.setdooutput(true); try (dataoutputstream wr = new dataoutputstream(con.getoutputstream())) { wr.writebytes(postbody); wr.flush(); } // 发送post请求并获取响应 int responsecode = con.getresponsecode(); system.out.println("post response code :: " + responsecode); try (bufferedreader in = new bufferedreader(new inputstreamreader(con.getinputstream()))) { string inputline; stringbuilder response = new stringbuilder(); while ((inputline = in.readline()) != null) { response.append(inputline); } system.out.println("post response :: " + response.tostring()); } } }
在代码中,需要将post_url替换为引擎端的api接口地址,将postbody替换为要发送的post请求体。
需要注意的是,这里的post请求体需要按照引擎端api接口的要求进行格式化。
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论