当前位置: 首页 > 知识库问答 >
问题:

使用OpenWeatherMap API密钥

贲宏硕
2023-03-14

我得到异常“http://api.openweathermap.org/data/2.5/weather?q=sydney”。有人能帮忙怎么用吗。当我粘贴以下内容时,可以很好地使用web浏览器

我也试过下面的组合,但没有运气

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个答案

耿炎彬
2023-03-14

1.-在你的app上添加上网权限如何在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<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();

    }
}
 类似资料:
  • OpenWeatherMapAPI openweathermap.org 网站的 iOS 客户端 API 包

  • 这是可能的还是加密必须共享和使用相同的密钥? 主要目的就是这样。 我将有两个客户端可以发送和接收加密数据到彼此。

  • 我需要在iPhone或iPad上加密字符串(实际上是XML文件),然后用.NET应用程序解密。感谢David Veksler在这里提出的问题“.NET和iPhone之间的AES互操作性?”,以及在这里发表的博客文章http://automagical.rationalmind.net/2009/02/12/aes-interoperability-between-net-and-iPhone/。

  • 我正在尝试使用KMS和AWS加密SDK加密数据。查看AWS文档中提供的示例,似乎没有地方可以显式设置数据键。 使用由KMS生成的数据密钥使用AWS加密SDK加密数据的推荐方法是什么?

  • Swagger支持api密钥的安全性,但这似乎仅限于单个参数。 有没有办法定义一组参数(key和secret)作为请求中的参数? 或者,唯一的方法就是跳过安全方案,只将这些参数添加到每个请求中?

  • 我有这样的情况,我使用OpenSSL生成了一个公钥/私钥对,供gdcmanon使用,遵循他们网站上列出的说明。具体地说,我使用以下命令为gdcmanon生成密钥 然后,我就能够按照他们的指示,加密一个文件,使用 这在c.init(cipher.decrypt_mode,key)行失败; 我已经为Java6安装了JCE(我正在使用)。我不知道我做错了什么。谁能给我指出正确的方向吗。 谢谢

  • null 我正在尝试使用Jasypt的StandardPBEStringEncryptor类 当我这样做时,我会得到以下异常: java.security.NosuchAlgorithmException:AES/CBC/PKCS5Padding SecretKeyFactory不可用 谢谢

  • 我们使用Azure Key Vault加密和解密Blob,以保护我们的文件不受开发人员或任何不想要的访问。 我创建了如下所示的RSA密钥 现在我很少怀疑了