-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPokemonAsyncTask.java
More file actions
95 lines (78 loc) · 2.52 KB
/
Copy pathPokemonAsyncTask.java
File metadata and controls
95 lines (78 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package edu.lclark.homework2;
import android.os.AsyncTask;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* Created by maiaphoebedylansamerjan on 2/21/16.
*/
public class PokemonAsyncTask extends AsyncTask<String,Integer,JSONArray> {
public static final String TAG = PokemonAsyncTask.class.getSimpleName();
Pokemon currentPokemon;
@Override
protected void onPreExecute() {
super.onPreExecute();
Log.d(TAG, "Started AsyncTask");
}
@Override
protected JSONArray doInBackground(String... params) {
StringBuilder responseBuilder = new StringBuilder();
JSONArray jsonArray=null;
if (params.length == 0) {
return null;
}
HttpURLConnection urlConnection;
try {
URL url = new URL("http://pokeapi.co/api/v2/pokemon/"+currentPokemon.getID());
urlConnection = (HttpURLConnection) url.openConnection();
InputStreamReader inputStream = new InputStreamReader(urlConnection.getInputStream());
BufferedReader reader = new BufferedReader(inputStream);
String line;
if (isCancelled()) {
return null;
}
while ((line = reader.readLine()) != null) {
responseBuilder.append(line);
if (isCancelled()) {
return null;
}
}
jsonArray = new JSONArray(responseBuilder.toString());
if (isCancelled()) {
return null;
}
} catch (IOException | JSONException e) {
Log.e(TAG, e.getLocalizedMessage());
}
return jsonArray;
}
@Override
protected void onCancelled(JSONArray jsonArray) {
super.onCancelled(jsonArray);
Log.d(TAG, "AsyncTask cancelled");
}
@Override
protected void onPostExecute(JSONArray jsonArray) {
super.onPostExecute(jsonArray);
if (jsonArray == null) {
Log.e(TAG, "Resulting JSON is null");
} else {
for (int i = 0; i < jsonArray.length(); i++) {
try {
JSONObject subscription = jsonArray.getJSONObject(i);
JSONObject owner = subscription.getJSONObject("owner");
Log.d(TAG, owner.getString("Pokemon") + " has " + subscription.getString("number"));
} catch (JSONException e) {
Log.e(TAG, e.getLocalizedMessage());
}
}
Log.d(TAG, jsonArray.toString());
}
}
}