Login Model

Login Model

Library工程是 用户模块 包含用户信息

  • 登陆操作并保存用户信息到数据库

Setup

使用这个library工程minSdkVersion 最小版本必须 >= 16

在你工程根目录的build.gradle文件添加:

repositories {
    maven { url "http://192.168.2.63:8180/nexus/content/repositories/releases" }
  }

Setup2

在你需要使用的Model工程的build.gradle文件添加:

dependencies {
    compile 'com.gt.common:great-tao-photo:1.0.0@aar'
}

Usege

初始化

创建SSLSocketFactory对象:

public SSLSocketFactory setCertificates(InputStream... certificates) {
    try {
      CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
      KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
      keyStore.load(null);
      int index = 0;
      for (InputStream certificate : certificates) {
        String certificateAlias = Integer.toString(index++);
        keyStore.setCertificateEntry(certificateAlias,
            certificateFactory.generateCertificate(certificate));

        try {
          if (certificate != null) certificate.close();
        } catch (IOException e) {
        }
      }
      SSLContext sslContext = SSLContext.getInstance("TLS");
      TrustManagerFactory trustManagerFactory =
          TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());

      trustManagerFactory.init(keyStore);
      sslContext.init(null, trustManagerFactory.getTrustManagers(), new SecureRandom());
      return sslContext.getSocketFactory();
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }

提示 : 如果需要使用到 https 协议需要添加


创建 OkHttpClient 对象:

public OkHttpClient getOkhttp() {
    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient okHttpClient = null;
    try {
      okHttpClient = new OkHttpClient.Builder().addNetworkInterceptor(new StethoInterceptor())
          .addInterceptor(interceptor)
          .sslSocketFactory(setCertificates(getAssets().open("login.dev.egtcp.com.cer")))
          .build();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return okHttpClient;
  }

底层网络库使用OkHttp

  • 使用HttpLoggingInterceptor提供日志打印功能
  • 使用StethoInterceptor提供Chrome后台打印功能


创建Retrofit对象:

public Retrofit getRetrofit(OkHttpClient okHttpClient) {
    Retrofit retrofit = new Retrofit.Builder().baseUrl(LoginURL.getHostURL())
        .addConverterFactory(ScalarsConverterFactory.create())
        .addConverterFactory(GsonConverterFactory.create())
        .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
        .client(okHttpClient)
        .build();
    return retrofit;
  }

使用 Retrofit 进行Restful网络请求

  • 添加ScalarsConverterFactory.create()支持Retrofit返回 String 类型
  • 添加GsonConverterFactory.create()支持 Gson 库进行对象转换
  • 添加RxJavaCallAdapterFactory.create()支持Rxjava


初始化LoginConfig对象

public void initLogin() {
    OkHttpClient okHttpClient = getOkhttp();
    LoginConfig.getInstance().setOkHttpClient(okHttpClient).setRetrofit(getRetrofit(okHttpClient));
    LoginConfig.getInstance().setL(new L() {
      @Override public void i(String msg) {
        com.gt.common.hybrid.utils.L.i(msg);
      }

      @Override public void e(String msg) {
        com.gt.common.hybrid.utils.L.e(msg);
      }
    });
  }

提示 :

  • setOkHttpClientsetRetrofit方法必须要初化
  • 当项目中需要增加日志功能可以使用setL方法进行注册


AndroidManifest.xml文件中增加:



需要的权限:

  
  
  
  
  
  
  
  


Example

调用登陆接口

initLogin();
findViewById(R.id.login).setOnClickListener(v -> {
      LoginActivity.start(getBaseContext());
    });

提示 : 当前Hello World是以Java8为事例


获取用户信息:

Realm instance = Realm.getDefaultInstance();
    RealmResults all = instance.where(UserDB.class).findAll();
    for (UserDB user : all) {
      String userUsername = user.getUsername();
      String password = user.getPassword();
      String tgt = user.getTGT();
      String token = user.getToken();
    }
instance.close();


提示 : 如果当 Model 工程没有安装Realm数据库环境请参照以下:

  • 根目录build.gradle增加
classpath "io.realm:realm-gradle-plugin:2.2.1"

  • 当前 Model 工程的build.gradle增加
apply plugin: 'realm-android'


代码混淆

-dontwarn com.gt.common.login.**
-keep class com.gt.common.login.**{*;}

你可能感兴趣的:(Login Model)