parent
f836df523b
commit
c6b093e822
@ -0,0 +1,27 @@ |
||||
package com.community.pocket.data.model; |
||||
|
||||
import java.util.List; |
||||
|
||||
/** |
||||
* 检索预约人 |
||||
*/ |
||||
public class VisitorPeople { |
||||
private String name; |
||||
private List<String> time; |
||||
|
||||
public String getName() { |
||||
return name; |
||||
} |
||||
|
||||
public void setName(String name) { |
||||
this.name = name; |
||||
} |
||||
|
||||
public List<String> getTime() { |
||||
return time; |
||||
} |
||||
|
||||
public void setTime(List<String> time) { |
||||
this.time = time; |
||||
} |
||||
} |
@ -1,46 +1,14 @@ |
||||
package com.community.pocket.ui.main.ui.forum.data; |
||||
|
||||
import androidx.annotation.Nullable; |
||||
import com.community.pocket.data.model.ForumContent; |
||||
import com.community.pocket.ui.main.ui.share.Response; |
||||
|
||||
import java.util.List; |
||||
|
||||
/** |
||||
* 帖子详情数据管理 |
||||
* |
||||
* @param <T> 响应实体 |
||||
*/ |
||||
public class ForumDataResponse<T> { |
||||
@Nullable |
||||
private Integer success; |
||||
@Nullable |
||||
private Integer error; |
||||
|
||||
private T body; |
||||
|
||||
@Nullable |
||||
public Integer getSuccess() { |
||||
return success; |
||||
} |
||||
|
||||
public ForumDataResponse<T> setSuccess(@Nullable Integer success) { |
||||
this.success = success; |
||||
return this; |
||||
} |
||||
|
||||
@Nullable |
||||
public Integer getError() { |
||||
return error; |
||||
} |
||||
|
||||
public ForumDataResponse<T> setError(@Nullable Integer error) { |
||||
this.error = error; |
||||
return this; |
||||
} |
||||
|
||||
T getBody() { |
||||
return body; |
||||
} |
||||
public class ForumDataResponse extends Response<List<ForumContent>> { |
||||
|
||||
public ForumDataResponse<T> setBody(T body) { |
||||
this.body = body; |
||||
return this; |
||||
} |
||||
} |
||||
|
@ -1,41 +1,12 @@ |
||||
package com.community.pocket.ui.main.ui.forum.post; |
||||
|
||||
import androidx.annotation.Nullable; |
||||
import com.community.pocket.ui.main.ui.share.Response; |
||||
|
||||
public class ForumPostResponse<T> { |
||||
@Nullable |
||||
private Integer success; |
||||
@Nullable |
||||
private Integer error; |
||||
import java.util.List; |
||||
|
||||
private T body; |
||||
/** |
||||
* 检索投诉人响应结果 |
||||
*/ |
||||
public class ForumPostResponse extends Response<List<String>> { |
||||
|
||||
@Nullable |
||||
public Integer getSuccess() { |
||||
return success; |
||||
} |
||||
|
||||
public ForumPostResponse<T> setSuccess(@Nullable Integer success) { |
||||
this.success = success; |
||||
return this; |
||||
} |
||||
|
||||
@Nullable |
||||
public Integer getError() { |
||||
return error; |
||||
} |
||||
|
||||
public ForumPostResponse<T> setError(@Nullable Integer error) { |
||||
this.error = error; |
||||
return this; |
||||
} |
||||
|
||||
T getBody() { |
||||
return body; |
||||
} |
||||
|
||||
public ForumPostResponse<T> setBody(T body) { |
||||
this.body = body; |
||||
return this; |
||||
} |
||||
} |
||||
|
@ -1,47 +1,12 @@ |
||||
package com.community.pocket.ui.main.ui.info; |
||||
|
||||
import androidx.annotation.Nullable; |
||||
import com.community.pocket.data.model.MyInfo; |
||||
import com.community.pocket.ui.main.ui.share.Response; |
||||
|
||||
|
||||
/** |
||||
* 个人信息响应结果 |
||||
* |
||||
* @param <T> 响应实体信息 |
||||
*/ |
||||
public class InfoResponse<T> { |
||||
@Nullable |
||||
private Integer success; |
||||
@Nullable |
||||
private Integer error; |
||||
|
||||
private T body; |
||||
|
||||
@Nullable |
||||
public Integer getSuccess() { |
||||
return success; |
||||
} |
||||
|
||||
public InfoResponse<T> setSuccess(@Nullable Integer success) { |
||||
this.success = success; |
||||
return this; |
||||
} |
||||
|
||||
@Nullable |
||||
public Integer getError() { |
||||
return error; |
||||
} |
||||
|
||||
public InfoResponse<T> setError(@Nullable Integer error) { |
||||
this.error = error; |
||||
return this; |
||||
} |
||||
|
||||
T getBody() { |
||||
return body; |
||||
} |
||||
|
||||
public InfoResponse<T> setBody(T body) { |
||||
this.body = body; |
||||
return this; |
||||
} |
||||
public class InfoResponse extends Response<MyInfo> { |
||||
} |
||||
|
@ -1,46 +1,12 @@ |
||||
package com.community.pocket.ui.main.ui.visitor.appointment; |
||||
|
||||
import androidx.annotation.NonNull; |
||||
import androidx.annotation.Nullable; |
||||
import com.community.pocket.data.model.VisitorPeople; |
||||
import com.community.pocket.ui.main.ui.share.Response; |
||||
|
||||
import java.util.List; |
||||
|
||||
/** |
||||
* 访客接口请求结果 |
||||
*/ |
||||
public class VisitorResponse<T> { |
||||
@Nullable |
||||
private Integer success; |
||||
@Nullable |
||||
private Integer error; |
||||
|
||||
private T body; |
||||
|
||||
@Nullable |
||||
public Integer getSuccess() { |
||||
return success; |
||||
} |
||||
|
||||
public VisitorResponse<T> setSuccess(@Nullable Integer success) { |
||||
this.success = success; |
||||
return this; |
||||
} |
||||
|
||||
@Nullable |
||||
public Integer getError() { |
||||
return error; |
||||
} |
||||
|
||||
public VisitorResponse<T> setError(@Nullable Integer error) { |
||||
this.error = error; |
||||
return this; |
||||
} |
||||
|
||||
@NonNull |
||||
T getBody() { |
||||
return body; |
||||
} |
||||
|
||||
public VisitorResponse<T> setBody(@NonNull T body) { |
||||
this.body = body; |
||||
return this; |
||||
} |
||||
public class VisitorResponse extends Response<List<VisitorPeople>> { |
||||
} |
||||
|
@ -0,0 +1,15 @@ |
||||
package com.community.pocket.util; |
||||
|
||||
import org.jetbrains.annotations.NotNull; |
||||
|
||||
import okhttp3.Call; |
||||
import okhttp3.Response; |
||||
|
||||
/** |
||||
* 解析接口返回数据 |
||||
* |
||||
* @param <T> 转化实体类型 |
||||
*/ |
||||
public interface HttpParse<T extends com.community.pocket.ui.main.ui.share.Response> { |
||||
void onParseOk(@NotNull Call call, @NotNull Response response, T t); |
||||
} |
@ -0,0 +1,14 @@ |
||||
package com.community.pocket.util; |
||||
|
||||
import java.lang.annotation.Retention; |
||||
import java.lang.annotation.Target; |
||||
|
||||
import static java.lang.annotation.ElementType.METHOD; |
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME; |
||||
|
||||
@Retention(RUNTIME) |
||||
@Target({METHOD}) |
||||
public @interface HttpRequest { |
||||
//接口地址
|
||||
String value(); |
||||
} |
@ -0,0 +1,76 @@ |
||||
package com.community.pocket.util; |
||||
|
||||
import android.util.Log; |
||||
|
||||
import com.google.gson.Gson; |
||||
import com.google.gson.JsonSyntaxException; |
||||
|
||||
import org.jetbrains.annotations.NotNull; |
||||
|
||||
import java.io.IOException; |
||||
|
||||
import okhttp3.Call; |
||||
import okhttp3.Callback; |
||||
import okhttp3.Response; |
||||
import okhttp3.ResponseBody; |
||||
|
||||
/** |
||||
* 解析HTTP响应体 |
||||
* |
||||
* @param <T> 响应实体类型 |
||||
*/ |
||||
public class HttpResponse<T extends com.community.pocket.ui.main.ui.share.Response> implements Callback { |
||||
|
||||
private Class<T> tClass; |
||||
|
||||
private HttpParse<T> httpParse; |
||||
|
||||
private static final String header = "Content-Type"; |
||||
|
||||
HttpResponse(Class<T> tClass, HttpParse<T> httpParse) { |
||||
this.tClass = tClass; |
||||
this.httpParse = httpParse; |
||||
} |
||||
|
||||
@Override |
||||
public void onFailure(@NotNull Call call, @NotNull IOException e) { |
||||
e.printStackTrace(); |
||||
Log.e(HttpResponse.class.getName(), e.toString()); |
||||
} |
||||
|
||||
@Override |
||||
public void onResponse(@NotNull Call call, @NotNull Response response) { |
||||
String contentType = response.header(header); |
||||
if (contentType != null && contentType.contains("application/json")) { |
||||
ResponseBody body = response.body(); |
||||
if (body != null) { |
||||
Gson gson = new Gson(); |
||||
try { |
||||
String message = body.string(); |
||||
Log.i(HttpUtil.class.getName(), "响应数据:\n" + message); |
||||
T jsonObject = gson.fromJson(message, tClass); |
||||
if (jsonObject != null) { |
||||
httpParse.onParseOk(call, response, jsonObject); |
||||
} else { |
||||
onParseError(call, response, "无法把数据" + message + "解析为" + tClass + "类型"); |
||||
} |
||||
} catch (JsonSyntaxException e) { |
||||
onParseError(call, response, "解析异常" + e); |
||||
} catch (IOException e) { |
||||
onParseError(call, response, "解析异常" + e); |
||||
} |
||||
} else { |
||||
onParseError(call, response, "响应体为null"); |
||||
} |
||||
} else { |
||||
onParseError(call, response, "接口不是响应json数据,非法响应头" + header + "=" + contentType); |
||||
} |
||||
} |
||||
|
||||
|
||||
private void onParseError(@NotNull Call call, @NotNull Response response, String err) { |
||||
Log.e(HttpResponse.class.getName(), err); |
||||
throw new RuntimeException(err); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,108 @@ |
||||
package com.community.pocket.util; |
||||
|
||||
import androidx.annotation.NonNull; |
||||
|
||||
import com.community.pocket.BuildConfig; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
import okhttp3.Call; |
||||
import okhttp3.Callback; |
||||
import okhttp3.FormBody; |
||||
import okhttp3.OkHttpClient; |
||||
import okhttp3.Request; |
||||
|
||||
public class HttpUtil { |
||||
|
||||
private static final Map<String, HttpRequest> map = new HashMap<>(); |
||||
|
||||
public static void getRequest(Method method, Callback callback) { |
||||
getRequest(method, callback, null); |
||||
} |
||||
|
||||
public static void getRequest(Method method, Callback callback, FormBody formBody) { |
||||
//1.创建OkHttpClient对象
|
||||
OkHttpClient okHttpClient = new OkHttpClient(); |
||||
//2.创建Request对象,设置一个url地址(百度地址),设置请求方式。
|
||||
Request.Builder builder = new Request.Builder(); |
||||
if (method == Method.GET && formBody != null) { |
||||
StringBuilder sb = new StringBuilder(); |
||||
for (int i = 0; i < formBody.size(); i++) { |
||||
sb.append("&").append(formBody.name(i)).append("=").append(formBody.value(i)); |
||||
} |
||||
builder = builder.url(BuildConfig.API_HOST + getUrl() + "?" + sb.substring(1)).method(method.name(), null); |
||||
} else { |
||||
builder = builder.url(BuildConfig.API_HOST + getUrl()).method(method.name(), formBody); |
||||
} |
||||
//3.创建一个call对象,参数就是Request请求对象
|
||||
Call call = okHttpClient.newCall(builder.build()); |
||||
//4.请求加入调度,重写回调方法
|
||||
call.enqueue(callback); |
||||
} |
||||
|
||||
public enum Method { |
||||
GET, |
||||
POST |
||||
} |
||||
|
||||
|
||||
/** |
||||
* 获取调用 |
||||
*/ |
||||
@NonNull |
||||
private static String getUrl() { |
||||
HttpRequest httpRequest = null; |
||||
|
||||
StackTraceElement[] stack = Thread.currentThread().getStackTrace(); |
||||
String className = null; |
||||
String methodName = null; |
||||
for (int index = 1; index < stack.length; index++) { |
||||
if (HttpUtil.class.getName().equals(stack[index - 1].getClassName()) && !HttpUtil.class.getName().equals(stack[index].getClassName())) { |
||||
className = stack[index].getClassName(); |
||||
methodName = stack[index].getMethodName(); |
||||
break; |
||||
} |
||||
} |
||||
RuntimeException runtimeException = new RuntimeException("未知异常!!!"); |
||||
if (className != null) { |
||||
|
||||
try { |
||||
|
||||
if (!map.containsKey(className + methodName)) { |
||||
|
||||
Class c = Class.forName(className); |
||||
java.lang.reflect.Method[] methods = c.getMethods(); |
||||
List<java.lang.reflect.Method> matchMethod = new ArrayList<>(); |
||||
|
||||
for (java.lang.reflect.Method m : methods) { |
||||
if (methodName.equals(m.getName()) && m.getAnnotation(HttpRequest.class) != null) { |
||||
matchMethod.add(m); |
||||
} |
||||
} |
||||
if (matchMethod.size() == 1) { |
||||
httpRequest = matchMethod.get(0).getAnnotation(HttpRequest.class); |
||||
map.put(matchMethod.get(0).getName(), httpRequest); |
||||
} else if (matchMethod.size() > 1) { |
||||
runtimeException = new RuntimeException(c.getName() + "包含多个方法名为:" + methodName + "的方法!!!"); |
||||
} else { |
||||
runtimeException = new RuntimeException("类:" + c.getName() + "方法:" + methodName + "缺少注解" + HttpRequest.class + ",无法调用" + HttpUtil.class); |
||||
} |
||||
} else { |
||||
httpRequest = map.get(className + methodName); |
||||
} |
||||
if (httpRequest != null) { |
||||
return httpRequest.value(); |
||||
} |
||||
} catch (ClassNotFoundException e) { |
||||
e.printStackTrace(); |
||||
} catch (RuntimeException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
} |
||||
throw runtimeException; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,5 @@ |
||||
package com.community.pocket.util; |
||||
|
||||
public interface MyCallBack<T> { |
||||
void call(T forumNoticeResponse); |
||||
} |
@ -1,20 +0,0 @@ |
||||
package com.community.pocket.util; |
||||
|
||||
public class Result<T> { |
||||
private Valid valid; |
||||
|
||||
private T body; |
||||
|
||||
public Result(Valid valid, T body) { |
||||
this.valid = valid; |
||||
this.body = body; |
||||
} |
||||
|
||||
public Valid getValid() { |
||||
return valid; |
||||
} |
||||
|
||||
public T getBody() { |
||||
return body; |
||||
} |
||||
} |
@ -0,0 +1,39 @@ |
||||
package com.community.pocket.util; |
||||
|
||||
import androidx.annotation.StringRes; |
||||
import androidx.lifecycle.MutableLiveData; |
||||
|
||||
import org.jetbrains.annotations.NotNull; |
||||
|
||||
import okhttp3.Call; |
||||
import okhttp3.Response; |
||||
|
||||
public class SimpleHttpParse<T extends com.community.pocket.ui.main.ui.share.Response> implements HttpParse<T> { |
||||
|
||||
private MutableLiveData liveData; |
||||
|
||||
private Integer success; |
||||
|
||||
private Integer error; |
||||
|
||||
public SimpleHttpParse(MutableLiveData liveData, @StringRes Integer success, @StringRes Integer error) { |
||||
this.liveData = liveData; |
||||
this.success = success; |
||||
this.error = error; |
||||
} |
||||
|
||||
public HttpResponse<T> getInterface(Class<T> tClass) { |
||||
return new HttpResponse<>(tClass, this); |
||||
} |
||||
|
||||
@Override |
||||
@SuppressWarnings("unchecked") |
||||
public void onParseOk(@NotNull Call call, @NotNull Response response, T t) { |
||||
if (t.getResult() == com.community.pocket.ui.main.ui.share.Response.Result.OK) { |
||||
t.setSuccess(success); |
||||
} else { |
||||
t.setError(error); |
||||
} |
||||
liveData.postValue(t); |
||||
} |
||||
} |
Loading…
Reference in new issue