
正文
java请求api的代码 java请求接口的几种方式
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java 调用windows api 如何实现
public class TestJni {
public native void GetTickCount();
static {
System.loadLibrary("Kernel32.dll");
}
public static void main(String[] args) {
TestJni testJni = new TestJni();
testJni.GetTickCount();
}
}
执行上面代码结果是,用java调用windowsAPI
java.lang.UnsatisfiedLinkError: no Kernel32.dll in java.library.path
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at com.TestJni. clinit(TestJni.java:7)
Exception in thread "main"
Java平台提供了一套自己的API。这套API中的一些平台相关的东西,比如文件的读取,则是Java虚拟机调用windows API来实现的。所以你使用Java给你提供的API就足够了,而且还保证了可以跨平台运行。
如果你非要像你说的那样做的话,就用Java中的native方法。这样你就可以用C/C++来实现Java中声明的函数了。
相关问答
Q1: JAVA 调用系统API, 有的参数需要传地址,代码如下,希望懂的朋友能教教我
jtf.setSize(20, 5);
jta = new JTextArea(5,20);
jta.setLineWrap(true); //自动换行
jsp = new JScrollPane(jta); //滚动面板
jb=new JButton("查询");
this.setContentPane(jp);
jp.add(jtf);
jp.add(jb);
jp.add(jsp);
jb.addMouseListener(new MyAction());
}
public void check()
{
try {
fr=new FileReader(file);
bfr=new BufferedReader(fr);
int i=0,k=0;
String str="";
while(i20)
{
str=bfr.readLine();
String[] emp=str.split(",");
if((jtf.getText()).equals(emp[1]))
{
jta.setText(str);
k++;
}
i++;
}
if(k1){
JOptionPane.showMessageDialog(EmpCheck.this, "未找到该员工信息!","错误",JOptionPane.ERROR_MESSAGE);
}
bfr.close();
fr.close();
}catch (FileNotFoundException a){
a.printStackTrace();
}catch (IOException a){
a.printStackTrace();
}
}
class MyAction implements MouseListener{
@Override
public void mouseClicked(MouseEvent e) {
check();
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}
Q2: 如何用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;
}
Q3: java调用百度api生成短链接,跪求java代码,不要复制网上的,要自己亲测实际能运行的!谢谢
package com.zhidao.www;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class TestShort {
public static void main(String[] args) {
// TODO Auto-generated method stub
String httpUrl = "";
String httpArg = "url_long=http%3A%2F%2Fapistore.baidu.com%2Fastore%2Fshopready%2F1973.html";
String jsonResult = request(httpUrl, httpArg);
System.out.println(jsonResult);
}
/**
* @param urlAll
* :请求接口
* @param httpArg
* :参数
* @return 返回结果
*/
public static String request(String httpUrl, String httpArg) {
BufferedReader reader = null;
String result = null;
StringBuffer sbf = new StringBuffer();
httpUrl = httpUrl + "?" + httpArg;
try {
URL url = new URL(httpUrl);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setRequestMethod("GET");
// 填入apikey到HTTP header
connection.setRequestProperty("apikey", "自己的apikey");
connection.connect();
InputStream is = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String strRead = null;
while ((strRead = reader.readLine()) != null) {
sbf.append(strRead);
sbf.append("\r\n");
}
reader.close();
result = sbf.toString();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
这个可以用 不过要自己申请下api key 得到的字符串也要自己解析
关于java请求api的代码和java请求接口的几种方式的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。






