Skip to content

Instantly share code, notes, and snippets.

@sirvon
Created December 10, 2014 10:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sirvon/6da95381bd4a22bd5ee8 to your computer and use it in GitHub Desktop.
Save sirvon/6da95381bd4a22bd5ee8 to your computer and use it in GitHub Desktop.
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
//note that it's not this name anymore, update it!
import dme.forecastiolib.FIOCurrently;
import dme.forecastiolib.FIODaily;
import dme.forecastiolib.ForecastIO;
public class MainActivity extends Activity {
final static String APIKEY = YOUR_API_KEY;
private ListView mainListView ;
private ArrayAdapter<String> listAdapter ;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find the ListView resource.
mainListView = (ListView) findViewById( R.id.mainListView );
// Create ArrayAdapter using the planet list.
listAdapter = new ArrayAdapter<String>(this, R.layout.simple_row);
new AsyncCaller().execute();
// Set the ArrayAdapter as the ListView's adapter.
mainListView.setAdapter( listAdapter );
}
private class AsyncCaller extends AsyncTask<Void, Void, ForecastIO> {
ProgressDialog pdLoading = new ProgressDialog(MainActivity.this);
@Override
protected void onPreExecute() {
super.onPreExecute();
Log.d("My Info", "Inside onPreExecute");
//this method will be running on UI thread
pdLoading.setMessage("\tLoading...");
pdLoading.show();
}
@Override
protected ForecastIO doInBackground(Void... params) {
//this method will be running on background thread so don't update UI frome here
//do your long running http tasks here,you dont want to pass argument and u can access the parent class' variable url over here
String apikey = APIKEY;
ForecastIO fio = new ForecastIO(apikey);
fio.setUnits(ForecastIO.UNITS_SI);
fio.setLang(ForecastIO.LANG_ENGLISH);
fio.getForecast("38.7252993" , "-9.1500364");
return fio;
}
@Override
protected void onPostExecute(ForecastIO result) {
super.onPostExecute(result);
pdLoading.dismiss();
//Currently data
FIOCurrently currently = new FIOCurrently(result);
//Daily data
FIODaily daily = new FIODaily(result);
//Currently data
listAdapter.add("Currently Data:");
String [] f = currently.get().getFieldsArray();
for(int i = 0; i<f.length;i++){
listAdapter.add( f[i]+": "+currently.get().getByKey(f[i]) );
}
//Daily data
listAdapter.add("Currently Data:");
for(int i = 0; i<daily.days(); i++){
String [] h = daily.getDay(i).getFieldsArray();
listAdapter.add("Day #"+(i+1));
for(int j=0; j<h.length; j++)
listAdapter.add(h[j]+": "+daily.getDay(i).getByKey(h[j]));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment