使用OpenWeatherMap API密钥 [英] Using OpenWeatherMap API Key

查看:197
本文介绍了使用OpenWeatherMap API密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到异常"

I get Exception "http://api.openweathermap.org/data/2.5/weather?q=Sydney". Can someone please help how to use it. Works fine with the web browser when i paste the following

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

I tried the following combination as well but no luck

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());
        return null;
    }  

解决方案

1.-Add internet permission on your app How to add manifest permission to android application?

2.-Here you have an example about how implement an api call

 public class MainActivity extends Activity {

    JSONObject data = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getJSON("Sydney");
    }

    public void getJSON(final String city) {

        new AsyncTask<Void, Void, Void>() {


            @Override
            protected void onPreExecute() {
                super.onPreExecute();

            }

            @Override
            protected Void doInBackground(Void... params) {
                try {
                    URL url = new URL("http://api.openweathermap.org/data/2.5/weather?q="+city+"&APPID=ea574594b9d36ab688642d5fbeab847e");

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

                    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();

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

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


                } catch (Exception e) {

                    System.out.println("Exception "+ e.getMessage());
                    return null;
                }

                return null;
            }

            @Override
            protected void onPostExecute(Void Void) {
            if(data!=null){
                Log.d("my weather received",data.toString());
            }

            }
        }.execute();

    }
}

这篇关于使用OpenWeatherMap API密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