Skip to content

Commit

Permalink
parser example code added in readme
Browse files Browse the repository at this point in the history
  • Loading branch information
amitshekhariitbhu committed Jul 31, 2016
1 parent 62c190c commit 9a7e739
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 2 deletions.
51 changes: 49 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ AndroidNetworking.get("http://api.localhost.com/{pageNumber}/test")
public void onError(ANError error) {
// handle error
}
});
});
```
### Making a POST Request
```java
Expand Down Expand Up @@ -173,6 +173,54 @@ AndroidNetworking.post("http://api.localhost.com/postFile")
}
});
```

### Using it with your own JAVA Object - JSON Parser
```
/*--------------Example One -> Getting the userList----------------*/
AndroidNetworking.get("http://api.localhost.com/getAllUsers/{pageNumber}")
.addPathParameter("pageNumber", "0")
.addQueryParameter("limit", "3")
.setTag(this)
.setPriority(Priority.LOW)
.build()
.getAsParsed(new TypeToken<List<User>>() {}, new ParsedRequestListener<List<User>>() {
@Override
public void onResponse(List<User> users) {
// do anything with response
Log.d(TAG, "userList size : " + users.size());
for (User user : users) {
Log.d(TAG, "id : " + user.id);
Log.d(TAG, "firstname : " + user.firstname);
Log.d(TAG, "lastname : " + user.lastname);
}
}
@Override
public void onError(ANError anError) {
// handle error
}
});
/*--------------Example Two -> Getting an user----------------*/
AndroidNetworking.get("http://api.localhost.com/getAnUser/{userId}")
.addPathParameter("userId", "1")
.setTag(this)
.setPriority(Priority.LOW)
.build()
.getAsParsed(new TypeToken<User>() {}, new ParsedRequestListener<User>() {
@Override
public void onResponse(User user) {
// do anything with response
Log.d(TAG, "id : " + user.id);
Log.d(TAG, "firstname : " + user.firstname);
Log.d(TAG, "lastname : " + user.lastname);
}
@Override
public void onError(ANError anError) {
// handle error
}
});
/*-- Note : TypeToken and getAsParsed is important here--*/
```

### Downloading a file from server
```java
AndroidNetworking.download(url,dirPath,fileName)
Expand Down Expand Up @@ -489,7 +537,6 @@ AndroidNetworking.initialize(getApplicationContext(),okHttpClient);
### TODO
* Integration with other library
* And of course many many features and bug fixes
* Json Parser

### CREDITS
* [Square](https://square.github.io/) - As both [OkHttp](http://square.github.io/okhttp/) and [Okio](https://github.com/square/okio)
Expand Down
58 changes: 58 additions & 0 deletions RxAndroidNetworking.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,64 @@ RxAndroidNetworking.download(url,dirPath,fileName)
});
```

### Using it with your own JAVA Object - JSON Parser
```
/*--------------Example One -> Getting the userList----------------*/
RxAndroidNetworking.get("http://api.localhost.com/getAllUsers/{pageNumber}")
.addPathParameter("pageNumber", "0")
.addQueryParameter("limit", "3")
.build()
.getParseObservable(new TypeToken<List<User>>() {})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<List<User>>() {
@Override
public void onCompleted() {
// do anything onComplete
}
@Override
public void onError(Throwable e) {
// handle error
}
@Override
public void onNext(List<User> users) {
// do anything with response
Log.d(TAG, "userList size : " + users.size());
for (User user : users) {
Log.d(TAG, "id : " + user.id);
Log.d(TAG, "firstname : " + user.firstname);
Log.d(TAG, "lastname : " + user.lastname);
}
}
});
/*--------------Example Two -> Getting an user----------------*/
RxAndroidNetworking.get("http://api.localhost.com/getAnUser/{userId}")
.addPathParameter("userId", "1")
.setUserAgent("getAnUser")
.build()
.getParseObservable(new TypeToken<User>() {})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<User>() {
@Override
public void onCompleted() {
// do anything onComplete
}
@Override
public void onError(Throwable e) {
// handle error
}
@Override
public void onNext(User user) {
// do anything with response
Log.d(TAG, "id : " + user.id);
Log.d(TAG, "firstname : " + user.firstname);
Log.d(TAG, "lastname : " + user.lastname);
}
});
/*-- Note : TypeToken and getParseObservable is important here--*/
```

### Uploading a file to server
```java
RxAndroidNetworking.upload("http://api.localhost.com/uploadImage")
Expand Down

0 comments on commit 9a7e739

Please sign in to comment.