Issue
I am building a sunshine app from Udacity course. In lesson 2, I'm trying to connect app to the cloud on OpenWeatherMap.org site to get the weather data for the city. At first, the basic query works i.e. URL url = new URL("http://api.openweathermap.org/data/2.5/forecast?id=524901&APPID=c21566b1153f87e9f1d256b962cd6d42 "); But when i try to get the data via JSON parsing, it gives me following error in the Logcat.
java.io.FileNotFoundException: http://api.openweathermap.org/data/2.5/forecast/daily?q=44000&mode=json&units=metric&cnt=7&APPID=c21566b1153f87e9f1d256b962cd6d42
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:251)
at com.example.android.sunshine.app.ForecastFragment$FetchWeatherTask.doInBackground(ForecastFragment.java:173)
at com.example.android.sunshine.app.ForecastFragment$FetchWeatherTask.doInBackground(ForecastFragment.java:110)
at android.os.AsyncTask$2.call(AsyncTask.java:345)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:257)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:784)##
Here is my code.
private class FetchWeatherTask extends AsyncTask<String, Void, Void> {
private final String LOG_TAG = FetchWeatherTask.class.getSimpleName();
@Override
protected Void doInBackground(String... params) {
// If there's no zip code, there's nothing to look up. Verify size of params.
if (params.length == 0){
return null;
}
// These two need to be declared outside the try/catch
// so that they can be closed in the finally block.
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
// Will contain the raw JSON response as a string.
String forecastJsonStr = null;
String format = "json";
String units = "metric";
int numDays = 7;
try {
// Construct the URL for the OpenWeatherMap query
// Possible parameters are available at OWM's forecast API page, at
//
URL url = new URL("http://api.openweathermap.org/data/2.5/forecast?id=524901&APPID=c21566b1153f87e9f1d256b962cd6d42 ");
//URL url = new URL("http://api.openweathermap.org/data/2.5/weather?zip=94040,us");
/**Debugging */
Log.d("Test", "down keycode: " + url);
final String FORECAST_BASE_URL =
"http://api.openweathermap.org/data/2.5/forecast/daily?";
final String QUERY_PARAM = "q";
final String FORMAT_PARAM = "mode";
final String UNITS_PARAM = "units";
final String DAYS_PARAM = "cnt";
final String APPID_PARAM = "APPID";
Uri builtUri = Uri.parse(FORECAST_BASE_URL).buildUpon()
.appendQueryParameter(QUERY_PARAM, params[0])
.appendQueryParameter(FORMAT_PARAM, format)
.appendQueryParameter(UNITS_PARAM, units)
.appendQueryParameter(DAYS_PARAM, Integer.toString(numDays))
.appendQueryParameter(APPID_PARAM, BuildConfig.OPEN_WEATHER_MAP_API_KEY)
.build();
url = new URL(builtUri.toString());
Log.v(LOG_TAG, "Built URI " + builtUri.toString());
/**Debugging */
Log.d("Test", "down keycode: " + url);
//Log.v(LOG_TAG, "Built URI " + builtUri.toString());
// Create the request to OpenWeatherMap, and open the connection
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
// Read the input stream into a String
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
if (inputStream == null) {
// Nothing to do.
forecastJsonStr = null;
}
reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
// Since it's JSON, adding a newline isn't necessary (it won't affect parsing)
// But it does make debugging a *lot* easier if you print out the completed
// buffer for debugging.
buffer.append(line + "\n");
Log.i("Test", String.valueOf(line));
}
if (buffer.length() == 0) {
// Stream was empty. No point in parsing.
forecastJsonStr = null;
}
forecastJsonStr = buffer.toString();
} catch (IOException e) {
Log.e("FetchWeatherTask", "Error ", e);
// If the code didn't successfully get the weather data, there's no point in attempting
// to parse it.
Log.d("Test", "Here it reaches", e);
forecastJsonStr = null;
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (final IOException e) {
Log.e("FetchWeatherTask", "Error closing stream", e);
}
}
}
return null;
}
There is definitely a problem with a url that is built after JSON parsing. Some parameter that must be wrong because as I said earlier the basic url above works but the url that is built after JSON parsing gives filenotfoundexception! I tried looking over the internet for this problem but my problem couldn't solved.
Here again the url before JSON parsing that works and fetch data from the website http://api.openweathermap.org/data/2.5/forecast?id=524901&APPID=c21566b1153f87e9f1d256b962cd6d42
and here url that is formed after JSON parsing that gives FILENOTFOUNDEXCEPTION http://api.openweathermap.org/data/2.5/forecast/daily?q=44000&mode=json&units=metric&cnt=7&APPID=c21566b1153f87e9f1d256b962cd6d42
Solution
Your problem stays in the q parameter. For OpenWeatherMap api you have to use q as a parameter followed by the city name.
api.openweathermap.org/data/2.5/forecast/daily?q=London&mode=xml&units=metric&cnt=7
Documentation here: https://openweathermap.org/forecast16
Answered By - Nicola Gallazzi
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.