小编Ria*_*Din的帖子

使用OpenWeatherMap API密钥

我得到例外" http://api.openweathermap.org/data/2.5/weather?q=Sydney ".有人可以帮助如何使用它.粘贴以下内容时,可以使用Web浏览器正常工作

http://api.openweathermap.org/data/2.5/weather?q=Sydney&APPID=ea574594b9d36ab688642d5fbeab847e

我尝试了以下组合,但没有运气

connection.addRequestProperty("x-api-key",
                    "&APPID=cea574594b9d36ab688642d5fbeab847e");


private static final String OPEN_WEATHER_MAP_API =
        "http://api.openweathermap.org/data/2.5/weather?q=%s";


public static JSONObject getJSON(String city) {
    try {
        URL url = new URL(String.format(OPEN_WEATHER_MAP_API, city));

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        connection.addRequestProperty("x-api-key",
                "cea574594b9d36ab688642d5fbeab847e");

        BufferedReader reader =
                new BufferedReader(new InputStreamReader(connection.getInputStream()));

        StringBuffer json = new StringBuffer(1024);
        String tmp = "";

        while((tmp = reader.readLine()) != null)
            json.append(tmp).append("\n");
        reader.close();

        JSONObject data = new JSONObject(json.toString());

        if(data.getInt("cod") != 200) {
            System.out.println("Cancelled");
            return null;
        }

        return data;
    } catch (Exception e) {

        System.out.println("Exception "+ e.getMessage()); …
Run Code Online (Sandbox Code Playgroud)

android weather-api openweathermap

3
推荐指数
1
解决办法
1万
查看次数

如果类有引用类型成员java,你如何实现相等和hashCode方法?

我试图实现以下示例来覆盖相等和hashCode方法,如果类具有引用类型成员但没有运气.任何帮助将受到高度赞赏.提前感谢你们..

     class Point{
    private int x, y;
    Point (int x, int y)
    {
        this.x =x;
        this.y = y;
    }

} 


class Circle 
{
    int radius; 
    Point point ;

    Circle(int x, int y, int radius)
    {
        point = new Point (x ,y);
        this.radius = radius;
    }



    @Override
    public boolean equals(Object arg) {

        if(arg == null) return false;
        if(arg == this) return true;
        if(arg instanceof Circle) 
        {
            if(this.point ==((Circle) arg).point && this.radius == ((Circle)           arg).radius)
            {
                return true;
            }

        } 


        return false; …
Run Code Online (Sandbox Code Playgroud)

java

0
推荐指数
1
解决办法
368
查看次数

标签 统计

android ×1

java ×1

openweathermap ×1

weather-api ×1