Skip to content

Instantly share code, notes, and snippets.

@toneloc
Created July 26, 2016 12:32
Show Gist options
  • Save toneloc/83b6b5aab7c79a0b93135a804565319d to your computer and use it in GitHub Desktop.
Save toneloc/83b6b5aab7c79a0b93135a804565319d to your computer and use it in GitHub Desktop.
Working on JSON Async Task
package com.toneloc.concertconcierge;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class MainActivity extends AppCompatActivity {
private ArrayList<Concert> mConcertList = new ArrayList<>();
private TextView mResult;
private class FetchConcertsTask extends AsyncTask<String, Void, JSONObject> {
private OkHttpClient http = new OkHttpClient();
// while the task runs....
@Override
protected JSONObject doInBackground(String... strings) {
//allows for debugging in async task
android.os.Debug.waitForDebugger();
// async call the API
String url = "http://api.songkick.com/api/3.0/metro_areas/24426/calendar.json?apikey=";
Request req = new Request.Builder().url(url).build();
try {
Response res = http.newCall(req).execute();
String data = res.body().string();
//format JSON response
JSONObject rootObject = new JSONObject(data);
JSONArray events = rootObject.getJSONArray("events");
String string = rootObject.optString("displayName");
// purge prior results
mConcertList.clear();
Integer length = events.length();
//loop through the array of events to get relevant concert data
for (int i = 0; i < length; i++) {
//get a single event JSON object
JSONObject singleEvent = events.getJSONObject(i);
//event is JSON object
int id = singleEvent.getInt("id");
String displayName = singleEvent.getString("displayName");
double popularity = singleEvent.getDouble("popularity");
//start is JSON object
JSONObject start = singleEvent.getJSONObject("start");
double startTime = start.getDouble("time");
double startDate = start.getDouble("date");
//location is also JSON object
JSONObject location = singleEvent.getJSONObject("location");
String city = location.getString("city");
double latitude = location.getDouble("latitude");
double longitude = location.getDouble("longitude");
Concert tempConcert = new Concert(id, displayName, popularity, city, latitude, longitude, startTime, startDate);
mConcertList.add(tempConcert);
}
} catch (IOException ex) {
ex.printStackTrace();
} catch (JSONException ex) {
ex.printStackTrace();
}
return null;
}
// when the task is done
@Override
protected void onPostExecute(JSONObject location) {
// when done, we need to populate page with concerts, somehow
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mResult = (TextView) findViewById(R.id.result);
// find our asynctask
FetchConcertsTask fetchConcertsTask = new FetchConcertsTask();
// start our task
fetchConcertsTask.execute();
//upcoming events for a metro area
//hard coded london as the metro area
//can make get lat/lng, then make separate API call to
//get closest songkick metro area
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment