Skip to content

Commit c6b2382

Browse files
author
Vens Chen
committed
update
1 parent ebc378b commit c6b2382

File tree

7 files changed

+165
-88
lines changed

7 files changed

+165
-88
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
<category android:name="android.intent.category.LAUNCHER"/>
2727
</intent-filter>
2828
</activity>
29+
2930
</application>
3031

3132
</manifest>

app/src/main/java/com/threetree/thttp/BaseActivity.java

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.threetree.thttp;
22

3+
import android.os.Bundle;
4+
import android.support.annotation.Nullable;
35
import android.support.v7.app.AppCompatActivity;
46

7+
import com.threetree.ttdialog.LoadingDialog;
58
import com.threetree.tthttp.viewbind.ILoadingView;
69

710
/**
@@ -10,22 +13,47 @@
1013

1114
public class BaseActivity extends AppCompatActivity implements ILoadingView {
1215

16+
protected boolean mActive;
17+
LoadingDialog mDialog;
18+
1319
@Override
14-
public void showLoading(boolean b)
20+
protected void onCreate(@Nullable Bundle savedInstanceState)
1521
{
22+
super.onCreate(savedInstanceState);
23+
mActive = true;
24+
}
1625

26+
@Override
27+
protected void onDestroy()
28+
{
29+
super.onDestroy();
30+
mActive = false;
1731
}
1832

1933
@Override
20-
public void dismissLoading()
34+
public void showLoading(boolean isCancel)
2135
{
36+
if(mDialog==null)
37+
{
38+
mDialog = new LoadingDialog.Builder(this)
39+
.setCancelable(isCancel)
40+
.setCancelOutside(isCancel)
41+
.create();
42+
}
43+
mDialog.show();
44+
}
2245

46+
@Override
47+
public void dismissLoading()
48+
{
49+
if(mDialog!=null)
50+
mDialog.dismiss();
2351
}
2452

2553
@Override
2654
public boolean isActive()
2755
{
28-
return false;
56+
return mActive;
2957
}
3058

3159

app/src/main/java/com/threetree/thttp/CommonPresenter.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ public CommonPresenter(Context context,ILoadingView iLoadingView)
1919
{
2020
super(iLoadingView);
2121
mContext = context;
22-
mApiService = RetrofitManager.getInstence().getRetrofitService(ApiService.class);
22+
// mApiService = RetrofitManager.getInstence().getRetrofitService(ApiService.class);
23+
mApiService = (ApiService)RetrofitManager.getInstence().getRetrofitService();
2324
}
2425

26+
2527
@Override
2628
protected void showError(ApiException ex)
2729
{

app/src/main/java/com/threetree/thttp/DocPresenter.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public DocPresenter(Context context, IDocView iDocView)
2222
*/
2323
public void getDoc()
2424
{
25+
if(mDocView.isActive())
26+
mDocView.showLoading(false);
2527
Observable observable = mApiService.getDoc(1,4);//调用接口
2628
subscribeHttp(observable, new IHttpResultListener<String>() {
2729
@Override

app/src/main/java/com/threetree/thttp/MainActivity.java

Lines changed: 18 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,19 @@
22

33
import android.graphics.BitmapFactory;
44
import android.os.Bundle;
5-
import android.support.v7.app.AppCompatActivity;
65
import android.view.View;
76
import android.widget.ImageView;
87
import android.widget.Toast;
98

10-
import com.threetree.ttdialog.LoadingDialog;
119
import com.threetree.tthttp.RetrofitManager;
1210
import com.threetree.tthttp.presenter.DownLoadPresenter;
1311
import com.threetree.tthttp.viewbind.IProgressView;
1412

15-
public class MainActivity extends AppCompatActivity implements IDocView{
13+
public class MainActivity extends BaseActivity implements IDocView {
1614

1715
ImageView mIv;
18-
DocPresenter mDocPresenter;
1916
DownLoadPresenter mDownLoadPresenter;
20-
boolean mActive;
21-
22-
LoadingDialog mDialog;
17+
DocPresenter mDocPresenter;
2318

2419
@Override
2520
protected void onCreate(Bundle savedInstanceState)
@@ -28,20 +23,22 @@ protected void onCreate(Bundle savedInstanceState)
2823
setContentView(R.layout.activity_main);
2924

3025
RetrofitManager.getInstence()
31-
.baseUrl("your baseurl")
26+
.baseUrl("your baseUrl")
3227
.addInterceptor(new HttpInterceptor())
28+
.serviceClass(ApiService.class)
3329
.create();
3430

35-
mActive = true;
36-
mIv = (ImageView)findViewById(R.id.iv);
3731
mDocPresenter = new DocPresenter(this,this);
32+
33+
mIv = (ImageView)findViewById(R.id.iv);
3834
findViewById(R.id.load_tv).setOnClickListener(new View.OnClickListener() {
3935
@Override
4036
public void onClick(View v)
4137
{
4238
download();
4339
}
4440
});
41+
4542
findViewById(R.id.doc_tv).setOnClickListener(new View.OnClickListener() {
4643
@Override
4744
public void onClick(View v)
@@ -56,11 +53,16 @@ public void onClick(View v)
5653
protected void onDestroy()
5754
{
5855
super.onDestroy();
59-
mActive = false;
60-
if(mDocPresenter!=null)
61-
mDocPresenter.destroy();
6256
if(mDownLoadPresenter!=null)
6357
mDownLoadPresenter.destroy();
58+
if(mDocPresenter != null)
59+
mDocPresenter.destroy();
60+
}
61+
62+
@Override
63+
public void onSuccess(String doc)
64+
{
65+
Toast.makeText(this,doc,Toast.LENGTH_SHORT).show();
6466
}
6567

6668
private void download()
@@ -90,43 +92,12 @@ public void progress(long progress, long total, float speed)
9092
public void success()
9193
{
9294
mIv.setImageBitmap(BitmapFactory.decodeFile(FileUtil.getFilePath()));
95+
mDownLoadPresenter.destroy();
9396
}
94-
}, "your baseurl");
97+
}, "http://upload.cbg.cn/2016/0726/1469533389366.jpg");
9598
}
9699
mDownLoadPresenter.setFileDir(FileUtil.ROOT_PATH);
97100
mDownLoadPresenter.setDestFileName(FileUtil.NAME);
98-
mDownLoadPresenter.download("http://upload.cbg.cn/2016/0726/1469533389366.jpg");
99-
}
100-
101-
@Override
102-
public void showLoading(boolean isCancel)
103-
{
104-
if(mDialog==null)
105-
{
106-
mDialog = new LoadingDialog.Builder(this)
107-
.setCancelable(isCancel)
108-
.setCancelOutside(isCancel)
109-
.create();
110-
}
111-
mDialog.show();
112-
}
113-
114-
@Override
115-
public void dismissLoading()
116-
{
117-
if(mDialog!=null)
118-
mDialog.dismiss();
119-
}
120-
121-
@Override
122-
public boolean isActive()
123-
{
124-
return mActive;
125-
}
126-
127-
@Override
128-
public void onSuccess(String doc)
129-
{
130-
Toast.makeText(this,doc,Toast.LENGTH_SHORT).show();
101+
mDownLoadPresenter.download();
131102
}
132103
}

