Skip to content

Commit b37a96a

Browse files
committed
[独立出新的Android Lib]
1 parent 1129a0a commit b37a96a

22 files changed

Lines changed: 215 additions & 43 deletions

File tree

fuHttp/app/build.gradle

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ android {
2020
}
2121

2222
dependencies {
23-
implementation fileTree(dir: 'libs', include: ['*.jar'])
23+
implementation fileTree(include: ['*.jar'], dir: 'libs')
2424
implementation 'com.android.support:appcompat-v7:26.1.0'
2525
implementation 'com.android.support:design:26.1.0'
2626
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
@@ -33,13 +33,5 @@ dependencies {
3333
releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.4'
3434
implementation 'com.jakewharton:butterknife:8.8.1'
3535
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
36-
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
37-
implementation 'com.google.code.gson:gson:2.6.2'
38-
implementation 'com.squareup.retrofit2:converter-gson:2.1.0'
39-
implementation 'com.squareup.retrofit2:converter-scalars:2.3.0'
40-
compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
41-
compile 'com.trello:rxlifecycle:1.0'
42-
compile 'com.trello:rxlifecycle-components:1.0'
43-
/*rotrofit*/
44-
compile 'com.google.code.gson:gson:2.8.0'
36+
implementation project(':fuhttp')
4537
}

fuHttp/app/src/main/java/cn/fulushan/fuhttp/activity/ReTrofitActivity.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import retrofit2.Callback;
1818
import retrofit2.Response;
1919
import retrofit2.Retrofit;
20-
import retrofit2.converter.gson.GsonConverterFactory;
2120

2221
public class ReTrofitActivity extends AppCompatActivity implements View.OnClickListener {
2322

@@ -45,7 +44,6 @@ protected void onCreate(Bundle savedInstanceState) {
4544

4645
Retrofit retrofit = new Retrofit.Builder()
4746
.baseUrl(Url.BASE_URL)
48-
.addConverterFactory(GsonConverterFactory.create())
4947
.build();
5048

5149
// service = retrofit.create(MyAppService.class);
@@ -58,17 +56,17 @@ public void onClick(View view) {
5856
ReTrofitActivity.this.finish();
5957
break;
6058
case R.id.quert_art_btn:
61-
Call<Article> call = service.listArt(0);
62-
call.enqueue(new Callback<Article>() {
63-
@Override
64-
public void onResponse(Call<Article> call, Response<Article> response) {
65-
resultTv.setText(response.body().getData().getDatas().get(10).getTitle());
66-
}
67-
@Override
68-
public void onFailure(Call<Article> call, Throwable t) {
69-
t.printStackTrace();
70-
}
71-
});
59+
// Call<Article> call = service.listArt(0);
60+
// call.enqueue(new Callback<Article>() {
61+
// @Override
62+
// public void onResponse(Call<Article> call, Response<Article> response) {
63+
// resultTv.setText(response.body().getData().getDatas().get(10).getTitle());
64+
// }
65+
// @Override
66+
// public void onFailure(Call<Article> call, Throwable t) {
67+
// t.printStackTrace();
68+
// }
69+
// });
7270

7371
break;
7472
}

fuHttp/app/src/main/java/cn/fulushan/fuhttp/activity/ReTrofitRxJavaActivity.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,12 @@
1010
import butterknife.BindView;
1111
import butterknife.ButterKnife;
1212
import cn.fulushan.fuhttp.R;
13-
import cn.fulushan.fuhttp.bean.Article;
14-
import cn.fulushan.fuhttp.config.Url;
1513
import cn.fulushan.fuhttp.net.FuRestClient;
1614
import cn.fulushan.fuhttp.net.callback.IError;
1715
import cn.fulushan.fuhttp.net.callback.IFailure;
1816
import cn.fulushan.fuhttp.net.callback.IRequest;
1917
import cn.fulushan.fuhttp.net.callback.ISuccess;
2018
import cn.fulushan.fuhttp.net.service.MyAppService;
21-
import retrofit2.Call;
22-
import retrofit2.Callback;
23-
import retrofit2.Response;
24-
import retrofit2.Retrofit;
25-
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
26-
import retrofit2.converter.gson.GsonConverterFactory;
2719

2820
public class ReTrofitRxJavaActivity extends AppCompatActivity implements View.OnClickListener {
2921

fuHttp/fuhttp/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

fuHttp/fuhttp/build.gradle

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
apply plugin: 'com.android.library'
2+
3+
android {
4+
compileSdkVersion 26
5+
6+
7+
8+
defaultConfig {
9+
minSdkVersion 14
10+
targetSdkVersion 26
11+
versionCode 1
12+
versionName "1.0"
13+
14+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
15+
16+
}
17+
18+
buildTypes {
19+
release {
20+
minifyEnabled false
21+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
22+
}
23+
}
24+
25+
}
26+
27+
dependencies {
28+
implementation fileTree(dir: 'libs', include: ['*.jar'])
29+
30+
implementation 'com.android.support:appcompat-v7:26.1.0'
31+
testImplementation 'junit:junit:4.12'
32+
androidTestImplementation 'com.android.support.test:runner:1.0.2'
33+
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
34+
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
35+
implementation 'com.google.code.gson:gson:2.6.2'
36+
implementation 'com.squareup.retrofit2:converter-gson:2.1.0'
37+
implementation 'com.squareup.retrofit2:converter-scalars:2.3.0'
38+
compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
39+
compile 'com.trello:rxlifecycle:1.0'
40+
compile 'com.trello:rxlifecycle-components:1.0'
41+
/*rotrofit*/
42+
compile 'com.google.code.gson:gson:2.8.0'
43+
}

fuHttp/fuhttp/proguard-rules.pro

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package cn.fulushan.fuhttp;
2+
3+
import android.content.Context;
4+
import android.support.test.InstrumentationRegistry;
5+
import android.support.test.runner.AndroidJUnit4;
6+
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
10+
import static org.junit.Assert.*;
11+
12+
/**
13+
* Instrumented test, which will execute on an Android device.
14+
*
15+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
16+
*/
17+
@RunWith(AndroidJUnit4.class)
18+
public class ExampleInstrumentedTest {
19+
@Test
20+
public void useAppContext() throws Exception {
21+
// Context of the app under test.
22+
Context appContext = InstrumentationRegistry.getTargetContext();
23+
24+
assertEquals("cn.fulushan.fuhttp.test", appContext.getPackageName());
25+
}
26+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2+
package="cn.fulushan.fuhttp" />

fuHttp/app/src/main/java/cn/fulushan/fuhttp/net/FuRestClient.java renamed to fuHttp/fuhttp/src/main/java/cn/fulushan/fuhttp/net/FuRestClient.java

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import cn.fulushan.fuhttp.net.callback.RequestCallBack;
1010
import cn.fulushan.fuhttp.net.enums.HttpMethod;
1111
import cn.fulushan.fuhttp.net.service.FuRestService;
12+
import okhttp3.RequestBody;
1213
import retrofit2.Call;
1314
import retrofit2.Callback;
1415

@@ -26,14 +27,16 @@ public class FuRestClient {
2627
private final ISuccess iSuccess;
2728
private final IFailure iFailure;
2829
private final IError iError;
30+
private final RequestBody requestBody;
2931

30-
public FuRestClient(String url, Map<String, Object> params, IRequest iRequest, ISuccess iSuccess, IFailure iFailure, IError iError) {
32+
public FuRestClient(String url, Map<String, Object> params, IRequest iRequest, ISuccess iSuccess, IFailure iFailure, IError iError,RequestBody requestBody) {
3133
this.url = url;
3234
this.params = params;
3335
this.iRequest = iRequest;
3436
this.iSuccess = iSuccess;
3537
this.iFailure = iFailure;
3638
this.iError = iError;
39+
this.requestBody = requestBody;
3740
}
3841

3942
public static FuRestClientBuilder builder(){
@@ -55,8 +58,19 @@ public void request(HttpMethod method){
5558
call = restService.get(url,params);
5659
break;
5760
case POST:
61+
call = restService.post(url,params);
5862
break;
5963
case DELETE:
64+
call = restService.delete(url,params);
65+
break;
66+
case PUT:
67+
call = restService.put(url, params);
68+
break;
69+
case POST_RAW:
70+
call = restService.postRaw(url, requestBody);
71+
break;
72+
case PUT_RAW:
73+
call = restService.putRaw(url, requestBody);
6074
break;
6175
case DOWNLOAD:
6276
break;
@@ -78,4 +92,40 @@ private Callback<String> getRequestCallBack() {
7892
public final void get(){
7993
request(HttpMethod.GET);
8094
}
95+
96+
public final void post() {
97+
if (requestBody == null) {
98+
request(HttpMethod.POST);
99+
} else {
100+
if (!params.isEmpty()) {
101+
throw new RuntimeException("params must be null!");
102+
}
103+
request(HttpMethod.POST_RAW);
104+
}
105+
}
106+
107+
public final void put() {
108+
if (requestBody == null) {
109+
request(HttpMethod.PUT);
110+
} else {
111+
if (!params.isEmpty()) {
112+
throw new RuntimeException("params must be null!");
113+
}
114+
request(HttpMethod.PUT_RAW);
115+
}
116+
}
117+
118+
public final void delete() {
119+
request(HttpMethod.DELETE);
120+
}
121+
122+
public final void upload() {
123+
request(HttpMethod.UPLOAD);
124+
}
125+
126+
public final void download() {
127+
// new DownloadHandler(URL, REQUEST, DOWNLOAD_DIR, EXTENSION, NAME,
128+
// SUCCESS, FAILURE, ERROR)
129+
// .handleDownload();
130+
}
81131
}

fuHttp/app/src/main/java/cn/fulushan/fuhttp/net/FuRestClientBuilder.java renamed to fuHttp/fuhttp/src/main/java/cn/fulushan/fuhttp/net/FuRestClientBuilder.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import cn.fulushan.fuhttp.net.callback.IFailure;
88
import cn.fulushan.fuhttp.net.callback.IRequest;
99
import cn.fulushan.fuhttp.net.callback.ISuccess;
10+
import okhttp3.MediaType;
11+
import okhttp3.RequestBody;
1012

1113
/**
1214
* Created by fulushan on 18/5/5.
@@ -21,6 +23,17 @@ public class FuRestClientBuilder {
2123
private ISuccess iSuccess;
2224
private IFailure iFailure;
2325
private IError iError;
26+
private RequestBody requestBody;
27+
28+
// public final RestClientBuilder file(File file) {
29+
// this.mFile = file;
30+
// return this;
31+
// }
32+
//
33+
// public final RestClientBuilder file(String file) {
34+
// this.mFile = new File(file);
35+
// return this;
36+
// }
2437

2538
FuRestClientBuilder() {
2639

@@ -42,6 +55,11 @@ public final FuRestClientBuilder params(String param,Object value){
4255
return this;
4356
}
4457

58+
public final FuRestClientBuilder raw(String raw) {
59+
this.requestBody = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), raw);
60+
return this;
61+
}
62+
4563
public final FuRestClientBuilder iRequest(IRequest iRequest){
4664
this.iRequest = iRequest;
4765
return this;
@@ -60,7 +78,7 @@ public final FuRestClientBuilder iError(IError iError){
6078
}
6179

6280
public final FuRestClient build(){
63-
return new FuRestClient(url,params,iRequest,iSuccess,iFailure,iError);
81+
return new FuRestClient(url,params,iRequest,iSuccess,iFailure,iError,requestBody);
6482
}
6583

6684
}

0 commit comments

Comments
 (0)