blob: b6a7a39c0cc510732e9701a611191e93eb09b7c5 (
plain) (
blame)
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
|
package User;
import android.os.AsyncTask;
import android.util.Log;
import org.json.JSONException;
import org.json.JSONObject;
import ml.docilealligator.infinityforreddit.JSONUtils;
public class ParseUserData {
interface ParseUserDataListener {
void onParseUserDataSuccess(User user);
void onParseUserDataFail();
}
static void parseMyInfo(String response, ParseUserDataListener parseUserDataListener) {
new ParseUserDataAsyncTask(response, parseUserDataListener).execute();
}
private static class ParseUserDataAsyncTask extends AsyncTask<Void, Void, Void> {
private JSONObject jsonResponse;
private ParseUserDataListener parseUserDataListener;
private boolean parseFailed;
private User user;
ParseUserDataAsyncTask(String response, ParseUserDataListener parseUserDataListener){
try {
jsonResponse = new JSONObject(response);
this.parseUserDataListener = parseUserDataListener;
parseFailed = false;
} catch (JSONException e) {
Log.i("user data json error", e.getMessage());
parseUserDataListener.onParseUserDataFail();
}
}
@Override
protected Void doInBackground(Void... voids) {
try {
jsonResponse = jsonResponse.getJSONObject(JSONUtils.DATA_KEY);
String userName = jsonResponse.getString(JSONUtils.NAME_KEY);
String iconImageUrl = jsonResponse.getString(JSONUtils.ICON_IMG_KEY);
String bannerImageUrl = "";
if(!jsonResponse.isNull(JSONUtils.SUBREDDIT_KEY)) {
bannerImageUrl = jsonResponse.getJSONObject(JSONUtils.SUBREDDIT_KEY).getString(JSONUtils.BANNER_IMG_KEY);
}
int linkKarma = jsonResponse.getInt(JSONUtils.LINK_KARMA_KEY);
int commentKarma = jsonResponse.getInt(JSONUtils.COMMENT_KARMA_KEY);
int karma = linkKarma + commentKarma;
boolean isGold = jsonResponse.getBoolean(JSONUtils.IS_GOLD_KEY);
boolean isFriend = jsonResponse.getBoolean(JSONUtils.IS_FRIEND_KEY);
user = new User(userName, iconImageUrl, bannerImageUrl, karma, isGold, isFriend);
} catch (JSONException e) {
parseFailed = true;
Log.i("parse user data error", e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
if(!parseFailed) {
parseUserDataListener.onParseUserDataSuccess(user);
} else {
parseUserDataListener.onParseUserDataFail();
}
}
}
}
|