tthttp/src/main/java/com/threetree/tthttp/RetrofitManager.java

Lines changed: 79 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package com.threetree.tthttp;
22

3+
import android.text.TextUtils;
4+
35
import java.util.concurrent.TimeUnit;
46

57
import okhttp3.Interceptor;
68
import okhttp3.OkHttpClient;
9+
import retrofit2.Converter;
710
import retrofit2.Retrofit;
811
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
912
import retrofit2.converter.gson.GsonConverterFactory;
@@ -12,15 +15,30 @@
1215
* Created by Administrator on 2018/7/12.
1316
*/
1417

15-
public class RetrofitManager {
18+
public class RetrofitManager<T> {
1619
private static RetrofitManager mRetrofitManager;
1720
private String mBaseUrl;
1821
private Retrofit mRetrofit;
1922
private OkHttpClient.Builder mClientBuilder;
23+
private Retrofit.Builder mRetrofitBuilder;
24+
private T mService;
25+
private Class<T> mServiceClass;
26+
private long mConnectTimeout;
27+
private long mReadTimeout;
28+
private long mWriteTimeout;
29+
private boolean isReConnect;
2030

2131
private RetrofitManager()
2232
{
2333
mClientBuilder = new OkHttpClient.Builder();
34+
mRetrofitBuilder = new Retrofit.Builder();
35+
mRetrofitBuilder.addCallAdapterFactory(RxJava2CallAdapterFactory.create());
36+
mRetrofitBuilder.addConverterFactory(GsonConverterFactory.create());
37+
38+
mConnectTimeout = 15;
39+
mReadTimeout = 20;
40+
mWriteTimeout =20;
41+
isReConnect = true;
2442
}
2543

2644
public static RetrofitManager getInstence()
@@ -43,30 +61,76 @@ public RetrofitManager addInterceptor(Interceptor interceptor)
4361

4462
public RetrofitManager baseUrl(String baseUrl)
4563
{
64+
if(TextUtils.isEmpty(baseUrl))
65+
throw new NullPointerException("baseUrl must not be null");
66+
mRetrofitBuilder.baseUrl(baseUrl);
4667
mBaseUrl = baseUrl;
4768
return this;
4869
}
4970

71+
public RetrofitManager serviceClass(Class<T> serviceClass)
72+
{
73+
if(serviceClass == null)
74+
throw new NullPointerException("serviceClass must not be null");
75+
mServiceClass = serviceClass;
76+
return this;
77+
}
78+
79+
public RetrofitManager addConverterFactory(Converter.Factory factory)
80+
{
81+
mRetrofitBuilder.addConverterFactory(factory);
82+
return this;
83+
}
84+
85+
public RetrofitManager connectTimeout(long connectTimeout)
86+
{
87+
mConnectTimeout = connectTimeout;
88+
return this;
89+
}
90+
91+
public RetrofitManager readTimeout(long readTimeout)
92+
{
93+
mReadTimeout = readTimeout;
94+
return this;
95+
}
96+
97+
public RetrofitManager writeTimeout(long writeTimeout)
98+
{
99+
mWriteTimeout = writeTimeout;
100+
return this;
101+
}
102+
103+
public RetrofitManager retryOnConnectionFailure(boolean reConnect)
104+
{
105+
isReConnect = reConnect;
106+
return this;
107+
}
108+
50109
public void create()
51110
{
52-
Retrofit.Builder builder = new Retrofit.Builder();
53-
builder.baseUrl(mBaseUrl);
54-
builder.addCallAdapterFactory(RxJava2CallAdapterFactory.create());
55-
builder.addConverterFactory(GsonConverterFactory.create());
56-
57-
mClientBuilder.connectTimeout(15, TimeUnit.SECONDS);//超时
58-
mClientBuilder.readTimeout(20, TimeUnit.SECONDS);
59-
mClientBuilder.writeTimeout(20, TimeUnit.SECONDS);
60-
mClientBuilder.retryOnConnectionFailure(true);//重连
61-
62-
builder.client(mClientBuilder.build());
63-
mRetrofit = builder.build();
111+
if(TextUtils.isEmpty(mBaseUrl))
112+
throw new NullPointerException("baseUrl must not be null");
113+
if(mServiceClass == null)
114+
throw new NullPointerException("service must not be null");
115+
116+
mClientBuilder.connectTimeout(mConnectTimeout, TimeUnit.SECONDS);//超时
117+
mClientBuilder.readTimeout(mReadTimeout, TimeUnit.SECONDS);
118+
mClientBuilder.writeTimeout(mWriteTimeout, TimeUnit.SECONDS);
119+
mClientBuilder.retryOnConnectionFailure(isReConnect);//重连
120+
121+
mRetrofitBuilder.client(mClientBuilder.build());
122+
mRetrofit = mRetrofitBuilder.build();
123+
mService = mRetrofit.create(mServiceClass);
64124
}
65125

66-
public <T> T getRetrofitService(Class<T> service)
126+
public T getRetrofitService()
67127
{
68128
if (mRetrofit == null)
69129
throw new RuntimeException("retrofit must be init");
70-
return mRetrofit.create(service);
130+
if(mServiceClass == null)
131+
throw new NullPointerException("service must not be null");
132+
if(mService == null)
133+
mService = mRetrofit.create(mServiceClass);
134+
return mService;
71135
}
72136
}

0 commit comments

Comments
 (0)