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;
}
Try this
Create a class named GetData
And then use this to get the data
new Getdata().execute("your city or country");
It looks like Open Weather might be experiencing problems. I say this because their example that they give is returning the same error message as yours is. From their site >> http://openweathermap.org/appid
Example of API call (does not have valid key):
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
Working sample with Retrofit