首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用OpenWeatherMap API密钥

使用OpenWeatherMap API密钥
EN

Stack Overflow用户
提问于 2015-12-16 20:41:18
回答 4查看 18.5K关注 0票数 4

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

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

我也尝试了下面的组合,但是没有运气。

代码语言:javascript
复制
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;
    }  
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2015-12-16 21:27:56

1.-在应用程序How to add manifest permission to android application?上添加internet权限

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

代码语言:javascript
复制
 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();

    }
}

票数 17
EN

Stack Overflow用户

发布于 2015-12-16 20:54:18

看来开放天气可能遇到了问题。我这么说是因为他们给出的例子是返回与您相同的错误消息。从他们的网站>> http://openweathermap.org/appid

API调用示例(没有有效密钥):

代码语言:javascript
复制
api.openweathermap.org/data/2.5/forecast/city?id=524901&APPID=1111111111
票数 2
EN

Stack Overflow用户

发布于 2015-12-16 20:55:33

尝尝这个

  1. 创建一个名为GetData的类 类GetData扩展AsyncTask < String,Void,String> {@重载受保护的字符串doInBackground(字符串.( params) { // TODO自动生成的方法存根字符串结果= "";HttpURLConnection conn =空;尝试{ url URL =新URL("http://api.openweathermap.org/data/2.5/weather?q="+URLEncoder.encode(params[0],HttpURLConnection conn = (HttpURLConnection) url.openConnection();InputStream in =新BufferedInputStream(conn.getInputStream());如果(in = null) { BufferedReader bufferedReader =新BufferedReader(新的InputStreamReader(in));字符串行= "";while ((line = bufferedReader.readLine()) != null)结果+=行;} in.close();返回结果;} catch (IOException e) { // TODO自动生成的catch块e.printStackTrace();}最后{ if(conn!=null) conn.disconnect();}返回结果;}@覆盖受保护的无效onPostExecute(字符串结果){ // TODO自动生成的方法存根super.onPostExecute(结果);Toast.makeText(getApplicationContext(),result,Toast.LENGTH_LONG).show();}}
  2. 然后用这个来获取数据 new Getdata().execute("your city or country");
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34321728

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档