-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInfoTeam.java
More file actions
293 lines (259 loc) · 10.4 KB
/
Copy pathInfoTeam.java
File metadata and controls
293 lines (259 loc) · 10.4 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package com.example.marvelselect;
import androidx.appcompat.app.AppCompatActivity;
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.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
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.TimeZone;
public class InfoTeam 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 ImageView img1;
static ImageView img2;
static ImageView img3;
static TextView name1;
static TextView name2;
static TextView name3;
static Button back;
//variables storing superhero names
static String heroName1;
static String heroName2;
static String heroName3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_info_team);
//find views
img1 = findViewById(R.id.id_infoteam_imageView1);
img2 = findViewById(R.id.id_infoteam_imageView2);
img3 = findViewById(R.id.id_infoteam_imageView3);
name1 = findViewById(R.id.id_infoteam_textView1);
name2 = findViewById(R.id.id_infoteam_textView2);
name3 = findViewById(R.id.id_infoteam_textView3);
back = findViewById(R.id.id_infoteam_back);
//on click listener for back button
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//get info from Locker
Intent anotherIntent = getIntent();
int hero1Pos = anotherIntent.getIntExtra("hero1Pos", 0);
int hero2Pos = anotherIntent.getIntExtra("hero2Pos", 0);
int hero3Pos = anotherIntent.getIntExtra("hero3Pos", 0);
//go back to Locker class
Intent intent = new Intent(InfoTeam.this, Locker.class);
intent.putExtra("hero1Pos", hero1Pos);
intent.putExtra("hero2Pos", hero2Pos);
intent.putExtra("hero3Pos", hero3Pos);
finish();
}
});
//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();
}
//get hero position data
Intent intent = getIntent();
int hero1Pos = intent.getIntExtra("hero1Pos", 0);
int hero2Pos = intent.getIntExtra("hero2Pos", 0);
int hero3Pos = intent.getIntExtra("hero3Pos", 0);
//method call
new InfoTeam.RetrieveTask(0, hero1Pos).execute(url);
new InfoTeam.RetrieveTask(1, hero2Pos).execute(url1);
new InfoTeam.RetrieveTask(2, hero3Pos).execute(url2);
//ACCESSING API DATA - END
}//onCreate
//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");
//checks which hero that is being referred to
//updates heroName values
if(id == 0) {
heroName1 = name;
}
else if(id == 1) {
heroName2 = name;
}
else if(id == 2) {
heroName3 = name;
}
//accessing superhero's image
String img = thumb.getString("path");
String ext = thumb.getString("extension");
//finalizing URL to make it valid
img += "/landscape_incredible." + 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 InfoTeam.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) {
//set variables
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) {
//distinguishing between superheros taken from different URLs
if (id == 0) {
img1.setImageBitmap(bitmap);
displayName();
} else if (id == 1) {
img2.setImageBitmap(bitmap);
displayName();
} else if (id == 2) {
img3.setImageBitmap(bitmap);
displayName();
}
}
//to display each superhero's name
private void displayName() {
//distinguishing between superheros taken from different URLs
if (id == 0) {
name1.setText(heroName1);
} else if (id == 1) {
name2.setText(heroName2);
} else if (id == 2) {
name3.setText(heroName3);
}
}
}//LoadImages
}