示例:
1.在fanyi.youdao.com底部点击api,
.
2.调用数据接口
.
3.填写相关后申请并复制链接
.
4.代码:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class TestGet {
public static void main(String[] args) {
new ReadByGet().start();
}
}
class ReadByGet extends Thread {
public void run() {
try {
// 有道翻译api申请的网站链接,doctype改为xml
URL url = new URL(
"http://fanyi.youdao.com/openapi.do?keyfrom=CLP-test&key=1870425496&type=data&doctype=xml&version=1.1&q=welcome");
URLConnection connection = url.openConnection();// 打开
InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is,"UTF-8");//UTF-8可以不用
BufferedReader br = new BufferedReader(isr);
String line;
StringBuilder builder = new StringBuilder();
while ((line = br.readLine()) != null) {
builder.append(line);
}
br.close();
isr.close();
is.close();
System.out.println(builder.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
5.结果:
6.复制到test.xml文件中,ctrl+i(格式化)