Java tutorial
/* * Copyright 2014 Nicholas Liu * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package ca.zadrox.dota2esportticker.util.steamapi; import android.content.AsyncTaskLoader; import android.content.Context; import com.google.gson.Gson; import com.squareup.okhttp.OkHttpClient; import com.squareup.okhttp.Request; import java.io.IOException; import ca.zadrox.dota2esportticker.data.gson.model.DotaGameData; import ca.zadrox.dota2esportticker.data.gson.model.Game; import ca.zadrox.dota2esportticker.data.gson.model.LiveLeagueGames; import ca.zadrox.dota2esportticker.data.gson.model.LiveLeagueGamesResult; import ca.zadrox.dota2esportticker.util.LogUtils; /** * Created by Acco on 11/3/2014. */ public class SAPISingleLiveGameLoader extends AsyncTaskLoader<DotaGameData> { private final String TAG = SAPISingleLiveGameLoader.class.getSimpleName(); DotaGameData mGame; long matchId; public SAPISingleLiveGameLoader(Context context, long mId) { super(context); matchId = mId; } @Override protected void onStopLoading() { cancelLoad(); } @Override public void deliverResult(DotaGameData data) { if (isReset()) { if (mGame != null) { onReleaseResources(mGame); } } super.deliverResult(data); } @Override protected void onReset() { super.onReset(); onStopLoading(); if (mGame != null) { onReleaseResources(mGame); mGame = null; } } @Override public void onCanceled(DotaGameData data) { super.onCanceled(data); onReleaseResources(data); } @Override protected void onStartLoading() { if (mGame != null) { deliverResult(mGame); } else { forceLoad(); } } @Override public DotaGameData loadInBackground() { String steamAPIKey = SteamAuth.getSteamApiKey(getContext()); String getLiveLeagueGamesEndpoint = "http://api.steampowered.com/IDOTA2Match_570/GetLiveLeagueGames/v1/"; if (mGame == null) { mGame = new Game(); } try { OkHttpClient okHttpClient = new OkHttpClient(); String rawJson = okHttpClient.newCall(new Request.Builder().url(getLiveLeagueGamesEndpoint + "?key=" + steamAPIKey + (matchId != 0 ? "&match_id=" + matchId : "")).build()).execute().body() .string(); Gson gson = new Gson(); LiveLeagueGamesResult liveLeagueGames = gson.fromJson(rawJson, LiveLeagueGames.class).result; if (liveLeagueGames == null || liveLeagueGames.games == null || liveLeagueGames.games.length == 0) { return null; // game has concluded, return null and handle appropriately. } else { mGame = liveLeagueGames.games[0]; return mGame; } } catch (IOException e) { LogUtils.LOGD(TAG, "Could not grab match json"); } return null; } protected void onReleaseResources(DotaGameData data) { } }