当前位置:  开发笔记 > 前端 > 正文

使用OpenWeatherMap API密钥

如何解决《使用OpenWeatherMapAPI密钥》经验,为你挑选了1个好方法。

我得到例外" 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());
        return null;
    }  

JoCuTo.. 16

1.-在您的应用程序上添加Internet权限 如何向Android应用程序添加清单权限?

2.-这里有一个关于如何实现api调用的示例

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


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

    }
}

在此输入图像描述



1> JoCuTo..:

1.-在您的应用程序上添加Internet权限 如何向Android应用程序添加清单权限?

2.-这里有一个关于如何实现api调用的示例

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


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

    }
}

在此输入图像描述

推荐阅读
赛亚兔备_393
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有