8
8
import android .os .AsyncTask ;
9
9
10
10
import org .json .JSONArray ;
11
+ import org .json .JSONException ;
11
12
import org .json .JSONObject ;
12
13
13
14
import java .io .BufferedReader ;
14
15
import java .io .InputStream ;
15
16
import java .io .InputStreamReader ;
17
+ import java .net .MalformedURLException ;
16
18
import java .net .URL ;
17
19
import java .util .ArrayList ;
18
20
import java .util .Arrays ;
21
23
import javax .net .ssl .HttpsURLConnection ;
22
24
23
25
class ApiRequestAsyncTask extends
24
- AsyncTask <Void , Void , ApiResponse []> {
26
+ AsyncTask <Void , Void , ApiNotebookResponse []> {
25
27
private final static String API_ROOT = "https://www.onenote.com/api/v1.0/" ;
26
28
public List <ApiAsyncResponse > delegates = new ArrayList <ApiAsyncResponse >(2 );
27
29
public ApiRequest [] mRequests ;
@@ -38,10 +40,10 @@ public void addDelegate(ApiAsyncResponse delegate) {
38
40
}
39
41
40
42
@ Override
41
- protected ApiResponse [] doInBackground (Void ... voids ) {
42
- List <ApiResponse > responseList = new ArrayList <ApiResponse >();
43
+ protected ApiNotebookResponse [] doInBackground (Void ... voids ) {
44
+ List <ApiNotebookResponse > responseList = new ArrayList <ApiNotebookResponse >();
43
45
for (ApiRequest request : mRequests ) {
44
- ApiResponse [] apiResponses ;
46
+ ApiNotebookResponse [] apiResponses ;
45
47
try {
46
48
apiResponses = requestResource (request );
47
49
} catch (Exception ex ) {
@@ -51,22 +53,27 @@ protected ApiResponse[] doInBackground(Void... voids) {
51
53
}
52
54
responseList .addAll (Arrays .asList (apiResponses ));
53
55
}
54
- return responseList .toArray (new ApiResponse [responseList .size ()]);
56
+ return responseList .toArray (new ApiNotebookResponse [responseList .size ()]);
55
57
}
56
58
57
- private ApiResponse [] requestResource (ApiRequest request ) throws Exception {
59
+ private ApiNotebookResponse [] requestResource (ApiRequest request ) throws Exception {
58
60
JSONObject responseJSON = getJSONResponse (API_ROOT + request .getEndpointURL ());
59
61
60
- List <ApiResponse > apiResponses = new ArrayList <ApiResponse >();
62
+ List <ApiNotebookResponse > notebookResponses = new ArrayList <ApiNotebookResponse >();
61
63
if (responseJSON != null ) {
62
64
// Determine if values were returned or an error occurred
63
65
if (responseJSON .has ("value" )) {
64
66
// Get response values
65
- JSONArray values = responseJSON .getJSONArray ("value" );
67
+ JSONArray notebookObjects = responseJSON .getJSONArray ("value" );
66
68
// Iterate over all values, and convert JSON objects into library objects
67
- for (int i = 0 ; i < values .length (); i ++) {
68
- JSONObject object = values .getJSONObject (i );
69
- apiResponses .add (buildApiResponse (request , object ));
69
+ for (int i = 0 ; i < notebookObjects .length (); i ++) {
70
+ JSONObject notebookObject = notebookObjects .getJSONObject (i );
71
+
72
+ ApiNotebookResponse notebook = new ApiNotebookResponse (notebookObject );
73
+ notebook .sections = getSections (notebookObject );
74
+ notebook .sectionGroups = getSectionGroups (notebookObject );
75
+
76
+ notebookResponses .add (notebook );
70
77
}
71
78
} else if (responseJSON .has ("error" )) {
72
79
// Process and throw an API error
@@ -85,50 +92,34 @@ private ApiResponse[] requestResource(ApiRequest request) throws Exception {
85
92
}
86
93
}
87
94
88
- return apiResponses .toArray (new ApiResponse [ apiResponses .size ()]);
95
+ return notebookResponses .toArray (new ApiNotebookResponse [ notebookResponses .size ()]);
89
96
}
90
-
91
- private ApiResponse buildApiResponse (ApiRequest request , JSONObject object ) throws Exception {
92
- ApiResponse apiResponse = null ;
93
- // Get links
94
- JSONObject links = object .optJSONObject ("links" );
95
- if (request .getPrimaryEndpoint () == ApiRequestEndpoint .NOTEBOOKS
96
- && !request .hasResourceId ()
97
- && !request .hasSecondaryEndpoint ()) {
98
- // Build notebook response
99
- apiResponse = new ApiNotebookResponse ();
100
- ((ApiNotebookResponse ) apiResponse ).setIsDefault (
101
- object .optBoolean ("isDefault" ));
102
- ((ApiNotebookResponse ) apiResponse ).setUserRole (
103
- object .optString ("userRole" ));
104
- ((ApiNotebookResponse ) apiResponse ).setOwnerName (
105
- object .optString ("ownerName" ));
106
- } else if (request .getSecondaryEndpoint () == ApiRequestEndpoint .SECTIONS ) {
107
- // Build section response
108
- apiResponse = new ApiSectionResponse ();
109
- ((ApiSectionResponse ) apiResponse ).setIsDefault (
110
- object .optBoolean ("isDefault" ));
111
- if (links != null ) {
112
- ((ApiSectionResponse ) apiResponse ).setPagesUrl (
113
- new URL (links .getJSONObject ("pagesUrl" ).getString ("href" )));
114
- } else {
115
- ((ApiSectionResponse ) apiResponse ).setPagesUrl (
116
- new URL (object .getString ("pagesUrl" )));
117
- }
118
-
119
- } else if (request .getSecondaryEndpoint () == ApiRequestEndpoint .SECTION_GROUPS ) {
120
- // Build section groups response
121
- apiResponse = new ApiSectionGroupResponse ();
97
+
98
+ private List <ApiSectionGroupResponse > getSectionGroups (JSONObject parentObject ) throws JSONException , MalformedURLException {
99
+ List <ApiSectionGroupResponse > sectionGroups = new ArrayList <ApiSectionGroupResponse >();
100
+ JSONArray sectionGroupObjects = parentObject .getJSONArray ("sectionGroups" );
101
+
102
+ for (int k = 0 ; k < sectionGroupObjects .length (); k ++)
103
+ {
104
+ JSONObject sectionGroupObject = sectionGroupObjects .getJSONObject (k );
105
+ ApiSectionGroupResponse sectionGroup = new ApiSectionGroupResponse (sectionGroupObject );
106
+ sectionGroup .sectionGroups = getSectionGroups (sectionGroupObject );
107
+ sectionGroup .sections = getSections (sectionGroupObject );
108
+ sectionGroups .add (sectionGroup );
122
109
}
123
- if (apiResponse != null ) {
124
- // Apply properties common to all response types
125
- apiResponse .setId (object .getString ("id" ));
126
- apiResponse .setName (object .getString ("name" ));
127
- apiResponse .setCreatedTime (object .optString ("createdTime" ));
128
- apiResponse .setModifiedTime (object .optString ("lastModifiedTime" ));
129
- apiResponse .setLastModifiedBy (object .optString ("lastModifiedBy" ));
110
+
111
+ return sectionGroups ;
112
+ }
113
+
114
+ private List <ApiSectionResponse > getSections (JSONObject parentObject ) throws JSONException , MalformedURLException
115
+ {
116
+ List <ApiSectionResponse > sections = new ArrayList <ApiSectionResponse >();
117
+ JSONArray sectionObjects = parentObject .getJSONArray ("sections" );
118
+ for (int k = 0 ; k < sectionObjects .length (); k ++)
119
+ {
120
+ sections .add (new ApiSectionResponse (sectionObjects .getJSONObject (k )));
130
121
}
131
- return apiResponse ;
122
+ return sections ;
132
123
}
133
124
134
125
@@ -184,7 +175,7 @@ private JSONObject getJSONResponse(String endpoint) throws Exception {
184
175
}
185
176
186
177
@ Override
187
- protected void onPostExecute (ApiResponse [] responses ) {
178
+ protected void onPostExecute (ApiNotebookResponse [] responses ) {
188
179
super .onPostExecute (responses );
189
180
for (ApiAsyncResponse delegate : delegates ) {
190
181
delegate .onApiResponse (responses , mCaughtException );
0 commit comments