导航:首页 > 编程语言 > java访问api

java访问api

发布时间:2022-08-08 23:41:30

⑴ 如何用java调用别人API接口

java发一个http请求过去,带上参数就可以了啊,跟我们在浏览器上访问资源是一样的 只是它返回的是json格式的数据而已
给你两个方法吧:
public static String do_post(String url, List<NameValuePair> 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怎么调用啊

1,java中的api并不能直接调用的,常说的调用api其实是调用相关的类和接口,这些类和接口里包含这些api。

2,比如要计算一个值的绝对值、平方根、指数...
那么就需要调用Java API中的java.lang.Math类中的abs(); sqrt(); exp();的这些方法。
Math.abs();
Math.sqrt();
Math.exp();
Math是静态的,不需要创建Math对象,所以可以直接用【类.方法】调用。
如果是其它非静态的类,就需要【对象.方法】的方式调用。

⑶ java中怎么调用api数据接口

java发一个http请求过去,带上参数就可以了啊,跟我们在浏览器上访问资源是一样的只是它返回的是json格式的数据而已
给你两个方法吧:
publicstaticStringdo_post(Stringurl,List<NameValuePair>name_value_pair)throwsIOException{
Stringbody="{}";
DefaultHttpClienthttpclient=newDefaultHttpClient();
try{
HttpPosthttpost=newHttpPost(url);
httpost.setEntity(newUrlEncodedFormEntity(name_value_pair,StandardCharsets.UTF_8));
HttpResponseresponse=httpclient.execute(httpost);
HttpEntityentity=response.getEntity();
body=EntityUtils.toString(entity);
}finally{
httpclient.getConnectionManager().shutdown();
}
returnbody;
}
publicstaticStringdo_get(Stringurl)throwsClientProtocolException,IOException{
Stringbody="{}";
DefaultHttpClienthttpclient=newDefaultHttpClient();
try{
HttpGethttpget=newHttpGet(url);
HttpResponseresponse=httpclient.execute(httpget);
HttpEntityentity=response.getEntity();
body=EntityUtils.toString(entity);
}finally{
httpclient.getConnectionManager().shutdown();
}
returnbody;
}

⑷ 如何在java中调用api接口

  1. 需要导入对应的lib包,然后调用具体的接口以及方法

  2. 通过实现http协议进行post或者get请求具体api接口

⑸ java 怎么调用php的api接口

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import XmlHelper;

public class QXOutStream {
public String outPutStr(String urlStr, String input) throws Exception{
StringBuffer strBuf = new StringBuffer();
String Resulst="";
try{
URL url = new URL(urlStr);
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setRequestMethod("POST");
con.setAllowUserInteraction(false);
con.setUseCaches(false);
con.setRequestProperty("Accept-Charset", "GBK");
BufferedOutputStream bufOutPut = new BufferedOutputStream(con.getOutputStream());

byte[] bdat = input.getBytes("UTF-8");//解决中文乱码问题
bufOutPut.write(bdat, 0, bdat.length);
bufOutPut.flush();
BufferedInputStream inp = new BufferedInputStream(con.getInputStream());
InputStreamReader in = new InputStreamReader(inp,Charset.forName("GBK"));
BufferedReader bufReador = new BufferedReader(in);

String tempStr = "";
while (tempStr != null) {
strBuf.append(tempStr);
tempStr = bufReador.readLine();

}
Resulst = XmlHelper.getPostNodeText(strBuf.toString(), "OPERATOR_RESULT");//.getPostFirstRowText(strBuf.toString(), "OPERATOR_RESULT");

}
catch (Exception e) {
//System.err.println("Exception:"+e.toString());
throw e;
//return "N";
}
finally{
return Resulst;
}

}
}
你可以参考这个例子调用php 的api接口,这里面的urlStr就是你调用php的api url接口

⑹ 怎样在java中调用百度地图的API

网络地图Web服务API为开发者提供http接口,即开发者通过http形式发起检索请求,获取返回json或xml格式的检索数据。
api官网说明链接:

http://developer..com/map/webservice.htm

1、可用接口列举:
获取相关地址提示 place suggestion
http://api.map..com/place/v2/suggestion?query=水杉®ion=成都&output=json&ak=

2、获取坐标 geocoding

http://api.map..com/geocoder?address=锦江区菱安路299号&output=json&key=

http://api.map..com/place/v2/search?ak=&output=json&query=蓝光coco金沙&page_size=10&page_num=0&scope=1®ion=成都
{
"status":0,
"message":"ok",
"total":2,
"results":[
{
"name":"蓝光COCO金沙",
"location":{
"lat":30.687544,
"lng":103.996691
},
"address":"西三环外金沙西源大道(IT大道旁)",
"uid":"223992992c5ee7e0841541df"
},
{
"name":"蓝光COCO金沙2期",
"location":{
"lat":30.681123,
"lng":103.991123
},
"address":"青羊区金沙IT大道旁",
"uid":"b7bb5abb1cd4982213293580"
}
]
}

3、设施导航
http://api.map..com/place/v2/search?ak=&output=json&query=图书馆&page_size=20&page_num=0&scope=2®ion=成都

4、java测试类:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

public class LocationUtil {

private static final String BAIDU_APP_KEY = "";

/**
* 返回输入地址的经纬度坐标 key lng(经度),lat(纬度)
*/
public static Map<String, String> getLatitude(String address) {
try {
// 将地址转换成utf-8的16进制
address = URLEncoder.encode(address, "UTF-8");
// 如果有代理,要设置代理,没代理可注释
// System.setProperty("http.proxyHost","192.168.172.23");
// System.setProperty("http.proxyPort","3209");

URL resjson = new URL("http://api.map..com/geocoder?address="
+ address + "&output=json&key=" + BAIDU_APP_KEY);
BufferedReader in = new BufferedReader(new InputStreamReader(
resjson.openStream()));
String res;
StringBuilder sb = new StringBuilder("");
while ((res = in.readLine()) != null) {
sb.append(res.trim());
}
in.close();
String str = sb.toString();
System.out.println("return json:" + str);
if(str!=null&&!str.equals("")){
Map<String, String> map = null;
int lngStart = str.indexOf("lng\":");
int lngEnd = str.indexOf(",\"lat");
int latEnd = str.indexOf("},\"precise");
if (lngStart > 0 && lngEnd > 0 && latEnd > 0) {
String lng = str.substring(lngStart + 5, lngEnd);
String lat = str.substring(lngEnd + 7, latEnd);
map = new HashMap<String, String>();
map.put("lng", lng);
map.put("lat", lat);
return map;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

public static void main(String args[]) {

Map<String, String> map = LocationUtil.getLatitude("成都 高新西区西区大道1398号");
if (null != map) {
System.out.println(map.get("lng"));
System.out.println(map.get("lat"));
}
}
}

⑺ 怎么使用java调用api接口

java发一个http请求过去,带上参数就可以了啊,跟我们在浏览器上访问资源是一样的 只是它返回的是json格式的数据而已给你两个方法吧:public static String do_post(String url, List name_value_pair) throws IOException { String body = "{}"

⑻ java 中的api怎么使用

可以按照类名搜索,比附Math这个类,

直接输入Math,然后就能自动搜索到,双击打开,右侧显示该类的信息,包括实现的接口,继承的类等,往下看,列出了所有方法,包括返回值,参数等

⑼ JAVA中如何调用API函数

JAVA不能直接调用Windows底层API,除非使用JNI技术。需要C语言配合

阅读全文

与java访问api相关的资料

热点内容
微信怎么发应用app 浏览:776
花生壳dns服务器地址 浏览:648
squad服务器一般什么时候人多 浏览:479
程序员战门课 浏览:474
config保存服务器地址 浏览:317
预订网吧座位的app叫什么 浏览:416
香港服务器主机地址 浏览:640
网店美工pdf 浏览:447
一堆文件夹怎么弄出来 浏览:743
博途如何编译硬件 浏览:418
fortran程序pdf 浏览:504
电池消耗算法 浏览:394
服务器中断连接怎么处理 浏览:222
上世纪互联网不发达程序员很难 浏览:841
语音识别android开源 浏览:762
地埋式垃圾压缩中转站 浏览:902
apachehttpdlinux 浏览:944
快递员中通app预付款是什么 浏览:843
java路径转义 浏览:857
keytool加密算法 浏览:131