forked from MrZhousf/OkHttp3
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
309 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package base; | ||
|
||
import android.os.Handler; | ||
import android.os.Looper; | ||
import android.os.Message; | ||
|
||
/** | ||
* 通用句柄 | ||
* @author zhousf | ||
*/ | ||
public class BaseHandler extends Handler { | ||
|
||
private CallBack callBack; | ||
|
||
public interface CallBack{ | ||
void handleMessage(Message msg); | ||
} | ||
|
||
public BaseHandler(CallBack callBack){ | ||
super(Looper.getMainLooper()); | ||
this.callBack = callBack; | ||
} | ||
|
||
@Override | ||
public void handleMessage(Message msg) { | ||
if(null != callBack){ | ||
callBack.handleMessage(msg); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package base; | ||
|
||
import android.os.Bundle; | ||
import android.os.Message; | ||
import android.support.annotation.Nullable; | ||
import android.support.v7.app.AppCompatActivity; | ||
|
||
import com.okhttplib.HttpInfo; | ||
import com.okhttplib.OkHttpUtil; | ||
import com.okhttplib.callback.Callback; | ||
|
||
import java.io.IOException; | ||
|
||
import base.view.LoadingDialog; | ||
|
||
/** | ||
* Activity基类:支持网络请求、加载提示框 | ||
* @author zhousf | ||
*/ | ||
public class HttpActivity extends AppCompatActivity implements BaseHandler.CallBack { | ||
|
||
private BaseHandler baseHandler;//Handler句柄 | ||
|
||
private LoadingDialog loadingDialog;//加载提示框 | ||
|
||
private final int SHOW_DIALOG = 0x001; | ||
private final int DISMISS_DIALOG = 0x002; | ||
private final int LOAD_SUCCEED = 0x003; | ||
private final int LOAD_FAILED = 0x004; | ||
|
||
|
||
@Override | ||
protected void onCreate(@Nullable Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
} | ||
|
||
/** | ||
* 同步请求 | ||
* @param info 请求信息体 | ||
* @return HttpInfo | ||
*/ | ||
protected HttpInfo doHttpSync(HttpInfo info) { | ||
//显示加载框 | ||
getBaseHandler().sendEmptyMessage(SHOW_DIALOG); | ||
info = OkHttpUtil.getDefault(this).doSync(info); | ||
if(info.isSuccessful()){ | ||
//显示加载成功 | ||
getBaseHandler().sendEmptyMessage(LOAD_SUCCEED); | ||
}else { | ||
//显示加载失败 | ||
getBaseHandler().sendEmptyMessage(LOAD_FAILED); | ||
} | ||
//隐藏加载框 | ||
getBaseHandler().sendEmptyMessageDelayed(DISMISS_DIALOG,1000); | ||
return info; | ||
} | ||
|
||
/** | ||
* 异步请求 | ||
* @param info 请求信息体 | ||
* @param callback 结果回调接口 | ||
*/ | ||
protected void doHttpAsync(HttpInfo info, final Callback callback){ | ||
getBaseHandler().sendEmptyMessage(SHOW_DIALOG); | ||
OkHttpUtil.getDefault(this).doAsync(info, new Callback() { | ||
@Override | ||
public void onSuccess(HttpInfo info) throws IOException { | ||
getBaseHandler().sendEmptyMessage(DISMISS_DIALOG); | ||
callback.onSuccess(info); | ||
} | ||
|
||
@Override | ||
public void onFailure(HttpInfo info) throws IOException { | ||
getBaseHandler().sendEmptyMessage(DISMISS_DIALOG); | ||
callback.onFailure(info); | ||
} | ||
}); | ||
} | ||
|
||
protected LoadingDialog getLoadingDialog(){ | ||
if(null == loadingDialog){ | ||
loadingDialog = new LoadingDialog(this); | ||
//点击空白处Dialog不消失 | ||
loadingDialog.setCanceledOnTouchOutside(false); | ||
} | ||
return loadingDialog; | ||
} | ||
|
||
/** | ||
* 获取通用句柄,自动释放 | ||
*/ | ||
protected BaseHandler getBaseHandler(){ | ||
if(null == baseHandler){ | ||
baseHandler = new BaseHandler(this); | ||
} | ||
return baseHandler; | ||
} | ||
|
||
|
||
@Override | ||
public void handleMessage(Message msg) { | ||
switch (msg.what) { | ||
case SHOW_DIALOG: | ||
getLoadingDialog().showDialog(); | ||
break; | ||
case LOAD_SUCCEED: | ||
getLoadingDialog().succeed(); | ||
break; | ||
case LOAD_FAILED: | ||
getLoadingDialog().failed(); | ||
break; | ||
case DISMISS_DIALOG: | ||
getLoadingDialog().dismissDialog(); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
|
||
@Override | ||
protected void onDestroy() { | ||
if(null != baseHandler) | ||
baseHandler.removeCallbacksAndMessages(null); | ||
super.onDestroy(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package base.view; | ||
|
||
import android.app.Dialog; | ||
import android.content.Context; | ||
import android.os.Bundle; | ||
import android.text.TextUtils; | ||
import android.view.View; | ||
import android.widget.ImageView; | ||
import android.widget.ProgressBar; | ||
import android.widget.TextView; | ||
|
||
import com.okhttp3.R; | ||
|
||
|
||
/** | ||
* Author : zhousf | ||
* Description : 加载框 | ||
* Date : 2017/9/20. | ||
*/ | ||
public class LoadingDialog extends Dialog { | ||
|
||
private ImageView iv_load_result;// 加载的结果图标显示 | ||
private TextView tv_load;// 加载的文字展示 | ||
private ProgressBar pb_loading;// 加载中的图片 | ||
|
||
public LoadingDialog(Context context) { | ||
super(context, R.style.loading_dialog_style); | ||
} | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.commom_loading_layout); | ||
iv_load_result = (ImageView) findViewById(R.id.iv_load_result); | ||
tv_load = (TextView) findViewById(R.id.tv_load); | ||
pb_loading = (ProgressBar) findViewById(R.id.pb_loading); | ||
iv_load_result.setVisibility(View.GONE); | ||
|
||
} | ||
|
||
public LoadingDialog text(String text){ | ||
if(tv_load != null && !TextUtils.isEmpty(text)){ | ||
tv_load.setText(text); | ||
} | ||
return this; | ||
} | ||
|
||
public void showDialog(){ | ||
text("加载中"); | ||
if(null != pb_loading && pb_loading.getVisibility() != View.VISIBLE){ | ||
pb_loading.setVisibility(View.VISIBLE); | ||
} | ||
if(null != iv_load_result){ | ||
iv_load_result.setVisibility(View.GONE); | ||
} | ||
show(); | ||
} | ||
|
||
public void dismissDialog(){ | ||
dismiss(); | ||
} | ||
|
||
// 加载成功 | ||
public void succeed() { | ||
if(pb_loading != null) | ||
pb_loading.setVisibility(View.GONE); | ||
if(iv_load_result != null){ | ||
iv_load_result.setVisibility(View.VISIBLE); | ||
iv_load_result.setImageResource(R.mipmap.load_suc_icon); | ||
} | ||
tv_load.setText("加载成功"); | ||
} | ||
|
||
// 加载失败 | ||
public void failed() { | ||
if(pb_loading != null) | ||
pb_loading.setVisibility(View.GONE); | ||
if(iv_load_result != null){ | ||
iv_load_result.setVisibility(View.VISIBLE); | ||
iv_load_result.setImageResource(R.mipmap.load_fail_icon); | ||
} | ||
tv_load.setText("加载失败"); | ||
} | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<shape xmlns:android="http://schemas.android.com/apk/res/android" > | ||
<corners android:radius="10dp"/> | ||
<solid android:color="#9D303132"/> | ||
</shape> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<rotate xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:drawable="@mipmap/loading_icon" | ||
android:fromDegrees="0" | ||
android:pivotX="50%" | ||
android:pivotY="50%" | ||
android:toDegrees="360" | ||
/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:layout_width="125dp" | ||
android:layout_height="125dp" | ||
android:background="@drawable/loading_black_bg" | ||
android:gravity="center" | ||
android:orientation="vertical" > | ||
|
||
<ImageView | ||
android:id="@+id/iv_load_result" | ||
android:layout_width="50dp" | ||
android:layout_height="50dp" | ||
android:visibility="gone"/> | ||
|
||
<ProgressBar | ||
android:id="@+id/pb_loading" | ||
style="@style/circleProgressBarStyle" | ||
android:layout_width="50dp" | ||
android:layout_height="50dp" | ||
android:visibility="visible" /> | ||
|
||
<TextView | ||
android:id="@+id/tv_load" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_marginTop="10dp" | ||
android:text="加载中" | ||
android:textColor="@android:color/white" /> | ||
|
||
</LinearLayout> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters