Issue
I'm trying to print some data that i get from the RiotApi
Api using ASyncTask
and then log them in the logcat using Log.d
for the data and Log.i
if a RiotApiException
exception is thrown by the printSummonerData()
method. However, when i run the app it compiles and runs just fine and i get no RiotApiExceptions
but the data doesn't get logged. Can anybody help me figure out why this is happening?
PS: I'm not sure if i should be using ASyncTask
in the first place (i'm completely new to threads,Synchronous and Asynchronous Tasks etc) but from what i've read in the Android Documentation i shouldn't block the main UI thread with Network Requests etc. so i thought i should use ASyncTask
. Please correct me if my assumption was wrong.
public class MainActivity extends AppCompatActivity {
private final static String API_KEY = "MYKEY";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new DownloadSummonerData();
}
private static class DownloadSummonerData extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... urls) {
try {
printSummonerData();
} catch (RiotApiException e) {
e.printStackTrace();
Log.i("LOL","Something went wrong!");
}
return null;
}
protected void onProgressUpdate(Void... progress) {
}
protected void onPostExecute(Void result) {
}
}
private static void printSummonerData() throws RiotApiException {
ApiConfig config = new ApiConfig().setKey(API_KEY);
RiotApi api = new RiotApi(config);
Summoner summoner = api.getSummonerByName(Platform.EUNE, "XmaxUniverse");
Log.d("LOL","Name: " + summoner.getName());
Log.d("LOL","Summoner ID: " + summoner.getId());
Log.d("LOL","Account ID: " + summoner.getAccountId());
Log.d("LOL","PUUID: " + summoner.getPuuid());
Log.d("LOL","Summoner Level: " + summoner.getSummonerLevel());
Log.d("LOL","Profile Icon ID: " + summoner.getProfileIconId());
}
}
Solution
You didn't start your AsyncTask
. Do his
new DownloadSummonerData().execute();
OR
// Assume DownloadSummonerData extends AsyncTask
DownloadSummonerData myAsyncTask= new DownloadSummonerData();
// Execute in parallel
myAsyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR)
Answered By - NickUnuchek
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.