Skip to content

Commit

Permalink
2.0 初步(待完成)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeasonlzy committed Sep 28, 2016
1 parent a062db8 commit 7786251
Show file tree
Hide file tree
Showing 41 changed files with 406 additions and 564 deletions.
9 changes: 5 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
apply plugin: 'com.android.application'

def buildSdkVersion = 23
def buildToolVersion = "23.0.3"
def supportVersion = "23.4.0"
def buildSdkVersion = 24
def buildToolVersion = "24.0.2"
def supportVersion = "24.2.1"

android {
compileSdkVersion buildSdkVersion
buildToolsVersion buildToolVersion
defaultConfig {
applicationId "com.lzy.okhttpdemo"
minSdkVersion 14
targetSdkVersion 22
targetSdkVersion 24
versionCode 22
versionName "1.8.0"
}
Expand Down Expand Up @@ -80,4 +80,5 @@ dependencies {
// compile 'com.lzy.net:okhttpserver:+'
compile project(':okhttpgo')
compile project(':okhttpserver')
compile project(':okrx')
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

import com.lzy.okhttpgo.request.BaseRequest;

import java.lang.reflect.Type;

/**
* ================================================
* 作 者:廖子尧
Expand All @@ -30,13 +28,8 @@ private void initDialog(Activity activity) {
dialog.setMessage("请求网络中...");
}

public DialogCallback(Activity activity, Class<T> clazz) {
super(clazz);
initDialog(activity);
}

public DialogCallback(Activity activity, Type type) {
super(type);
public DialogCallback(Activity activity) {
super();
initDialog(activity);
}

Expand Down
22 changes: 1 addition & 21 deletions app/src/main/java/com/lzy/okhttpdemo/callback/JsonCallback.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import com.lzy.okhttpgo.callback.AbsCallback;
import com.lzy.okhttpgo.request.BaseRequest;

import java.lang.reflect.Type;

import okhttp3.Response;

/**
Expand All @@ -26,24 +24,6 @@
*/
public abstract class JsonCallback<T> extends AbsCallback<T> {

public Class<T> clazz;
public Type type;

/**
* 传class,直接返回解析生成的对象
*/
public JsonCallback(Class<T> clazz) {
this.clazz = clazz;
}

/**
* 对于需要返回集合类型的,可以传type
* type = new TypeToken<List<你的数据类型>>(){}.getType()
*/
public JsonCallback(Type type) {
this.type = type;
}

@Override
public void onBefore(BaseRequest request) {
super.onBefore(request);
Expand All @@ -60,6 +40,6 @@ public void onBefore(BaseRequest request) {
*/
@Override
public T convertSuccess(Response response) throws Exception {
return new JsonConvert<>(this).convertSuccess(response);
return JsonConvert.<T>create().convertSuccess(response);
}
}
88 changes: 36 additions & 52 deletions app/src/main/java/com/lzy/okhttpdemo/callback/JsonConvert.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.lzy.okhttpdemo.callback;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;
import com.lzy.okhttpdemo.model.LzyResponse;
import com.lzy.okhttpdemo.model.SimpleResponse;
import com.lzy.okhttpdemo.utils.Convert;
import com.lzy.okhttpgo.convert.Converter;

import org.json.JSONObject;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import okhttp3.Response;
Expand All @@ -22,66 +22,50 @@
*/
public class JsonConvert<T> implements Converter<T> {

public Class<T> clazz;
public Type type;

public static <T> JsonConvert<T> create(Class<T> clazz) {
return new JsonConvert<>(clazz);
public static <T> JsonConvert<T> create() {
return new JsonConvert<>();
}

public static <T> JsonConvert<T> create(Type type) {
return new JsonConvert<>(type);
}
@Override
public T convertSuccess(Response response) throws Exception {
Type genType = getClass().getGenericSuperclass();
Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
Type type = params[0];
if (!(type instanceof ParameterizedType)) throw new IllegalStateException("没有填写泛型参数");

/** 传class,直接返回解析生成的对象 */
public JsonConvert(Class<T> clazz) {
this.clazz = clazz;
}
Type[] args = ((ParameterizedType) type).getActualTypeArguments();
if (args == null || args.length == 0) throw new IllegalStateException("没有填写泛型参数");

/** 对于需要返回集合类型的,可以传type type = new TypeToken<List<你的数据类型>>(){}.getType() */
public JsonConvert(Type type) {
this.type = type;
}
JsonReader jsonReader = new JsonReader(response.body().charStream());

@Override
public T convertSuccess(Response response) throws Exception {
String responseData = response.body().string();
if (TextUtils.isEmpty(responseData)) return null;
//无数据类型
if (args[0] == Void.class) {
SimpleResponse baseWbgResponse = Convert.fromJson(jsonReader, SimpleResponse.class);
return (T) baseWbgResponse.toLzyResponse();
}

/**
* 一般来说,服务器返回的响应码都包含 code,msg,data 三部分,在此根据自己的业务需要完成相应的逻辑判断
* 以下只是一个示例,具体业务具体实现
*/
JSONObject jsonObject = new JSONObject(responseData);
final String msg = jsonObject.optString("msg", "");
final int code = jsonObject.optInt("code", 0);
String data = jsonObject.optString("data", "");
switch (code) {
case 0:
/**
* 假如 code = 0 代表成功,这里默认实现了Gson解析,可以自己替换成fastjson等
* clazz类型就是解析javaBean
* type类型就是解析List<javaBean>
*/
if (clazz == String.class) return (T) data;
if (clazz != null) return new Gson().fromJson(data, clazz);
if (type != null) return new Gson().fromJson(data, type);
break;
case 104:
//有数据类型
if (args[0] == LzyResponse.class) {
LzyResponse lzyResponse = Convert.fromJson(jsonReader, type);
int code = lzyResponse.code;
if (code == 0) {
return (T) lzyResponse;
} else if (code == 104) {
//比如:用户授权信息无效,在此实现相应的逻辑,弹出对话或者跳转到其他页面等,该抛出错误,会在onError中回调。
throw new IllegalStateException("用户授权信息无效");
case 105:
} else if (code == 105) {
//比如:用户收取信息已过期,在此实现相应的逻辑,弹出对话或者跳转到其他页面等,该抛出错误,会在onError中回调。
throw new IllegalStateException("用户收取信息已过期");
case 106:
} else if (code == 106) {
//比如:用户账户被禁用,在此实现相应的逻辑,弹出对话或者跳转到其他页面等,该抛出错误,会在onError中回调。
throw new IllegalStateException("用户账户被禁用");
case 300:
} else if (code == 300) {
//比如:其他乱七八糟的等,在此实现相应的逻辑,弹出对话或者跳转到其他页面等,该抛出错误,会在onError中回调。
throw new IllegalStateException("其他乱七八糟的等");
default:
throw new IllegalStateException("错误代码:" + code + ",错误信息:" + msg);
} else {
throw new IllegalStateException("错误代码:" + code + ",错误信息:" + lzyResponse.msg);
}
}
throw new IllegalStateException("数据解析错误");
throw new IllegalStateException("基类错误无法解析!");
}
}
}
21 changes: 21 additions & 0 deletions app/src/main/java/com/lzy/okhttpdemo/model/LzyResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.lzy.okhttpdemo.model;

import java.io.Serializable;

/**
* ================================================
* 作 者:jeasonlzy(廖子尧)Github地址:https://github.com/jeasonlzy
* 版 本:1.0
* 创建日期:16/9/28
* 描 述:
* 修订历史:
* ================================================
*/
public class LzyResponse<T> implements Serializable {

private static final long serialVersionUID = 5213230387175987834L;

public int code;
public String msg;
public T data;
}
27 changes: 27 additions & 0 deletions app/src/main/java/com/lzy/okhttpdemo/model/SimpleResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.lzy.okhttpdemo.model;

import java.io.Serializable;

/**
* ================================================
* 作 者:jeasonlzy(廖子尧)Github地址:https://github.com/jeasonlzy
* 版 本:1.0
* 创建日期:16/9/28
* 描 述:
* 修订历史:
* ================================================
*/
public class SimpleResponse implements Serializable {

private static final long serialVersionUID = -1477609349345966116L;

public int code;
public String msg;

public LzyResponse toLzyResponse() {
LzyResponse lzyResponse = new LzyResponse();
lzyResponse.code = code;
lzyResponse.msg = msg;
return lzyResponse;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public void first_cache_then_request(View view) {
private class CacheCallBack extends DialogCallback<ServerModel> {

public CacheCallBack(Activity activity) {
super(activity, ServerModel.class);
super(activity);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void formUpload(View view) {
// .params("file2",new File("文件路径"))
// .params("file3",new File("文件路径"))
.addFileParams("file", files) // 这种方式为同一个key,上传多个文件
.execute(new JsonCallback<ServerModel>(ServerModel.class) {
.execute(new JsonCallback<ServerModel>() {
@Override
public void onBefore(BaseRequest request) {
super.onBefore(request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.os.Bundle;
import android.view.View;

import com.google.gson.reflect.TypeToken;
import com.lzy.okhttpdemo.R;
import com.lzy.okhttpdemo.base.BaseDetailActivity;
import com.lzy.okhttpdemo.callback.DialogCallback;
Expand Down Expand Up @@ -43,7 +42,7 @@ public void requestJson(View view) {
.tag(this)//
.headers("header1", "headerValue1")//
.params("param1", "paramValue1")//
.execute(new DialogCallback<ServerModel>(this, ServerModel.class) {
.execute(new DialogCallback<ServerModel>(this) {
@Override
public void onSuccess(ServerModel serverModel, Call call, Response response) {
handleResponse(serverModel, call, response);
Expand All @@ -66,7 +65,7 @@ public void requestJsonArray(View view) {
.tag(this)//
.headers("header1", "headerValue1")//
.params("param1", "paramValue1")//
.execute(new DialogCallback<List<ServerModel>>(this, new TypeToken<List<ServerModel>>() {}.getType()) {
.execute(new DialogCallback<List<ServerModel>>(this) {
@Override
public void onSuccess(List<ServerModel> serverModels, Call call, Response response) {
handleResponse(serverModels, call, response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)
.tag(this)//
.headers("header1", "headerValue1")//
.params("param1", "paramValue1")//
.execute(new DialogCallback<ServerModel>(this, ServerModel.class) {
.execute(new DialogCallback<ServerModel>(this) {
@Override
public void onSuccess(ServerModel serverModel, Call call, Response response) {
handleResponse(serverModel, call, response);
Expand All @@ -75,7 +75,7 @@ public void onError(Call call, Response response, Exception e) {
.tag(this)//
.headers("header1", "headerValue1")//
.params("param1", "paramValue1")//
.execute(new DialogCallback<ServerModel>(this, ServerModel.class) {
.execute(new DialogCallback<ServerModel>(this) {
@Override
public void onSuccess(ServerModel serverModel, Call call, Response response) {
handleResponse(serverModel, call, response);
Expand All @@ -93,7 +93,7 @@ public void onError(Call call, Response response, Exception e) {
.tag(this)//
.headers("header1", "headerValue1")//
.params("param1", "paramValue1")//
.execute(new DialogCallback<ServerModel>(this, ServerModel.class) {
.execute(new DialogCallback<ServerModel>(this) {
@Override
public void onSuccess(ServerModel serverModel, Call call, Response response) {
handleResponse(serverModel, call, response);
Expand All @@ -111,7 +111,7 @@ public void onError(Call call, Response response, Exception e) {
.tag(this)//
.headers("header1", "headerValue1")//
.params("param1", "paramValue1")//
.execute(new DialogCallback<ServerModel>(this, ServerModel.class) {
.execute(new DialogCallback<ServerModel>(this) {
@Override
public void onSuccess(ServerModel serverModel, Call call, Response response) {
handleResponse(serverModel, call, response);
Expand All @@ -129,7 +129,7 @@ public void onError(Call call, Response response, Exception e) {
.tag(this)//
.headers("header1", "headerValue1")//
.params("param1", "paramValue1")//
.execute(new DialogCallback<ServerModel>(this, ServerModel.class) {
.execute(new DialogCallback<ServerModel>(this) {
@Override
public void onSuccess(ServerModel serverModel, Call call, Response response) {
handleResponse(serverModel, call, response);
Expand All @@ -148,7 +148,7 @@ public void onError(Call call, Response response, Exception e) {
.headers("header1", "headerValue1")//
.params("param1", "paramValue1")//
.requestBody(RequestBody.create(MediaType.parse("text/plain;charset=utf-8"), "这是要上传的数据"))//
.execute(new DialogCallback<ServerModel>(this, ServerModel.class) {
.execute(new DialogCallback<ServerModel>(this) {
@Override
public void onSuccess(ServerModel serverModel, Call call, Response response) {
handleResponse(serverModel, call, response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void postJson(View view) {
.headers("header1", "headerValue1")//
.params("param1", "paramValue1")//
.upJson(jsonObject.toString())//
.execute(new DialogCallback<ServerModel>(this, ServerModel.class) {
.execute(new DialogCallback<ServerModel>(this) {
@Override
public void onSuccess(ServerModel serverModel, Call call, Response response) {
handleResponse(serverModel, call, response);
Expand All @@ -72,7 +72,7 @@ public void postString(View view) {
.headers("header1", "headerValue1")//
.params("param1", "paramValue1")//
.upString("这是要上传的长文本数据!")//
.execute(new DialogCallback<ServerModel>(this, ServerModel.class) {
.execute(new DialogCallback<ServerModel>(this) {
@Override
public void onSuccess(ServerModel serverModel, Call call, Response response) {
handleResponse(serverModel, call, response);
Expand All @@ -94,7 +94,7 @@ public void postBytes(View view) {
.headers("header1", "headerValue1")//
.params("param1", "paramValue1")//
.upBytes("这是字节数据".getBytes())//
.execute(new DialogCallback<ServerModel>(this, ServerModel.class) {
.execute(new DialogCallback<ServerModel>(this) {
@Override
public void onSuccess(ServerModel serverModel, Call call, Response response) {
handleResponse(serverModel, call, response);
Expand Down
Loading

0 comments on commit 7786251

Please sign in to comment.