À l'aide de OpenWeatherMap Clé API

- Je obtenir de l'Exception "http://api.openweathermap.org/data/2.5/weather?q=Sydney". Quelqu'un peut s'il vous plaît aider comment l'utiliser.
Fonctionne très bien avec le navigateur web lorsque je collez le texte suivant

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

J'ai essayé la combinaison suivante, mais pas de chance

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;
    }  
  • votre clé api devrait être un param, pas n'importe lequel x-api-key est censé signifier.
InformationsquelleAutor Riaz Ud Din | 2015-12-16