java请求api的代码 java请求接口的几种方式

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, 有的参数需要传地址,代码如下,希望懂的朋友能教教我

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

}

}

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中声明的函数了。


标题名称:java请求api的代码 java请求接口的几种方式
文章源于:http://azwzsj.com/article/doijdsj.html