新项目由自己重头架构,因此使用MVP模式,库的使用上使用了Rxjava 2.0,OkHttp 3.0,Retrofit 2.0 及JDK1.8中的新特性lambda表达式。
1.Android Studio Gradle 配置
在project下的build.gradle中配置
dependencies {
classpath'com.android.tools.build:gradle:2.3.1'classpath'com.neenbedankt.gradle.plugins:android-apt:1.8'
classpath'me.tatarka:gradle-retrolambda:3.2.5'
}
在Module:app的build.gradle中配置
applyplugin:'com.android.application'
applyplugin:'com.neenbedankt.android-apt'
applyplugin:'me.tatarka.retrolambda'
sourceSets {
main {
jniLibs.srcDir'libs' //无需在java目录下新建jniLibs文件夹
}
}
compileOptions { //期盼2.4快点到,不用使用jack
sourceCompatibilityJavaVersion.VERSION_1_8
targetCompatibilityJavaVersion.VERSION_1_8
}
configurations.all {
resolutionStrategy.force'com.google.code.findbugs:jsr305:1.3.9'
}
dexOptions {
jumboMode =true
}
packagingOptions{
exclude'META-INF/rxjava.properties'
}
apt'com.jakewharton:butterknife-compiler:8.5.1'
compile'com.jakewharton:butterknife:8.5.1'
compile'com.google.dagger:dagger:2.6'
apt'com.google.dagger:dagger-compiler:2.6'
compile'io.reactivex.rxjava2:rxjava:2+'
compile'io.reactivex.rxjava2:rxandroid:2+'
compile'com.squareup.okhttp3:okhttp:3.3.1'
compile'com.squareup.okhttp3:logging-interceptor:3.3.1'
compile'com.google.code.gson:gson:2.8.0'
compile'com.squareup.retrofit2:retrofit:2+'
compile'com.squareup.retrofit2:converter-gson:2+'
compile'com.squareup.retrofit2:adapter-rxjava2:2+'
建立访问接口规则
public interfaceIHttpService {
/**上报设备信息**/
@FormUrlEncoded
@POST("device/info/save")
Observable
postDeviceInfo(@Field("jsonResult") String device); /**上报信号信息**/
@FormUrlEncoded
@POST("device/signal/save")
Observable
postSignalInfo(@Field("jsonResult") String signalList); /**上报APP详细记录**/
@FormUrlEncoded
@POST("sample/save")
@Observable
Observable
postDetialAppInfo(@Field("jsonResult") String signalList,@Field("catCode")String catCode); /**文件上传**/
@Multipart
@POST("upload/file")
Observable
postPcapFile(@PartMultipartBody.Part file,@Part("timetamp")String timetamp); @GET
Observable
getPGW(@UrlString url); }
实现接口
public static RetrofitService init() {
if(apiservice==null) {
OkHttpClient okHttpClient =newOkHttpClient
.Builder()
.addInterceptor(sLoggingInterceptor)
.retryOnConnectionFailure(true)
.connectTimeout(20,TimeUnit.SECONDS).build();
Retrofit retrofit =newRetrofit.Builder()
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.baseUrl(SERVICE_HOST)
.build();
Retrofit cmccretrofit =newRetrofit.Builder()
.client(okHttpClient)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.baseUrl(SERVICE_HOST)
.build();
apiservice= retrofit.create(IHttpService.class);
icmccApi= cmccretrofit.create(ICMCCapi.class);
}
returnInstanceRetrofit.INSTANCE;
}
POST JSON
public void postDeviceInfo(DeviceInfo record) {
String deviceInfo =newGsonBuilder().create().toJson(record);
apiservice.postDeviceInfo(deviceInfo)
.subscribeOn(Schedulers.io())
.unsubscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(response -> {
Logger.v("Next code %d",response.getCode());
Logger.v("Next message %s",response.getMessage());
if(response.getCode() ==200) {
postListSignalInfo(record.getTimetamp());
}
},error -> {error.printStackTrace();Logger.v("Error %s",error.getMessage());},() ->Logger.d("Complete "));}
public void postAppRecordInfo(String timeTamp) {
DeviceInfo info = DeviceInfo.where("timetamp like ? and isposted = 0",timeTamp).findFirst(DeviceInfo.class);
if(info !=null) {
postDeviceInfo(info);
}
}
UPLOAD FILE
public void postPcapFile(String timeTamp){
File pcapFile =newFile(PcapFileLoader.SDCARD_PCAP_LOG_FILE,timeTamp+"_capture.pcap");
if(pcapFile.exists()){
Uri fileUri = Uri.fromFile(pcapFile);
Log.d("fileRri",fileUri.toString());
RequestBody requestFile = RequestBody.create(MediaType.parse("application/pcap-stream"),pcapFile);
MultipartBody.Part body =
MultipartBody.Part.createFormData("file",pcapFile.getName(),requestFile);//file为接口参数,必须与后台接收一致
apiservice.postPcapFile(body,timeTamp)
.subscribeOn(Schedulers.io())
.unsubscribeOn(Schedulers.io())
.subscribe(request->{
Logger.v("next code %d",request.getCode());
Logger.v("next message %s",request.getMessage());
if(request.getCode() ==200){
DeviceInfoinfo= DataSupport.where("timetamp like ?",timeTamp).findFirst(DeviceInfo.class);
if(info!=null){
info.setIsposted(true);
info.save();
}
}
},error->Logger.e(error,"message %s",error.getMessage()),()->{
Logger.v("%s","postFileCompleted");
});
}
}