在http://blog.csdn.net/zhaihaohao1/article/details/50967137的基础上进行封装:
分装类容主要有两个:
1对请求,使用接口回调封装2在其中封装了状态对话框
封装的主要代码:
<span style="font-size:18px;">public class OkHttpPackage {
// 对get请求的封装,对外调用的方法
HttpGetRequest httpGetRequest;
public void httpGetManager(String url,Boolean isProgressDialog,Context context,HttpGetRequest httpGetRequest){
this.httpGetRequest= httpGetRequest;
myHttpGet(url,isProgressDialog,context);
}
public interface HttpGetRequest{
public void myOnGetFailure(Request request, IOException e);
public void myOnGetResponse(String response);
}
/**
* 异步请求
* 发送网络请求
* get请求
* 默认已经把线程分装好了
*/
private void myHttpGet(String url,Boolean isProgressDialog, final Context context) {
if(isProgressDialog) {
showDialog(context);
}
OkHttpClient httpClient = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.build();
httpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
if(httpGetRequest!=null) {
httpGetRequest.myOnGetFailure(request, e);
dismissDialog(context);
}
}
@Override
public void onResponse(Response response) throws IOException {
if (response.isSuccessful()) {
String responseGet = response.body().string();
httpGetRequest.myOnGetResponse(responseGet);
try {
Thread.sleep(2000);
dismissDialog(context);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
);
}
// -----------------对post请求进行封装---------------------
// 对外调用的方法
HttpRequest httpRequest;
public void httpPostManager(String url,FormEncodingBuilder formEncodingBuilder,Boolean isProgressDialog,Context context,HttpRequest httpRequest){
this.httpRequest=httpRequest;
myHttpPost(url,formEncodingBuilder,isProgressDialog,context);
}
public interface HttpRequest{
public void myOnFailure(Request request, IOException e);
public void myOnResponse(String response);
}
/**
* 异步请求
* 发送网络请求
* post请求
* 默认已经把线程分装好了
* 参数1路径
* 参数2参数
*/
private void myHttpPost(String url,FormEncodingBuilder formEncodingBuilder,Boolean isProgressDialog, final Context context) {
if(isProgressDialog) {
showDialog(context);
}
OkHttpClient httpClient = new OkHttpClient();
RequestBody body = formEncodingBuilder.build();
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
// 成功,用接口回调,失败用接口回调
httpClient.newCall(request).enqueue(new com.squareup.okhttp.Callback() {
@Override
public void onFailure(Request request, IOException e) {
if (httpRequest != null) {
httpRequest.myOnFailure(request, e);
dismissDialog(context);
}
}
@Override
public void onResponse(Response response) throws IOException {
if (response.isSuccessful()) {
String jsonStr = response.body().string();
if (httpRequest != null) {
httpRequest.myOnResponse(jsonStr);
dismissDialog(context);
}
}
}
});
}
CustomProgress customProgress;
private void showDialog(Context context){
if(customProgress==null) {
customProgress=new CustomProgress(context, R.style.Custom_Progress);
}
customProgress.myShow(customProgress, "玩命加载中...", true, null);
}
private void dismissDialog(Context context){
if(customProgress==null) {
customProgress=new CustomProgress(context, R.style.Custom_Progress);
}
customProgress.dismiss();
}
}
</span>
在activity中调用:
源码下载:<span style="font-size:18px;">public class MainActivity extends AppCompatActivity { private Button btn_okhttp01; private Button btn_okhttp02; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); myOnClick(); } /** * 初始化控件 */ private void initView() { btn_okhttp01 = (Button) findViewById(R.id.btn_okhttp01); btn_okhttp02 = (Button) findViewById(R.id.btn_okhttp02); } /** *post请求 */ private void myOnClick() { btn_okhttp01.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String url = "http://112.124.22.238:8081/course_api/banner/query?type=1"; FormEncodingBuilder formEncodingBuilder = new FormEncodingBuilder(); formEncodingBuilder.add("type", "1"); new OkHttpPackage().httpPostManager(url, formEncodingBuilder, true, MainActivity.this, new OkHttpPackage.HttpRequest() { @Override public void myOnFailure(Request request, IOException e) { } @Override public void myOnResponse(String response) { } }); } }); /** *get请求 */ btn_okhttp02.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String url = "http://112.124.22.238:8081/course_api/banner/query?type=1"; new OkHttpPackage().httpGetManager(url, true, MainActivity.this, new HttpGetRequest() { @Override public void myOnGetFailure(Request request, IOException e) { } @Override public void myOnGetResponse(String response) { } }); } }); } }//class</span>
http://download.csdn.net/detail/zhaihaohao1/9584743