/gist:6b306e1f6a21b3718fa4 Secret
-
Star
(128)
You must be signed in to star a gist -
Fork
(92)
You must be signed in to fork a gist
-
-
Save anonymous/6b306e1f6a21b3718fa4 to your computer and use it in GitHub Desktop.
| // 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; | |
| try { | |
| // Construct the URL for the OpenWeatherMap query | |
| // Possible parameters are avaiable at OWM's forecast API page, at | |
| // http://openweathermap.org/API#forecast | |
| URL url = new URL("http://api.openweathermap.org/data/2.5/forecast/daily?q=94043&mode=json&units=metric&cnt=7"); | |
| // 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"); | |
| } | |
| if (buffer.length() == 0) { | |
| // Stream was empty. No point in parsing. | |
| forecastJsonStr = null; | |
| } | |
| forecastJsonStr = buffer.toString(); | |
| } catch (IOException e) { | |
| Log.e("PlaceholderFragment", "Error ", e); | |
| // If the code didn't successfully get the weather data, there's no point in attemping | |
| // to parse it. | |
| forecastJsonStr = null; | |
| } finally{ | |
| if (urlConnection != null) { | |
| urlConnection.disconnect(); | |
| } | |
| if (reader != null) { | |
| try { | |
| reader.close(); | |
| } catch (final IOException e) { | |
| Log.e("PlaceholderFragment", "Error closing stream", e); | |
| } | |
| } | |
| } |
Why not use the Apache HTTP library?
@frankkienl They recommended the HttpURLConnection class because it's a general implementation and lightweight.
great
Love that piece of code! It can't be more simplified! :)
Thanks for the clean code.
Code is poetry!
Beautiful and clean code
good
Can't be better!
I think we can use HttpGet to get simply code
String url = "http://api.openweathermap.org/data/2.5/forecast/daily?cnt=7&q=94043&mode=json&units=metric";
HttpGet get = new HttpGet(url);
HttpClient client = new DefaultHttpClient();
try {
HttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
Log.e("Data", data);
} catch (IOException e) {
e.printStackTrace();
}Great, I like it.
Lines 25, 39, 46 all assign null to forecastJsonStr which will always be null at those points, since it is only assigned to a value as the last line of the try block line 41 with the toString which doesn't create an exception.
The catch block needs only print the error message, the assignment of forecastJsonStr to null is unnecessary as well.
@hoanganh200870 your solution looks tidy 👍
This person forked and changed to java filetype for syntax highlighting:
https://gist.github.com/yeow/60adb201fa866aff8503
where to place this code
I need the second gist for android setting
i coudnt found the link ?!!!
Its all starting to make some sense now :P
good.
nice
Cannot be more simpler than this....
GOOD , 😃
دمتان گرم ، آبادمان کردید 😊
Good :-)
Thank You...
thanku developer.google for such a great code
Retrofit is more better
havnt any errors. i love it
great
Great !!
I like Super Clean 👍 💯
مرسی گوگول 👍
error: unreachable statement
.
.
.
at this
HttpURLConnection urlConnection = null;
Great!
Thanks!