Skip to content

Commit 9a7e739

Browse files
parser example code added in readme
1 parent 62c190c commit 9a7e739

File tree

2 files changed

+107
-2
lines changed

2 files changed

+107
-2
lines changed

README.md

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ AndroidNetworking.get("http://api.localhost.com/{pageNumber}/test")
110110
public void onError(ANError error) {
111111
// handle error
112112
}
113-
});
113+
});
114114
```
115115
### Making a POST Request
116116
```java
@@ -173,6 +173,54 @@ AndroidNetworking.post("http://api.localhost.com/postFile")
173173
}
174174
});
175175
```
176+
177+
### Using it with your own JAVA Object - JSON Parser
178+
```
179+
/*--------------Example One -> Getting the userList----------------*/
180+
AndroidNetworking.get("http://api.localhost.com/getAllUsers/{pageNumber}")
181+
.addPathParameter("pageNumber", "0")
182+
.addQueryParameter("limit", "3")
183+
.setTag(this)
184+
.setPriority(Priority.LOW)
185+
.build()
186+
.getAsParsed(new TypeToken<List<User>>() {}, new ParsedRequestListener<List<User>>() {
187+
@Override
188+
public void onResponse(List<User> users) {
189+
// do anything with response
190+
Log.d(TAG, "userList size : " + users.size());
191+
for (User user : users) {
192+
Log.d(TAG, "id : " + user.id);
193+
Log.d(TAG, "firstname : " + user.firstname);
194+
Log.d(TAG, "lastname : " + user.lastname);
195+
}
196+
}
197+
@Override
198+
public void onError(ANError anError) {
199+
// handle error
200+
}
201+
});
202+
/*--------------Example Two -> Getting an user----------------*/
203+
AndroidNetworking.get("http://api.localhost.com/getAnUser/{userId}")
204+
.addPathParameter("userId", "1")
205+
.setTag(this)
206+
.setPriority(Priority.LOW)
207+
.build()
208+
.getAsParsed(new TypeToken<User>() {}, new ParsedRequestListener<User>() {
209+
@Override
210+
public void onResponse(User user) {
211+
// do anything with response
212+
Log.d(TAG, "id : " + user.id);
213+
Log.d(TAG, "firstname : " + user.firstname);
214+
Log.d(TAG, "lastname : " + user.lastname);
215+
}
216+
@Override
217+
public void onError(ANError anError) {
218+
// handle error
219+
}
220+
});
221+
/*-- Note : TypeToken and getAsParsed is important here--*/
222+
```
223+
176224
### Downloading a file from server
177225
```java
178226
AndroidNetworking.download(url,dirPath,fileName)
@@ -489,7 +537,6 @@ AndroidNetworking.initialize(getApplicationContext(),okHttpClient);
489537
### TODO
490538
* Integration with other library
491539
* And of course many many features and bug fixes
492-
* Json Parser
493540

494541
### CREDITS
495542
* [Square](https://square.github.io/) - As both [OkHttp](http://square.github.io/okhttp/) and [Okio](https://github.com/square/okio)

RxAndroidNetworking.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,64 @@ RxAndroidNetworking.download(url,dirPath,fileName)
8989
});
9090
```
9191

92+
### Using it with your own JAVA Object - JSON Parser
93+
```
94+
/*--------------Example One -> Getting the userList----------------*/
95+
RxAndroidNetworking.get("http://api.localhost.com/getAllUsers/{pageNumber}")
96+
.addPathParameter("pageNumber", "0")
97+
.addQueryParameter("limit", "3")
98+
.build()
99+
.getParseObservable(new TypeToken<List<User>>() {})
100+
.subscribeOn(Schedulers.io())
101+
.observeOn(AndroidSchedulers.mainThread())
102+
.subscribe(new Observer<List<User>>() {
103+
@Override
104+
public void onCompleted() {
105+
// do anything onComplete
106+
}
107+
@Override
108+
public void onError(Throwable e) {
109+
// handle error
110+
}
111+
@Override
112+
public void onNext(List<User> users) {
113+
// do anything with response
114+
Log.d(TAG, "userList size : " + users.size());
115+
for (User user : users) {
116+
Log.d(TAG, "id : " + user.id);
117+
Log.d(TAG, "firstname : " + user.firstname);
118+
Log.d(TAG, "lastname : " + user.lastname);
119+
}
120+
}
121+
});
122+
/*--------------Example Two -> Getting an user----------------*/
123+
RxAndroidNetworking.get("http://api.localhost.com/getAnUser/{userId}")
124+
.addPathParameter("userId", "1")
125+
.setUserAgent("getAnUser")
126+
.build()
127+
.getParseObservable(new TypeToken<User>() {})
128+
.subscribeOn(Schedulers.io())
129+
.observeOn(AndroidSchedulers.mainThread())
130+
.subscribe(new Observer<User>() {
131+
@Override
132+
public void onCompleted() {
133+
// do anything onComplete
134+
}
135+
@Override
136+
public void onError(Throwable e) {
137+
// handle error
138+
}
139+
@Override
140+
public void onNext(User user) {
141+
// do anything with response
142+
Log.d(TAG, "id : " + user.id);
143+
Log.d(TAG, "firstname : " + user.firstname);
144+
Log.d(TAG, "lastname : " + user.lastname);
145+
}
146+
});
147+
/*-- Note : TypeToken and getParseObservable is important here--*/
148+
```
149+
92150
### Uploading a file to server
93151
```java
94152
RxAndroidNetworking.upload("http://api.localhost.com/uploadImage")

0 commit comments

Comments
 (0)