本文目录一览:
如何用Java调用别人API接口
java发一个http请求过去,带上参数就可以了啊,跟我们在浏览器上访问资源是一样的 只是它返回的是json格式的数据而已
给你两个方法吧:
public static String do_post(String url, ListNameValuePair name_value_pair) throws IOException {
String body = "{}";
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
HttpPost httpost = new HttpPost(url);
httpost.setEntity(new UrlEncodedFormEntity(name_value_pair, StandardCharsets.UTF_8));
HttpResponse response = httpclient.execute(httpost);
HttpEntity entity = response.getEntity();
body = EntityUtils.toString(entity);
} finally {
httpclient.getConnectionManager().shutdown();
}
return body;
}
public static String do_get(String url) throws ClientProtocolException, IOException {
String body = "{}";
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
HttpGet httpget = new HttpGet(url);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
body = EntityUtils.toString(entity);
} finally {
httpclient.getConnectionManager().shutdown();
}
return body;
}
怎么使用java调用api接口
java发一个http请求过去,带上参数就可以了啊,跟我们在浏览器上访问资源是一样的 只是它返回的是json格式的数据而已给你两个方法吧:public static String do_post(String url, List name_value_pair) throws IOException { String body = "{}"
如何在java中调用api接口
需要导入对应的lib包,然后调用具体的接口以及方法
通过实现http协议进行post或者get请求具体api接口