Skip to content

Commit

Permalink
代码整体结构修改
Browse files Browse the repository at this point in the history
  • Loading branch information
jeasonlzy committed Aug 28, 2016
1 parent d8b4975 commit c9668c4
Show file tree
Hide file tree
Showing 30 changed files with 320 additions and 184 deletions.
17 changes: 4 additions & 13 deletions app/src/main/java/com/lzy/okhttpdemo/activity/BaseActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
}
}

protected <T> void handleResponse(boolean isFromCache, T data, Request request, @Nullable Response response) {
requestState.setText("请求成功 是否来自缓存:" + isFromCache + " 请求方式:" + request.method() + "\n" +
"url:" + request.url());

protected <T> void handleResponse(T data, Request request, @Nullable Response response) {
Headers requestHeadersString = request.headers();
Set<String> requestNames = requestHeadersString.names();
StringBuilder sb = new StringBuilder();
Expand Down Expand Up @@ -164,18 +161,12 @@ protected <T> void handleResponse(boolean isFromCache, T data, Request request,
for (String name : names) {
sb.append(name).append(" : ").append(responseHeadersString.get(name)).append("\n");
}
} else if (isFromCache) {
sb = new StringBuilder("响应头可以根据 cacheKey 获取到,在此不演示!");
}
responseHeader.setText(sb.toString());
}

protected void handleError(boolean isFromCache, Call call, @Nullable Response response) {
Request request = call.request();
requestState.setText("请求失败 是否来自缓存:" + isFromCache + " 请求方式:" + request.method() + "\n" +
"url:" + request.url());

Headers requestHeadersString = request.headers();
protected void handleError(Call call, @Nullable Response response) {
Headers requestHeadersString = call.request().headers();
Set<String> requestNames = requestHeadersString.names();
StringBuilder sb = new StringBuilder();
for (String name : requestNames) {
Expand All @@ -197,4 +188,4 @@ protected void handleError(boolean isFromCache, Call call, @Nullable Response re
responseHeader.setText("--");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import com.lzy.okhttpdemo.utils.Constant;
import com.lzy.okhttpdemo.utils.Urls;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.BitmapCallback;

import butterknife.Bind;
import butterknife.ButterKnife;
Expand Down Expand Up @@ -46,15 +45,15 @@ public void requestJson(View view) {
.params("param1", "paramValue1")//
.execute(new BitmapDialogCallback(this) {
@Override
public void onResponse(boolean isFromCache, Bitmap bitmap, Request request, Response response) {
handleResponse(isFromCache, bitmap, request, response);
public void onSuccess(Bitmap bitmap, Request request, Response response) {
handleResponse(bitmap, request, response);
imageView.setImageBitmap(bitmap);
}

@Override
public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
super.onError(isFromCache, call, response, e);
handleError(isFromCache, call, response);
public void onError(Call call, @Nullable Response response, @Nullable Exception e) {
super.onError(call, response, e);
handleError(call, response);
}
});
}
Expand Down
27 changes: 21 additions & 6 deletions app/src/main/java/com/lzy/okhttpdemo/activity/CacheActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,29 @@ public CacheCallBack(Activity activity) {
}

@Override
public void onResponse(boolean isFromCache, RequestInfo requestInfo, Request request, Response response) {
handleResponse(isFromCache, requestInfo, request, response);
public void onSuccess(RequestInfo requestInfo, Request request, Response response) {
requestState.setText("请求成功 是否来自缓存:false 请求方式:" + request.method() + "\n" + "url:" + request.url());
handleResponse(requestInfo, request, response);
}

@Override
public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
super.onError(isFromCache, call, response, e);
handleError(isFromCache, call, response);
public void onCacheSuccess(RequestInfo requestInfo, Request request, @Nullable Response response) {
requestState.setText("请求成功 是否来自缓存:true 请求方式:" + request.method() + "\n" + "url:" + request.url());
handleResponse(requestInfo, request, response);
responseHeader.setText("响应头可以根据 cacheKey 获取到,在此不演示!");
}

@Override
public void onError(Call call, @Nullable Response response, @Nullable Exception e) {
super.onError(call, response, e);
requestState.setText("请求失败 是否来自缓存:false 请求方式:" + call.request().method() + "\n" + "url:" + call.request().url());
handleError(call, response);
}

@Override
public void onCacheError(Call call, @Nullable Response response, @Nullable Exception e) {
requestState.setText("请求失败 是否来自缓存:true 请求方式:" + call.request().method() + "\n" + "url:" + call.request().url());
handleError(call, response);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ public CustomCallBack(Activity activity, Type type) {
}

@Override
public void onResponse(boolean isFromCache, T data, Request request, Response response) {
handleResponse(isFromCache, data, request, response);
public void onSuccess(T data, Request request, Response response) {
handleResponse(data, request, response);
}

@Override
public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
super.onError(isFromCache, call, response, e);
handleError(isFromCache, call, response);
public void onError(Call call, @Nullable Response response, @Nullable Exception e) {
super.onError(call, response, e);
handleError(call, response);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import com.lzy.okhttputils.request.BaseRequest;

import java.io.File;
import java.nio.ByteBuffer;

import butterknife.Bind;
import butterknife.ButterKnife;
Expand Down Expand Up @@ -69,8 +68,8 @@ public void onBefore(BaseRequest request) {
}

@Override
public void onResponse(boolean isFromCache, File file, Request request, Response response) {
handleResponse(isFromCache, file, request, response);
public void onSuccess(File file, Request request, Response response) {
handleResponse(file, request, response);
btnFileDownload.setText("下载完成");
}

Expand All @@ -89,9 +88,9 @@ public void downloadProgress(long currentSize, long totalSize, float progress, l
}

@Override
public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
super.onError(isFromCache, call, response, e);
handleError(isFromCache, call, response);
public void onError(Call call, @Nullable Response response, @Nullable Exception e) {
super.onError(call, response, e);
handleError(call, response);
btnFileDownload.setText("下载出错");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,15 @@ public void onBefore(BaseRequest request) {
}

@Override
public void onResponse(boolean isFromCache, T s, Request request, Response response) {
handleResponse(isFromCache, s, request, response);
public void onSuccess(T s, Request request, Response response) {
handleResponse(s, request, response);
btnFormUpload.setText("上传完成");
}

@Override
public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
super.onError(isFromCache, call, response, e);
handleError(isFromCache, call, response);
public void onError(Call call, @Nullable Response response, @Nullable Exception e) {
super.onError(call, response, e);
handleError(call, response);
btnFormUpload.setText("上传出错");
}

Expand Down
10 changes: 5 additions & 5 deletions app/src/main/java/com/lzy/okhttpdemo/activity/HttpsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,14 @@ public HttpsCallBack(Activity activity) {
}

@Override
public void onResponse(boolean isFromCache, String data, Request request, Response response) {
handleResponse(isFromCache, data, request, response);
public void onSuccess(String data, Request request, Response response) {
handleResponse(data, request, response);
}

@Override
public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
super.onError(isFromCache, call, response, e);
handleError(isFromCache, call, response);
public void onError(Call call, @Nullable Response response, @Nullable Exception e) {
super.onError(call, response, e);
handleError(call, response);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,14 @@ public MethodCallBack(Activity activity, Class<T> clazz) {
}

@Override
public void onResponse(boolean isFromCache, T data, Request request, Response response) {
handleResponse(isFromCache, data, request, response);
public void onSuccess(T data, Request request, Response response) {
handleResponse(data, request, response);
}

@Override
public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
super.onError(isFromCache, call, response, e);
handleError(isFromCache, call, response);
public void onError(Call call, @Nullable Response response, @Nullable Exception e) {
super.onError(call, response, e);
handleError(call, response);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void postJson(View view) {
.tag(this)//
.headers("header1", "headerValue1")//
.params("param1", "paramValue1")//
.postJson(jsonObject.toString())//
.upJson(jsonObject.toString())//
.execute(new TextCallBack<>(this, RequestInfo.class));
}

Expand All @@ -62,7 +62,7 @@ public void postString(View view) {
.tag(this)//
.headers("header1", "headerValue1")//
.params("param1", "paramValue1")//
.postString("这是要上传的长文本数据!")//
.upString("这是要上传的长文本数据!")//
.execute(new TextCallBack<>(this, RequestInfo.class));
}

Expand All @@ -72,7 +72,7 @@ public void postBytes(View view) {
.tag(this)//
.headers("header1", "headerValue1")//
.params("param1", "paramValue1")//
.postBytes("这是字节数据".getBytes())//
.upBytes("这是字节数据".getBytes())//
.execute(new TextCallBack<>(this, RequestInfo.class));
}

Expand All @@ -83,14 +83,14 @@ public TextCallBack(Activity activity, Class<T> clazz) {
}

@Override
public void onResponse(boolean isFromCache, T data, Request request, Response response) {
handleResponse(isFromCache, data, request, response);
public void onSuccess(T data, Request request, Response response) {
handleResponse(data, request, response);
}

@Override
public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
super.onError(isFromCache, call, response, e);
handleError(isFromCache, call, response);
public void onError(Call call, @Nullable Response response, @Nullable Exception e) {
super.onError(call, response, e);
handleError(call, response);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,18 @@ public RedirectCallBack(Activity activity) {
}

@Override
public void onResponse(boolean isFromCache, String data, Request request, Response response) {
handleResponse(isFromCache, data, request, response);
public void onSuccess(String data, Request request, Response response) {
handleResponse(data, request, response);

responseData.setText("注意看请求头的url和响应头的url是不一样的!\n这代表了在请求过程中发生了重定向," +
"okhttp默认将重定向封装在了请求内部,只有最后一次请求的数据会被真正的请求下来触发回调,中间过程" +
"是默认实现的,不会触发回调!");
}

@Override
public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
super.onError(isFromCache, call, response, e);
handleError(isFromCache, call, response);
public void onError(Call call, @Nullable Response response, @Nullable Exception e) {
super.onError(call, response, e);
handleError(call, response);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void onBefore(BaseRequest request) {
}

@Override
public void onAfter(boolean isFromCache, @Nullable Bitmap bitmap, Call call, @Nullable Response response, @Nullable Exception e) {
public void onAfter(@Nullable Bitmap bitmap, Call call, @Nullable Response response, @Nullable Exception e) {
if (dialog != null && dialog.isShowing()) {
dialog.dismiss();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public void onBefore(BaseRequest request) {
}

@Override
public void onAfter(boolean isFromCache, @Nullable T t, Call call, @Nullable Response response, @Nullable Exception e) {
super.onAfter(isFromCache, t, call, response, e);
public void onAfter(@Nullable T t, Call call, @Nullable Response response, @Nullable Exception e) {
super.onAfter(t, call, response, e);
//网络请求结束后关闭对话框
if (dialog != null && dialog.isShowing()) {
dialog.dismiss();
Expand Down
11 changes: 1 addition & 10 deletions app/src/main/java/com/lzy/okhttpdemo/callback/JsonCallback.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.lzy.okhttpdemo.callback;

import android.text.TextUtils;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lzy.okhttputils.OkHttpUtils;

import org.json.JSONObject;

Expand Down Expand Up @@ -73,13 +71,6 @@ public T parseNetworkResponse(Response response) throws Exception {
default:
throw new IllegalStateException("错误代码:" + code + ",错误信息:" + msg);
}
//如果要更新UI,需要使用handler,可以如下方式实现,也可以自己写handler
OkHttpUtils.getInstance().getDelivery().post(new Runnable() {
@Override
public void run() {
Toast.makeText(OkHttpUtils.getContext(), "错误代码:" + code + ",错误信息:" + msg, Toast.LENGTH_SHORT).show();
}
});
return null;
throw new IllegalStateException("数据解析错误");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public void onBefore(BaseRequest request) {
}

@Override
public void onAfter(boolean isFromCache, @Nullable String s, Call call, Response response, @Nullable Exception e) {
super.onAfter(isFromCache, s, call, response, e);
public void onAfter(@Nullable String s, Call call, Response response, @Nullable Exception e) {
super.onAfter(s, call, response, e);
//网络请求结束后关闭对话框
if (dialog != null && dialog.isShowing()) {
dialog.dismiss();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public ViewHolder(View convertView) {
// }
//
// @Override
// public void onResponse(Call call, Response response) throws IOException {
// public void onSuccess(Call call, Response response) throws IOException {
// System.out.println("-----" + response.body().string());
// }
// });
Expand All @@ -171,7 +171,7 @@ public ViewHolder(View convertView) {
// }
//
// @Override
// public void onResponse(Call call, Response response) throws IOException {
// public void onSuccess(Call call, Response response) throws IOException {
// System.out.println("-----" + response.body().string());
// }
// });
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'
classpath 'com.android.tools.build:gradle:2.1.3'

classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.0'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Wed Apr 13 18:38:37 CST 2016
#Sun Aug 28 17:42:10 CST 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public T parseNetworkResponse(Response response) throws Exception {
}

@Override
public void onResponse(boolean isFromCache, T t, Request request, Response response) {
public void onSuccess(T t, Request request, Response response) {
}
}

Expand Down
Loading

0 comments on commit c9668c4

Please sign in to comment.