Skip to content

Instantly share code, notes, and snippets.

@marlusantos
Created October 12, 2016 04:16
Show Gist options
  • Save marlusantos/1004ee13e528b2e99dd528ceb2294d1b to your computer and use it in GitHub Desktop.
Save marlusantos/1004ee13e528b2e99dd528ceb2294d1b to your computer and use it in GitHub Desktop.
public class Myconnect extends AsyncTask<String, Void, String[]>{
@Override
protected String[] 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 avaiable at OWM's forecast API page, at
// http://openweathermap.org/API#forecast
//ref: https://developer.android.com/reference/android/net/Uri.Builder.html
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 url = new URL(builtUri.toString());
Log.v(LOG_TAG, "Built URI " + builtUri.toString());
// Create the request 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.
return null;
}
reader = new BufferedReader(new InputStreamReader(inputStream));
Log.v("MENU", "Entrou no line = reader.readLine()) != null--> " + buffer.toString());
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.v("MENU", "reader = new BufferedReader(new InputStreamReader(inputStream));--> " + reader.readLine());
}
if (buffer.length() == 0) {
// Stream was empty. No point in parsing.
return null;
}
//My String in JSON
forecastJsonStr = buffer.toString();
Log.v("MENU", "forecastJsonStr--> " + forecastJsonStr);
} catch (IOException e) {
Log.e("DetailFragment", "Error ", e);
// If the code didn't successfully get the weather data, there's no point in attemping
// to parse it.
return null;
}
finally{
if (urlConnection != null) {
urlConnection.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (final IOException e) {
Log.e("DetailFragment", "Error closing stream", e);
}
}
}
try {
return getWeatherDataFromJson(forecastJsonStr, numDays);
}catch (JSONException e){
Log.e("DetailFragment", "Error closing stream", e);
}
// This will only happen if there was an error getting or parsing the forecast.
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment