-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTeamInfo.java
More file actions
244 lines (210 loc) · 8.34 KB
/
Copy pathTeamInfo.java
File metadata and controls
244 lines (210 loc) · 8.34 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package com.example.marvelselect;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager2.widget.ViewPager2;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
public class TeamInfo extends AppCompatActivity {
//API VARIABLES - START
//base URL - can later add more specific paths
/* REPLACE 'xxx' WITH YOUR OWN CHOICE OF ID */
String base = "http://gateway.marvel.com/v1/public/characters?events=xxx&";
String base1 = "http://gateway.marvel.com/v1/public/characters?events=xxx&";
String base2 = "http://gateway.marvel.com/v1/public/characters?events=xxx&";
//declaring MD5 digest hash variable of timestamp, private key, and public key
MessageDigest hash;
//API VARIABLES - END
//UI
static ViewPager2 page;
static Button back;
//variables
static ArrayList<Fragment> list;
static String heroName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_team_info);
//find views
page = findViewById(R.id.id_teaminfo_viewPager_page);
back = findViewById(R.id.id_teaminfo_button_back);
//on click listener for back button
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openLocker();
}
});
//ACCESSING API DATA - START
/* INSERT YOUR OWN PUBLIC & PRIVATE API KEY STRINGS HERE */
//creating valid URL to retrieve JSON
//creating timezone for URL to access
TimeZone tz = TimeZone.getTimeZone("EST");
@SuppressLint("SimpleDateFormat") SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'");
df.setTimeZone(tz);
String ts = df.format(new Date());
//adding timestamp to url
base += "ts=" + ts + "&";
base1 += "ts=" + ts + "&";
base2 += "ts=" + ts + "&";
//creating MD5 digest hash of timestamp, private key, and public key
try {
//looking for MD5 digest hash code
hash = MessageDigest.getInstance("MD5");
//update hash
hash.update((ts + privateKey + publicKey).getBytes(), 0, (ts + privateKey + publicKey).length());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
String hashVal = new BigInteger(1, hash.digest()).toString(16);
//adding public key and hash to url - now the URL can be used!
base += "apikey=" + publicKey + "&hash=" + hashVal;
base1 += "apikey=" + publicKey + "&hash=" + hashVal;
base2 += "apikey=" + publicKey + "&hash=" + hashVal;
//declaring url
URL url = null;
URL url1 = null;
URL url2 = null;
try {
//setting URL with JSON data to url variable
url = new URL(base);
url1 = new URL(base1);
url2 = new URL(base2);
} catch (MalformedURLException e) {
e.printStackTrace();
}
Intent intent = new Intent();
int hero1Pos = intent.getIntExtra("hero1Pos", 0);
int hero2Pos = intent.getIntExtra("hero2Pos", 0);
int hero3Pos = intent.getIntExtra("hero3Pos", 0);
//method call
new TeamInfo.RetrieveTask(0, hero1Pos).execute(url);
new TeamInfo.RetrieveTask(1, hero2Pos).execute(url1);
new TeamInfo.RetrieveTask(2, hero3Pos).execute(url2);
//ACCESSING API DATA - END
}//onCreate
private void openLocker() {
Intent intent = new Intent(this, Locker.class);
startActivity(intent);
}//openLocker
//AsyncTask - running url on separate thread
private static class RetrieveTask extends AsyncTask<URL, Void, URL> {
//variables
int id;
int heroPos;
//constructor
public RetrieveTask(int id, int heroPos) {
//set variables
this.heroPos = heroPos;
this.id = id;
}
@Override
protected URL doInBackground(URL... urls) {
try {
//create URL object
URL url = urls[0];
//create URLConnection object
URLConnection connection = url.openConnection();
//create InputStream object
InputStream stream = connection.getInputStream();
//create BufferedReader object
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
//concatenate data to one String value
String line;
String data = "";
while((line = reader.readLine()) != null) {
data += line;
}
//close BufferedReader
reader.close();
//converts JSON string to JSONObject
JSONObject dataObj = new JSONObject(data);
//JSON object - data
JSONObject info = dataObj.getJSONObject("data");
//JSON array - results
JSONArray results = info.getJSONArray("results");
//JSON object - sup
JSONObject sup = results.getJSONObject(heroPos);
//JSON object - thumbnail
JSONObject thumb = sup.getJSONObject("thumbnail");
//accessing superhero's name
String name = sup.getString("name");
heroName = name;
//accessing superhero's image
String img = thumb.getString("path");
String ext = thumb.getString("extension");
//finalizing URL to make it valid
img += "/portrait_xlarge." + ext;
//create URL variables from img String variable
URL imgUrl = new URL(img);
//returns ArrayList
return imgUrl;
} catch (IOException | JSONException e) {
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(URL url) {
//if ArrayList is not empty
if(url != null) {
//rendering images so that it can be displayed
new TeamInfo.LoadImages(id).execute(url);
}
}
}//RetrieveTask
//AsyncTask - loading images from JSON URL
private static class LoadImages extends AsyncTask<URL, Void, Bitmap> {
//variables
int id;
//constructor
public LoadImages(int id) {
this.id = id;
}
@Override
protected Bitmap doInBackground(URL... urls) {
URL url = urls[0];
//create URLConnection object
URLConnection connection = null;
try {
connection = url.openConnection();
//create InputStream object
InputStream stream = connection.getInputStream();
//Bitmap object
return BitmapFactory.decodeStream(stream);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(Bitmap bitmap) {
}
}//LoadImages
}