Skip to content

Commit

Permalink
network request optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
mxdldev committed Mar 23, 2020
1 parent d6e55a8 commit a36968b
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 5 deletions.
6 changes: 6 additions & 0 deletions lib_fly_api/src/main/java/com/fly/tour/api/CommonService.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package com.fly.tour.api;

import com.fly.tour.api.dto.RespDTO;
import com.fly.tour.api.security.Token;
import com.fly.tour.api.user.LoginDTO;

import io.reactivex.Observable;
import retrofit2.http.Header;
import retrofit2.http.POST;
import retrofit2.http.Query;

public interface CommonService {
@POST("/userapi/user/login")
Observable<RespDTO<LoginDTO>> login(@Query("username") String name, @Query("password") String pwd);

@POST("/oauth/token")
Observable<Token> getToken(@Header(value = "Authorization") String authorization, @Query("grant_type") String type,
@Query("username") String username, @Query("password") String password);
}
19 changes: 15 additions & 4 deletions lib_fly_api/src/main/java/com/fly/tour/api/RetrofitManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
import com.fly.tour.api.util.SSLContextUtil;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;

import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
Expand Down Expand Up @@ -41,19 +43,28 @@ private RetrofitManager() {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
okHttpBuilder = new OkHttpClient.Builder();
okHttpBuilder.connectTimeout(20 * 1000, TimeUnit.MILLISECONDS).
readTimeout(20 * 1000, TimeUnit.MILLISECONDS).
writeTimeout(20 * 1000, TimeUnit.MILLISECONDS);

okHttpBuilder.addInterceptor(logging);
okHttpBuilder.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
if(!TextUtils.isEmpty(mToken)){
Request.Builder requestBuilder = request.newBuilder()
.header("Authorization", "Bearer " + mToken);
request = requestBuilder.build();

if (!TextUtils.isEmpty(mToken)) {
//Request.Builder requestBuilder = request.newBuilder()
//.header("Authorization", "Bearer " + mToken);
//request = requestBuilder.build();
Request.Builder requestBuilder = request.newBuilder();
HttpUrl.Builder urlBuilder = request.url().newBuilder().addQueryParameter("access_token", mToken);
request = requestBuilder.url(urlBuilder.build()).build();
}
return chain.proceed(request);
}
});

//给client的builder添加了一个socketFactory
SSLContext sslContext = SSLContextUtil.getDefaultSLLContext();
if (sslContext != null) {
Expand Down
3 changes: 2 additions & 1 deletion lib_fly_api/src/main/java/com/fly/tour/api/config/API.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
* Update: <br>
*/
public class API {
public static final String URL_HOST_USER = "http://192.168.31.105:5000";
//public static final String URL_HOST_USER = "http://192.168.31.105:8080";
public static final String URL_HOST_USER = "http://106.12.61.238:8080";
}
56 changes: 56 additions & 0 deletions lib_fly_api/src/main/java/com/fly/tour/api/security/Token.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.fly.tour.api.security;

/**
* Description: <Token><br>
* Author: mxdl<br>
* Date: 2020/3/19<br>
* Version: V1.0.0<br>
* Update: <br>
*/
public class Token {
private String access_token;
private String token_type;
private String refresh_token;
private String expires_in;
private String scope;

public String getAccess_token() {
return access_token;
}

public void setAccess_token(String access_token) {
this.access_token = access_token;
}

public String getToken_type() {
return token_type;
}

public void setToken_type(String token_type) {
this.token_type = token_type;
}

public String getRefresh_token() {
return refresh_token;
}

public void setRefresh_token(String refresh_token) {
this.refresh_token = refresh_token;
}

public String getExpires_in() {
return expires_in;
}

public void setExpires_in(String expires_in) {
this.expires_in = expires_in;
}

public String getScope() {
return scope;
}

public void setScope(String scope) {
this.scope = scope;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.fly.tour.api.RetrofitManager;
import com.fly.tour.api.dto.RespDTO;
import com.fly.tour.api.http.RxAdapter;
import com.fly.tour.api.security.Token;
import com.fly.tour.api.user.LoginDTO;
import com.fly.tour.common.mvvm.model.BaseModel;

Expand Down Expand Up @@ -35,4 +36,9 @@ public Observable<RespDTO<LoginDTO>> login(String username, String password) {
.compose(RxAdapter.schedulersTransformer())
.compose(RxAdapter.exceptionTransformer());
}
public Observable<Token> getToken(String username, String password) {
return mCommonService.getToken("Basic bXhkbC1jbGllbnQ6bXhkbC1zZWNyZXQ=","password",username, password)
.compose(RxAdapter.schedulersTransformer())
.compose(RxAdapter.exceptionTransformer());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.fly.tour.api.RetrofitManager;
import com.fly.tour.api.dto.RespDTO;
import com.fly.tour.api.http.ExceptionHandler;
import com.fly.tour.api.security.Token;
import com.fly.tour.api.user.LoginDTO;
import com.fly.tour.common.event.SingleLiveEvent;
import com.fly.tour.common.mvvm.viewmodel.BaseViewModel;
Expand All @@ -31,6 +32,34 @@ public SplashViewModel(@NonNull Application application, SplashModel model) {
super(application, model);
}
public void login() {
mModel.getToken("mxdl","123456").subscribe(new Observer<Token>() {
@Override
public void onSubscribe(Disposable d) {

}

@Override
public void onNext(Token token) {
if(token != null){
RetrofitManager.getInstance().addToken(token.getAccess_token());
}else{
Log.v(TAG,"token is null");
}
}

@Override
public void onError(Throwable e) {
getmVoidSingleLiveEvent().call();
}

@Override
public void onComplete() {
getmVoidSingleLiveEvent().call();
}
});
}

private void login1() {
mModel.login("gxl","123456").subscribe(new Observer<RespDTO<LoginDTO>>() {
@Override
public void onSubscribe(Disposable d) {
Expand Down Expand Up @@ -58,6 +87,7 @@ public void onComplete() {
}
});
}

public SingleLiveEvent<Void> getmVoidSingleLiveEvent() {
return mVoidSingleLiveEvent = createLiveData(mVoidSingleLiveEvent);
}
Expand Down

0 comments on commit a36968b

Please sign in to comment.