-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainActivity.java
More file actions
458 lines (423 loc) · 17.9 KB
/
Copy pathMainActivity.java
File metadata and controls
458 lines (423 loc) · 17.9 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
package edu.calvin.cs262.homework02;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* Reads the CS 262 monopoly webservice
* The code is based on Deitel's WeatherViewer (Chapter 7), simplified based on Murach's NewsReader (Chapter 10).
* for CS 262, lab 6. The PUT, POST and DELETE handlers were based on Google's documentation
* (https://developer.android.com/reference/java/net/HttpURLConnection.html), several Stackoverflow
* pages (and prayer).
*
* @author kvlinden
* @version summer, 2016
*/
public class MainActivity extends AppCompatActivity {
private EditText idText;
private Button fetchButton;
private List<Player> playerList = new ArrayList<>();
private ListView itemsListView;
private NumberFormat numberFormat = NumberFormat.getInstance();
private static String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
idText = (EditText) findViewById(R.id.id_text);
fetchButton = (Button) findViewById(R.id.fetchButton);
itemsListView = (ListView) findViewById(R.id.playerListView);
numberFormat.setMaximumFractionDigits(0);
// The standard get player listener
fetchButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismissKeyboard(idText);
new GetPlayerTask().execute(createURL(idText.getText().toString()));
}
});
// Here are some hacked "fetch" button listeners that test PUT, POST and DELETE.
// They pass a hard-coded URL, just for testing purposes.
// PUT (This one edits the player with the ID entered on the UI.)
// fetchButton.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// dismissKeyboard(idText);
// try {
// new PutPlayerTask().execute(new URL("http://cs262.cs.calvin.edu:8089/monopoly/player/" + idText.getText().toString()));
// } catch (Exception e) {
// Log.d(TAG, "PUT error...");
// }
// }
// });
// POST
// fetchButton.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// dismissKeyboard(idText);
// try {
// new PostPlayerTask().execute(new URL("http://cs262.cs.calvin.edu:8089/monopoly/player"));
// } catch (Exception e) {
// Log.d(TAG, "POST error...");
// }
// }
// });
// DELETE (This one takes the ID to delete from the UI.)
// fetchButton.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// dismissKeyboard(idText);
// try {
// new DeletePlayerTask().execute(new URL("http://cs262.cs.calvin.edu:8089/monopoly/player/" + idText.getText().toString()));
// } catch (Exception e) {
// Log.d(TAG, "DELETE error...");
// }
// }
// });
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_about:
startActivity(new Intent(getApplicationContext(), edu.calvin.cs262.homework02.AboutActivity.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/**
* Formats a URL for the webservice specified in the string resources.
*
* @param id string version of the desired ID (or BLANK for all players)
* @return URL formatted for the course monopoly server
*/
private URL createURL(String id) {
try {
String urlString = getString(R.string.web_service_url);
if (id.equals("")) {
urlString += "/players";
} else {
urlString += "/player/" + id;
}
return new URL(urlString);
} catch (Exception e) {
Toast.makeText(this, getString(R.string.connection_error), Toast.LENGTH_SHORT).show();
}
return null;
}
/**
* Deitel's method for programmatically dismissing the keyboard.
*
* @param view the TextView currently being edited
*/
private void dismissKeyboard(View view) {
InputMethodManager imm = (InputMethodManager) getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
/**
* Inner class for GETing the player list from the course server asynchronously
*/
private class GetPlayerTask extends AsyncTask<URL, Void, JSONArray> {
@Override
protected JSONArray doInBackground(URL... params) {
HttpURLConnection connection = null;
StringBuilder jsonText = new StringBuilder();
JSONArray result = null;
try {
connection = (HttpURLConnection) params[0].openConnection();
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
BufferedReader reader = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
jsonText.append(line);
}
//Log.d(TAG, jsonText.toString());
if (jsonText.toString().startsWith("{ \"items\":")) {
// Google Cloud can't return a raw JSON list, so we had to add this "items" field;
// remove it now.
String jsonItemsText = new JSONObject(jsonText.toString()).get("items").toString();
result = new JSONArray(jsonItemsText);
} else if (jsonText.toString().equals("null")) {
result = new JSONArray();
} else {
result = new JSONArray().put(new JSONObject(jsonText.toString()));
}
} else {
throw new Exception();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
}
return result;
}
@Override
protected void onPostExecute(JSONArray players) {
playerList.clear();
if (players == null) {
Toast.makeText(MainActivity.this, getString(R.string.connection_error), Toast.LENGTH_SHORT).show();
} else if (players.length() == 0) {
Toast.makeText(MainActivity.this, getString(R.string.no_results_error), Toast.LENGTH_SHORT).show();
} else {
convertJSONtoArrayList(players);
}
MainActivity.this.updateDisplay();
}
}
/**
* Inner class for POSTing a new, hard-coded player to the course server asynchronously
* This is for testing purposes only.
*/
private class PostPlayerTask extends AsyncTask<URL, Void, JSONArray> {
@Override
protected JSONArray doInBackground(URL... params) {
HttpURLConnection connection = null;
StringBuilder jsonText = new StringBuilder();
JSONArray result = null;
try {
// Hard-code a new player using JSON.
JSONObject jsonData = new JSONObject();
jsonData.put("emailAddress", "kvlinden@calvin.edu");
// Open the connection as usual.
connection = (HttpURLConnection) params[0].openConnection();
// Configure the connection for a POST, including outputing streamed JSON data.
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type","application/json");
connection.setFixedLengthStreamingMode(jsonData.toString().length());
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.writeBytes(jsonData.toString());
out.flush();
out.close();
// Handle the response from the (Lab09) server as usual.
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
BufferedReader reader = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
jsonText.append(line);
}
//Log.d(TAG, jsonText.toString());
if (jsonText.charAt(0) == '[') {
result = new JSONArray(jsonText.toString());
} else if (jsonText.toString().equals("null")) {
result = new JSONArray();
} else {
result = new JSONArray().put(new JSONObject(jsonText.toString()));
}
} else {
throw new Exception();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
}
return result;
}
@Override
protected void onPostExecute(JSONArray players) {
playerList.clear();
if (players == null) {
Toast.makeText(MainActivity.this, getString(R.string.connection_error), Toast.LENGTH_SHORT).show();
} else if (players.length() == 0) {
Toast.makeText(MainActivity.this, getString(R.string.no_results_error), Toast.LENGTH_SHORT).show();
} else {
convertJSONtoArrayList(players);
}
MainActivity.this.updateDisplay();
}
}
/**
* Inner class for PUTing existing players asynchronously
* This is for testing purposes only.
*/
private class PutPlayerTask extends AsyncTask<URL, Void, JSONArray> {
@Override
protected JSONArray doInBackground(URL... params) {
HttpURLConnection connection = null;
StringBuilder jsonText = new StringBuilder();
JSONArray result = null;
try {
JSONObject jsonData = new JSONObject();
jsonData.put("emailAddress", "new@calvin.edu");
connection = (HttpURLConnection) params[0].openConnection();
connection.setRequestMethod("PUT");
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/json");
connection.setFixedLengthStreamingMode(jsonData.toString().length());
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.writeBytes(jsonData.toString());
out.flush();
out.close();
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
BufferedReader reader = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
jsonText.append(line);
}
//Log.d(TAG, jsonText.toString());
if (jsonText.charAt(0) == '[') {
result = new JSONArray(jsonText.toString());
} else if (jsonText.toString().equals("null")) {
result = new JSONArray();
} else {
result = new JSONArray().put(new JSONObject(jsonText.toString()));
}
} else {
throw new Exception();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
}
return result;
}
@Override
protected void onPostExecute(JSONArray players) {
playerList.clear();
if (players == null) {
Toast.makeText(MainActivity.this, getString(R.string.connection_error), Toast.LENGTH_SHORT).show();
} else if (players.length() == 0) {
Toast.makeText(MainActivity.this, getString(R.string.no_results_error), Toast.LENGTH_SHORT).show();
} else {
convertJSONtoArrayList(players);
}
MainActivity.this.updateDisplay();
}
}
/**
* Inner class for DELETEing existing players asynchronously
* This is for testing purposes only.
*/
private class DeletePlayerTask extends AsyncTask<URL, Void, JSONArray> {
@Override
protected JSONArray doInBackground(URL... params) {
HttpURLConnection connection = null;
StringBuilder jsonText = new StringBuilder();
JSONArray result = null;
try {
connection = (HttpURLConnection) params[0].openConnection();
connection.setRequestMethod("DELETE");
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
BufferedReader reader = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
jsonText.append(line);
}
//Log.d(TAG, jsonText.toString());
if (jsonText.charAt(0) == '[') {
result = new JSONArray(jsonText.toString());
} else if (jsonText.toString().equals("null")) {
result = new JSONArray();
} else {
result = new JSONArray().put(new JSONObject(jsonText.toString()));
}
} else {
throw new Exception();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
}
return result;
}
@Override
protected void onPostExecute(JSONArray players) {
playerList.clear();
if (players == null) {
Toast.makeText(MainActivity.this, getString(R.string.connection_error), Toast.LENGTH_SHORT).show();
} else if (players.length() == 0) {
Toast.makeText(MainActivity.this, getString(R.string.no_results_error), Toast.LENGTH_SHORT).show();
} else {
convertJSONtoArrayList(players);
}
MainActivity.this.updateDisplay();
}
}
/**
* Converts the JSON player data to an arraylist suitable for a listview adapter
*
* @param players JSON array of player objects
*/
private void convertJSONtoArrayList(JSONArray players) {
Log.d(TAG, players.toString());
try {
for (int i = 0; i < players.length(); i++) {
JSONObject player = players.getJSONObject(i);
playerList.add(new Player(
player.getLong("id"),
player.optString("name", "no name"),
player.optString("emailAddress", "no email")
));
}
} catch (JSONException e) {
e.printStackTrace();
}
Log.d(TAG, playerList.toString());
}
/**
* Refresh the weather data on the forecast ListView through a simple adapter
*/
private void updateDisplay() {
if (playerList == null) {
Toast.makeText(MainActivity.this, getString(R.string.connection_error), Toast.LENGTH_SHORT).show();
}
ArrayList<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
for (Player item : playerList) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("id", numberFormat.format(item.getId()));
map.put("name", item.getName());
map.put("emailAddress", item.getEmailAddress());
data.add(map);
}
int resource = R.layout.player_item;
String[] from = {"id", "name", "emailAddress"};
int[] to = {R.id.idTextView, R.id.nameTextView, R.id.emailAddressTextView};
SimpleAdapter adapter = new SimpleAdapter(this, data, resource, from, to);
itemsListView.setAdapter(adapter);
}
}