Skip to content

Commit eb7640e

Browse files
author
tangsiyuan
committed
集成网络框架Retrofit2+Okhttp3+Gson
1 parent 58866bb commit eb7640e

18 files changed

Lines changed: 253 additions & 1 deletion

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,12 @@
11
# BaseAndroidProject
2-
Android项目基础架构。包含架构分层、基本工具层等
2+
Android项目基础架构。包含架构分层、基本工具层等, 持续更新中
3+
4+
### 架构
5+
6+
基于MVP架构进行分层
7+
8+
### 底层库
9+
10+
1. 网络库-Retrofit2+Okhttp3+Gson
11+
12+
详细使用见文章:http://www.jianshu.com/p/283d1a7a0aff

app/build.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,10 @@ dependencies {
2323
compile fileTree(dir: 'libs', include: ['*.jar'])
2424
testCompile 'junit:junit:4.12'
2525
compile 'com.android.support:appcompat-v7:23.4.0'
26+
27+
//网络请求 retrofit+okhttp+gson
28+
compile 'com.squareup.retrofit2:retrofit:2.1.0'
29+
compile 'com.squareup.okhttp3:okhttp:3.4.1'
30+
compile 'com.google.code.gson:gson:2.7'
31+
compile 'com.squareup.retrofit2:converter-gson:2.0.2';
2632
}

app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="com.tsy.baseandroidproject">
44

5+
<uses-permission android:name="android.permission.INTERNET" />
6+
57
<application
68
android:name=".GlobalApp"
79
android:allowBackup="true"
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.tsy.baseandroidproject.Base;
2+
3+
import retrofit2.Call;
4+
import retrofit2.Callback;
5+
import retrofit2.Response;
6+
import retrofit2.Retrofit;
7+
import retrofit2.converter.gson.GsonConverterFactory;
8+
9+
/**
10+
* BaseApi
11+
* Created by tsy on 16/7/21.
12+
*/
13+
public abstract class BaseApi {
14+
15+
private static final String mBaseUrl = "http://www.baidu.com/";
16+
17+
protected Retrofit mRetrofit;
18+
19+
20+
public BaseApi() {
21+
mRetrofit = new Retrofit.Builder()
22+
.baseUrl(mBaseUrl)
23+
.addConverterFactory(GsonConverterFactory.create())
24+
.build();
25+
26+
}
27+
28+
public BaseApi(String baseUrl) {
29+
mRetrofit = new Retrofit.Builder()
30+
.baseUrl(baseUrl)
31+
.addConverterFactory(GsonConverterFactory.create())
32+
.build();
33+
34+
}
35+
36+
//处理retrofit回调 并调用ApiCallback相应返回
37+
protected <T> Callback<T> callBack(T t, final ApiCallback callback) {
38+
return new Callback<T>() {
39+
@Override
40+
public void onResponse(Call<T> call, Response<T> response) {
41+
if(response.isSuccessful()) {
42+
if(((BaseDataRet)response.body()).ret == 1) {
43+
callback.onSuccess(((BaseDataRet)response.body()));
44+
} else {
45+
callback.onError(((BaseDataRet)response.body()).err_code, ((BaseDataRet)response.body()).err_msg);
46+
}
47+
} else {
48+
callback.onFailure();
49+
}
50+
}
51+
52+
@Override
53+
public void onFailure(Call<T> call, Throwable t) {
54+
callback.onFailure();
55+
}
56+
};
57+
}
58+
59+
public interface ApiCallback {
60+
void onSuccess(BaseDataRet ret); //ret=1时返回
61+
void onError(int err_code, String err_msg); //ret=0时返回
62+
void onFailure(); //网络请求失败
63+
}
64+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.tsy.baseandroidproject.Base;
2+
3+
/**
4+
* Gson返回Ret基本格式
5+
* 成功:ret=1 + 业务数据
6+
* 失败:ret=0 + err_code + err_msg
7+
* Created by tsy on 16/7/21.
8+
*/
9+
public class BaseDataRet {
10+
public int ret; //成功-1 失败-0
11+
public int err_code; //错误code
12+
public String err_msg; //错误msg
13+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.tsy.baseandroidproject.Login.data;
2+
3+
import com.tsy.baseandroidproject.Base.BaseDataRet;
4+
5+
/**
6+
* Created by tsy on 16/7/18.
7+
*/
8+
public class LoginRetData extends BaseDataRet {
9+
10+
public int user_id;
11+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.tsy.baseandroidproject.Login.model;
2+
3+
import com.tsy.baseandroidproject.Base.BaseApi;
4+
import com.tsy.baseandroidproject.Login.data.LoginRetData;
5+
6+
import retrofit2.Call;
7+
import retrofit2.http.GET;
8+
9+
/**
10+
* Created by tsy on 16/7/18.
11+
*/
12+
public class LoginApi extends BaseApi {
13+
14+
private static final String mBaseUrl = "http://192.168.3.1/";
15+
16+
private ApiStore mApiStore;
17+
18+
public LoginApi() {
19+
super(mBaseUrl);
20+
mApiStore = mRetrofit.create(ApiStore.class);
21+
}
22+
23+
public void login(String username, String password, ApiCallback callback) {
24+
Call<LoginRetData> call = ((ApiStore)mApiStore).login();
25+
call.enqueue(callBack(new LoginRetData(), callback));
26+
}
27+
28+
public interface ApiStore {
29+
@GET("test_retrofit.php")
30+
Call<LoginRetData> login();
31+
}
32+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.tsy.baseandroidproject.Login.model;
2+
3+
/**
4+
* Created by tsy on 16/7/18.
5+
*/
6+
public class LoginCache {
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.tsy.baseandroidproject.Login.model;
2+
3+
/**
4+
* Created by tsy on 16/7/18.
5+
*/
6+
public class RegisterApi {
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.tsy.baseandroidproject.Login.model;
2+
3+
/**
4+
* Created by tsy on 16/7/18.
5+
*/
6+
public class RegisterCache {
7+
}

0 commit comments

Comments
 (0)