-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.java
160 lines (129 loc) · 5.51 KB
/
App.java
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
package com.github.ttdyce;
import java.io.IOException;
import java.util.ArrayList;
import com.github.ttdyce.model.Comic;
import com.github.ttdyce.model.api.NHAPI;
import com.github.ttdyce.model.api.ResponseCallback;
import com.github.ttdyce.model.factory.NHApiComicFactory;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
public class App {
public static void main(String[] args) {
NHAPI api = new NHAPI();
/*
* NHAPI usage demo, get comic list
**/
final ArrayList<Comic> list = new ArrayList<>();
ResponseCallback comicListReturnCallback = new ResponseCallback() {
@Override
public void onReponse(String response) {
JsonArray array = new JsonParser().parse(response).getAsJsonArray();
Gson gson = new Gson();
for (JsonElement jsonElement : array) {
Comic c = gson.fromJson(jsonElement, Comic.class);
list.add(c);
}
}
@Override
public void onError(String error) {
System.err.println("Error accessing comic list! This probably means that NHentai's DDoS protection is blocking us. API returned a \"" + error + "\"");
}
};
//end of callback
try {
api.getComicList(comicListReturnCallback);//index page 1
if (!list.isEmpty()) {
for (Comic comic : list) {
System.out.println(comic.getTitle());
}
System.out.println("*********");
System.out.println("");
}
api.getComicList("english", "", 1, true, comicListReturnCallback);//english comic page 1, sort by popularity
if (!list.isEmpty()) {
for (Comic comic : list) {
System.out.println(comic.getTitle());
}
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("");
System.out.println("---Next demo---");
System.out.println("");
if (list.isEmpty()) {
System.out.println("Error getting comic list. Cannot try comic demo.");
} else {
/*
* NHAPI usage demo, get comic
**/
int id = list.get(0).getId();
ResponseCallback comicReturnCallback = new ResponseCallback() {
@Override
public void onReponse(String response) {
JsonObject object = new JsonParser().parse(response).getAsJsonObject();
Gson gson = new Gson();
Comic comic = gson.fromJson(object, Comic.class);
//here is what a comic looks like
System.out.println(gson.toJson(comic));
}
@Override
public void onError(String error) {
System.err.println("Error accessing comic! This probably means that NHentai's DDoS protection is blocking us. API returned a \"" + error + "\"");
}
};
//end of callback
try {
api.getComic(id, comicReturnCallback);
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("");
System.out.println("---Next demo---");
System.out.println("");
list.clear();
/*
* NHApiComicFactory usage demo
**/
ResponseCallback onFactoryComicListReturn = new ResponseCallback() {
@Override
public void onReponse(String response) {
JsonArray array = new JsonParser().parse(response).getAsJsonArray();
Gson gson = new Gson();
Boolean hasNextPage = true;
if (array.size() == 0)
hasNextPage = false;
else if (array.size() != 25) {
hasNextPage = false;
for (JsonElement jsonElement : array) {
Comic c = gson.fromJson(jsonElement, Comic.class);
list.add(c);
}
} else {
hasNextPage = true;
for (JsonElement jsonElement : array) {
Comic c = gson.fromJson(jsonElement, Comic.class);
list.add(c);
}
}
}
@Override public void onError(String error) {
System.err.println("Error demoing Comic Factory! This probably means that NHentai's DDoS protection is blocking us. API returned a \"" + error + "\"");
}
};
//end of callback
NHApiComicFactory factory = new NHApiComicFactory("english", " ", 1, true, onFactoryComicListReturn);
factory.requestComicList();// trigger onFactoryComicListReturn.onResponse(response)
if (list.isEmpty()) {
System.out.println("There are no comics to demo.");
} else {
for (Comic comic : list) {
System.out.println(comic.getTitle());
}
}
}
}