如何使用Java获取API的内容?

How to get the contents of API using Java?

我想从

获取天气 api 的值

http://www.dataweave.in/apis/usage/13/Indian-Weather-Data

PHP 一样,从 api 获取值的语法是:

$String=file_get_contents("url of the api");

但我想在 java 中做同样的事情。如何从 Java 中的 api 获取文件内容?

您可以使用如下内容:

import java.net.*;
import java.io.*;

public class URLConnectionReader {
    public static void main(String[] args) throws Exception {
        URL oracle = new URL("http://api.dataweave.in/v1/indian_weather/findByCity/?api_key=b20a79e582ee4953ceccf41ac28aa08d&city=Agartala&date=20120501");
        URLConnection yc = oracle.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(
                                yc.getInputStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) 
            System.out.println(inputLine);
        in.close();
    }
}

inputLine 有你的数据。