Retrofit:
1. 解析Json结构
2. 制作放置Json数据的类
3. 定义网络请求接口
4. 创建Retrofit实例(指定Url、Gson、RxJava)
5. 创Retrofit实例建网络请求接口实例
6. 用网络接口实例创建定义在接口里的回调类实例
7. 用回调类实例实现异步或同步请求
例子:
接口的定义:
public interface GetRequest_party {
@GET("party/{id}")
Call<ResponseBody> getPartyInformation(@Path("id") String id);
}
网络请求:
Retrofit retrofit = new Retrofit.Builder()同步:
.baseUrl("http://120.24.168.102:8080/")
.addConverterFactory(GsonConverterFactory.create())
.build();
GetRequset_check_Interface checkInterface = retrofit.create(GetRequset_check_Interface.class);
Call<ResultForCheck> call = checkInterface.getCall(ecApplication.sessionId);
Response<ResultForCheck> response = null;
try {
response = call.execute();
if (response.isSuccessful()) {
if (response.body().getStatus() == 201){
//重新登陆
Toast.makeText(MainActivity.this,"请重新登陆",Toast.LENGTH_LONG).show();
Retrofit retrofit1 = new Retrofit.Builder().baseUrl("http://120.24.168.102:8080/")
.addConverterFactory(GsonConverterFactory.create())
.build();
PostRequest_login_Interface login_interface = retrofit1.create(PostRequest_login_Interface.class);
Call<ResultForLogin> call1 = login_interface.getCall(username,password);
try {
Response<ResultForLogin> response1 = call1.execute();
if (response1.isSuccessful()) {
if (response1.body().getStatus() == 100)
{
Toast.makeText(MainActivity.this,"重新登陆成功",Toast.LENGTH_LONG).show();
Log.e("test","返回来的sid:"+response1.body().getSessionid());
((ECApplication) getApplication()).sessionId = response1.body().getSessionid();
Log.e("test","此时的sid:" + ((ECApplication) getApplication()).sessionId );
}
}else {
Toast.makeText(MainActivity.this,"重新登陆失败",Toast.LENGTH_LONG).show();
}
} catch (IOException e) {
e.printStackTrace();
}
}
if (response.body().getStatus() == 200){
Toast.makeText(MainActivity.this,"已经登陆",Toast.LENGTH_LONG).show();
}
}else {
Toast.makeText(MainActivity.this,"访问服务器失败",Toast.LENGTH_LONG).show();
}
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(MainActivity.this,"登陆超时",Toast.LENGTH_LONG).show();
}
异步:
call.enqueue(new Callback<ResultForCheck>() {
@Override
public void onResponse(Call<ResultForCheck> call, Response<ResultForCheck> response) {
if (response.isSuccessful()){
Toast.makeText(MainActivity.this,"访onResponse",Toast.LENGTH_LONG).show();
Log.e("test","访onResponseForCheck");
if (response.body().getStatus().equals("200")){
//重新登陆
Toast.makeText(MainActivity.this,"请重新登陆",Toast.LENGTH_LONG).show();
Log.e("test","请重新登陆");
Retrofit retrofit1 = new Retrofit.Builder().baseUrl("http://120.24.168.102:8080/")
.addConverterFactory(GsonConverterFactory.create())
.build();
PostRequest_login_Interface login_interface = retrofit1.create(PostRequest_login_Interface.class);
Call<ResultForLogin> call1 = login_interface.getCall(username,password);
call1.enqueue(new Callback<ResultForLogin>() {
@Override
public void onResponse(Call<ResultForLogin> call, Response<ResultForLogin> response) {
if (response.body().getStatus().equals("100"))
{
Toast.makeText(MainActivity.this,"重新登陆成功",Toast.LENGTH_LONG).show();
Log.e("test","返回来的sid:"+response.body().getSessionid());
((ECApplication) getApplication()).sessionId = response.body().getSessionid();
Log.e("test","此时的sid:" + ((ECApplication) getApplication()).sessionId );
}
}
@Override
public void onFailure(Call<ResultForLogin> call, Throwable t) {
}
});
}
if (response.body().equals("200")){
Toast.makeText(MainActivity.this,"已经登陆",Toast.LENGTH_LONG).show();
}
}else {
Toast.makeText(MainActivity.this,"访问服务器失败",Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(Call<ResultForCheck> call, Throwable t) {
Toast.makeText(MainActivity.this,"访onFailure",Toast.LENGTH_LONG).show();
}
});
RxJava:
1. 简单的异步:
使用create创建被观察者,在参数里传进OnSubscribe接口,在里面的call里完成相关操作和调用subscriber的onNext方法;
用subscribeOn指定subscribe发生的线程,用observeOn指定回调接口;
用subscribe创建subscriber,在subscriber里定义onNext的执行。
例子:
Observable.create(new Observable.OnSubscribe<String>() {
@Override
public void call(Subscriber<? super String> subscriber) {
try {
EMClient.getInstance().contactManager().declineInvitation(name);
} catch (HyphenateException e) {
e.printStackTrace();
}
subscriber.onNext(name);
}
}).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<String>() {
@Override
public void onCompleted() {
Log.e("test","onCompleted:");
Log.e("test", "执行一次Async完毕在监听器处");
}
@Override
public void onError(Throwable e) {
Log.e("test","执行异常");
}
@Override
public void onNext(String s) {
Log.e("test","拒绝好友 " +s);
Toast.makeText(ChooseYoNActivity.this,"拒绝好友 " +s,Toast.LENGTH_LONG).show();
finish();
}
});
2.RxJava和Retrofit的结合使用:
解析Json结构;制作放置Json数据的类;定义网络请求接口,不过在这里返回一个Observable的对象;创建Retrofit的实例,指定使用RxJava;用Retrofit的实例创建网络请求接口的实例;用网络请求接口的实例创建一个Observable的实例;使用Observable的实例执行1.中的步骤。
例子:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://120.24.168.102:8080/")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
GetRequest_friend_Interface request = retrofit.create(GetRequest_friend_Interface.class);
Log.e("test","在请求好友列表时的sessionid: "+((ECApplication)mContext.getApplicationContext()).sessionId);
Observable<FriendGot> call = request.getCall(((ECApplication)mContext.getApplicationContext()).sessionId);
call.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<FriendGot>() {
@Override
public void onCompleted() {
Log.e("testRetrofit","onCompleted");
}
@Override
public void onError(Throwable e) {
Log.e("testRetrofit","onError");
}
@Override
public void onNext(FriendGot friendGot) {
if (friendGot.getStatus().equals("300") ){
Log.e("testRetrofit",friendGot.getMsg());
Toast.makeText(mContext,"请求好友列表成功",Toast.LENGTH_LONG).show();
if (friendGot.friendList != null && friendGot.friendList.size() > 0){
for (ResultForFriend resultForFriend : friendGot.friendList){
Friends friends = new Friends(resultForFriend.getUserid(),resultForFriend.getFriendid());
mmFriendses.add(friends);
}
}
}else {
Log.e("testRetrofit","not 300");
Log.e("testRetrofit",friendGot.getMsg());
}
}
});