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 android.util.Log; 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.FinishedGame; import ca.zadrox.dota2esportticker.data.gson.model.FinishedGameResult; import ca.zadrox.dota2esportticker.data.gson.model.NoGameResultData; import static ca.zadrox.dota2esportticker.util.LogUtils.LOGD; /** * Created by Acco on 11/3/2014. */ public class SAPIGameResultLoader extends AsyncTaskLoader<DotaGameData> { private final String TAG = SAPIGameResultLoader.class.getSimpleName(); DotaGameData mGame; long matchId; public SAPIGameResultLoader(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); } } /** * @return DotaGameData - containing either a FinishedGame, if results were found, * NoGameResultData, if no results were found, * null, if there's an error with the SAPI. */ @Override public DotaGameData loadInBackground() { String steamAPIKey = SteamAuth.getSteamApiKey(getContext()); String getMatchDetailsEndpoint = "http://api.steampowered.com/IDOTA2Match_570/GetMatchDetails/v1/"; if (mGame == null) { mGame = new FinishedGame(); } try { OkHttpClient okHttpClient = new OkHttpClient(); String rawJson = okHttpClient .newCall(new Request.Builder() .url(getMatchDetailsEndpoint + "?key=" + steamAPIKey + "&match_id=" + matchId).build()) .execute().body().string(); Gson gson = new Gson(); DotaGameData result = gson.fromJson(rawJson, FinishedGameResult.class).result; if (result == null) { Log.d(TAG, "returning null..."); return null; } else if (((FinishedGame) result).error != null) { return new NoGameResultData(); } else { mGame = result; return mGame; } } catch (IOException e) { LOGD(TAG, "Could not grab match json"); } return null; } protected void onReleaseResources(DotaGameData data) { } }