Skip to content

Instantly share code, notes, and snippets.

@hoornet
Forked from steen919/MainActivity.java
Created August 24, 2016 15:48
Show Gist options
  • Save hoornet/6e2ec70a3ca8a1afd9d2386414d73d84 to your computer and use it in GitHub Desktop.
Save hoornet/6e2ec70a3ca8a1afd9d2386414d73d84 to your computer and use it in GitHub Desktop.
package sengvall.flwebb.se.myweather;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import net.aksingh.owmjapis.CurrentWeather;
import net.aksingh.owmjapis.OpenWeatherMap;
import org.json.JSONException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Date;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new RetrieveFeedTask().execute();
}
class RetrieveFeedTask extends AsyncTask<Void, Void, CurrentWeather> {
private Exception exception;
protected CurrentWeather doInBackground(Void... voids) {
try {
// declaring object of "OpenWeatherMap" class
OpenWeatherMap owm = new OpenWeatherMap(OpenWeatherMap.Units.METRIC, OpenWeatherMap.Language.SWEDISH, "");
// getting current weather data for the "London" city
CurrentWeather cwd = null;
cwd = owm.currentWeatherByCityName("Luntan Lundstorp");
return cwd;
} catch (Exception e) {
this.exception = e;
return null;
}
}
protected void onPostExecute(CurrentWeather cwd) {
if (cwd.isValid()) {
Date now = new java.util.Date(System.currentTimeMillis());
Date sunriseDate = cwd.getSysInstance().getSunriseTime();
// String sunrise = new java.text.SimpleDateFormat("HH:mm:ss").format(sunriseDate);
Date sunsetDate = cwd.getSysInstance().getSunsetTime();
if (now.before(sunriseDate))
System.out.println("Solen har inte gått upp.");
else if (now.after(sunriseDate) && now.before(sunsetDate))
System.out.println("Solen är uppe.");
else
System.out.println("Solen har gått ner.");
if (cwd.hasCityName()) {
//printing city name from the retrieved data
System.out.println(cwd.getCityName());
}
System.out.println(cwd.getSysInstance().getCountryCode());
if(cwd.getWeatherInstance(0).hasWeatherDescription())
System.out.println(cwd.getWeatherInstance(0).getWeatherDescription());
String temp = "--";
if (cwd.getMainInstance().hasTemperature()) {
int currCelsius = (int) Math.round(cwd.getMainInstance().getTemperature());
temp = currCelsius + "";
}
// printing the max./min. temperature
System.out.println(temp + "\u00B0" + "C");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment