增加网络请求接口

完善获取数据的接口逻辑
增加RAP2测试
0515
panqihua 5 years ago
parent f836df523b
commit c6b093e822
  1. 13
      app/build.gradle
  2. 3
      app/src/main/AndroidManifest.xml
  3. 37
      app/src/main/java/com/community/pocket/data/adapter/VisitorAdpter.java
  4. 27
      app/src/main/java/com/community/pocket/data/main/forum/ForumDataRequest.java
  5. 38
      app/src/main/java/com/community/pocket/data/main/forum/ForumHotRequest.java
  6. 32
      app/src/main/java/com/community/pocket/data/main/forum/ForumMyRequest.java
  7. 30
      app/src/main/java/com/community/pocket/data/main/forum/ForumNewRequest.java
  8. 20
      app/src/main/java/com/community/pocket/data/main/forum/ForumPostRequest.java
  9. 29
      app/src/main/java/com/community/pocket/data/main/forum/ForumRequest.java
  10. 27
      app/src/main/java/com/community/pocket/data/main/garbage/GarbageSortingRequest.java
  11. 57
      app/src/main/java/com/community/pocket/data/main/garbage/GarbageWasteRequest.java
  12. 32
      app/src/main/java/com/community/pocket/data/main/info/InfoRequest.java
  13. 24
      app/src/main/java/com/community/pocket/data/main/visitor/VisitorMyRequest.java
  14. 33
      app/src/main/java/com/community/pocket/data/main/visitor/VisitorRequest.java
  15. 27
      app/src/main/java/com/community/pocket/data/main/visitor/VisitorReservationRequest.java
  16. 8
      app/src/main/java/com/community/pocket/data/model/Visitor.java
  17. 27
      app/src/main/java/com/community/pocket/data/model/VisitorPeople.java
  18. 23
      app/src/main/java/com/community/pocket/data/resetpwd/ResetPwdRequest.java
  19. 4
      app/src/main/java/com/community/pocket/ui/main/ui/forum/data/ForumDataActivity.java
  20. 42
      app/src/main/java/com/community/pocket/ui/main/ui/forum/data/ForumDataResponse.java
  21. 25
      app/src/main/java/com/community/pocket/ui/main/ui/forum/data/ForumDataViewModel.java
  22. 3
      app/src/main/java/com/community/pocket/ui/main/ui/forum/hot/ForumHotViewModel.java
  23. 3
      app/src/main/java/com/community/pocket/ui/main/ui/forum/main/ForumViewModel.java
  24. 3
      app/src/main/java/com/community/pocket/ui/main/ui/forum/my/ForumMyViewModel.java
  25. 3
      app/src/main/java/com/community/pocket/ui/main/ui/forum/news/ForumNewViewModel.java
  26. 14
      app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostActiveViewModel.java
  27. 5
      app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostComplainFragment.java
  28. 23
      app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostComplainViewModel.java
  29. 41
      app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostResponse.java
  30. 14
      app/src/main/java/com/community/pocket/ui/main/ui/forum/post/ForumPostTopicViewModel.java
  31. 1
      app/src/main/java/com/community/pocket/ui/main/ui/garbage/sorting/GarbageSortingFragment.java
  32. 3
      app/src/main/java/com/community/pocket/ui/main/ui/garbage/sorting/GarbageSortingViewModel.java
  33. 1
      app/src/main/java/com/community/pocket/ui/main/ui/garbage/waste/GarbageWasteFragment.java
  34. 6
      app/src/main/java/com/community/pocket/ui/main/ui/garbage/waste/GarbageWasteViewModel.java
  35. 6
      app/src/main/java/com/community/pocket/ui/main/ui/info/InfoFragment.java
  36. 41
      app/src/main/java/com/community/pocket/ui/main/ui/info/InfoResponse.java
  37. 13
      app/src/main/java/com/community/pocket/ui/main/ui/info/InfoViewModel.java
  38. 36
      app/src/main/java/com/community/pocket/ui/main/ui/share/Response.java
  39. 9
      app/src/main/java/com/community/pocket/ui/main/ui/visitor/appointment/VisitorAppointmentFragment.java
  40. 44
      app/src/main/java/com/community/pocket/ui/main/ui/visitor/appointment/VisitorResponse.java
  41. 24
      app/src/main/java/com/community/pocket/ui/main/ui/visitor/appointment/VisitorViewModel.java
  42. 3
      app/src/main/java/com/community/pocket/ui/main/ui/visitor/my/VisitorMyViewModel.java
  43. 3
      app/src/main/java/com/community/pocket/ui/main/ui/visitor/reservation/VisitorReservationViewModel.java
  44. 2
      app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdResponse.java
  45. 35
      app/src/main/java/com/community/pocket/ui/resetpwd/ResetPwdViewModel.java
  46. 15
      app/src/main/java/com/community/pocket/util/HttpParse.java
  47. 14
      app/src/main/java/com/community/pocket/util/HttpRequest.java
  48. 76
      app/src/main/java/com/community/pocket/util/HttpResponse.java
  49. 108
      app/src/main/java/com/community/pocket/util/HttpUtil.java
  50. 5
      app/src/main/java/com/community/pocket/util/MyCallBack.java
  51. 20
      app/src/main/java/com/community/pocket/util/Result.java
  52. 39
      app/src/main/java/com/community/pocket/util/SimpleHttpParse.java
  53. 1
      app/src/main/java/com/community/pocket/util/Valid.java
  54. 9
      app/src/test/java/com/community/pocket/ExampleUnitTest.java

@ -1,3 +1,7 @@
ext {
API_HOST = 'http://localhost:3000/'
RAP2_API_HOST = 'http://192.168.3.132:38080/app/mock/1'
}
apply plugin: 'com.android.application'
apply plugin: 'multi-languages'
android {
@ -15,9 +19,15 @@ android {
}
buildTypes {
debug{
buildConfigField('String','API_HOST',"\""+RAP2_API_HOST+"\"")
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
buildConfigField('String','API_HOST',"\""+API_HOST+"\"")
}
}
sourceSets {
@ -58,4 +68,7 @@ dependencies {
implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'
implementation 'androidx.gridlayout:gridlayout:1.0.0'
implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.6'
implementation 'org.conscrypt:conscrypt-android:2.2.1'
}

@ -1,7 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.community.pocket">
<!-- 添加网络访问权限-->
<uses-permission android:name="android.permission.INTERNET" />
<application
android:name=".util.InitApp"
android:allowBackup="true"

@ -3,7 +3,10 @@ package com.community.pocket.data.adapter;
import android.content.Context;
import android.widget.ArrayAdapter;
import com.community.pocket.data.model.VisitorPeople;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -12,19 +15,26 @@ import java.util.Map;
*/
public class VisitorAdpter extends ArrayAdapter<String> {
private Map<String, List<String>> map;
private List<String> list;
private List<VisitorPeople> dataList;
private List<String> list = new ArrayList<>();
private Map<String, VisitorPeople> map = new HashMap<>();
public VisitorAdpter(Map<String, List<String>> map, Context context) {
public VisitorAdpter(List<VisitorPeople> dataList, Context context) {
super(context, android.R.layout.simple_list_item_1);
addAll(map);
addAll(dataList);
}
public void addAll(Map<String, List<String>> map) {
this.map = map;
this.list = new ArrayList<>(this.map.keySet());
public void addAll(List<VisitorPeople> dataList) {
clear();
this.list.clear();
this.map.clear();
this.dataList = dataList;
for (VisitorPeople people : this.dataList) {
this.list.add(people.getName());
this.map.put(people.getName(), people);
}
addAll(this.list);
}
@ -33,10 +43,19 @@ public class VisitorAdpter extends ArrayAdapter<String> {
}
public List<String> getTime(String name) {
return map.get(name);
VisitorPeople people = map.get(name);
if (people != null) {
return people.getTime();
} else {
return new ArrayList<>();
}
}
public List<String> getList() {
return list;
}
public List<VisitorPeople> getDataList() {
return dataList;
}
}

@ -1,13 +1,15 @@
package com.community.pocket.data.main.forum;
import androidx.lifecycle.MutableLiveData;
import com.community.pocket.R;
import com.community.pocket.data.model.ForumContent;
import com.community.pocket.ui.main.ui.forum.data.ForumDataResponse;
import com.community.pocket.util.HttpRequest;
import com.community.pocket.util.HttpUtil;
import com.community.pocket.util.SimpleHttpParse;
import com.community.pocket.util.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import okhttp3.FormBody;
/**
* 帖子详情数据接口
@ -32,17 +34,10 @@ public class ForumDataRequest {
}
//获取帖子详情数据
public ForumDataResponse<List<ForumContent>> loadData(int forumId) {
return new ForumDataResponse<List<ForumContent>>().setSuccess(R.string.forum_data_ok).setBody(new ArrayList<ForumContent>() {{
for (int i = 1; i < 10; i++) {
ForumContent forumContent = new ForumContent();
forumContent.setUsername("fffname" + i);
forumContent.setTime(System.currentTimeMillis());
forumContent.setScore(new Random().nextInt(100));
forumContent.setTower(i);
forumContent.setContent("我是笨蛋我是笨蛋我是笨蛋我是笨蛋我是笨蛋我是笨蛋我是笨蛋我是笨蛋我是笨蛋我是笨蛋我是笨蛋");
add(forumContent);
}
}});
@HttpRequest("/forum/content")
public void loadData(MutableLiveData<ForumDataResponse> liveData, int forumId) {
HttpUtil.getRequest(HttpUtil.Method.GET,
new SimpleHttpParse<ForumDataResponse>(liveData, R.string.forum_data_ok, R.string.forum_data_fail).getInterface(ForumDataResponse.class),
new FormBody.Builder().add("forumId", String.valueOf(forumId)).build());
}
}

@ -1,17 +1,15 @@
package com.community.pocket.data.main.forum;
import androidx.lifecycle.MutableLiveData;
import com.community.pocket.R;
import com.community.pocket.data.model.ForumHot;
import com.community.pocket.data.model.Hot;
import com.community.pocket.data.model.UserHot;
import com.community.pocket.ui.main.ui.forum.hot.ForumHotResponse;
import java.util.ArrayList;
import java.util.List;
import com.community.pocket.util.HttpRequest;
import com.community.pocket.util.HttpUtil;
import com.community.pocket.util.SimpleHttpParse;
/**
* 热门动态接口请求
* TODO 完善逻辑
*/
public class ForumHotRequest {
private static volatile ForumHotRequest instance;
@ -27,27 +25,9 @@ public class ForumHotRequest {
}
//加载热门信息
public ForumHotResponse loadHot() {
List<UserHot> userHots = new ArrayList<>();
List<ForumHot> topicHots = new ArrayList<>();
List<ForumHot> activeHots = new ArrayList<>();
for (int i = 0; i < 3; i++) {
UserHot userHot = new UserHot();
userHot.setUserId(i);
userHot.setUserName("user" + i);
userHots.add(userHot);
ForumHot forumHot = new ForumHot();
forumHot.setForumId(i);
forumHot.setTitle("topIc" + i);
topicHots.add(forumHot);
ForumHot activeHot = new ForumHot();
activeHot.setForumId(i);
activeHot.setTitle("hot" + i);
activeHots.add(activeHot);
}
ForumHotResponse response = new ForumHotResponse();
response.setSuccess(R.string.load_hot_ok);
response.setBody(new Hot(userHots, topicHots, activeHots));
return response;
@HttpRequest("/forum/hot")
public void loadHot(MutableLiveData<ForumHotResponse> liveData) {
HttpUtil.getRequest(HttpUtil.Method.GET,
new SimpleHttpParse<ForumHotResponse>(liveData, R.string.load_hot_ok, R.string.load_hot_fail).getInterface(ForumHotResponse.class));
}
}

@ -1,17 +1,16 @@
package com.community.pocket.data.main.forum;
import androidx.lifecycle.MutableLiveData;
import com.community.pocket.R;
import com.community.pocket.data.model.Forum;
import com.community.pocket.ui.main.ui.forum.my.ForumMyResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import com.community.pocket.util.HttpRequest;
import com.community.pocket.util.HttpUtil;
import com.community.pocket.util.SimpleHttpParse;
/**
* 我的帖子请求接口
* TODO 完善逻辑
*/
public class ForumMyRequest {
private static volatile ForumMyRequest instance;
@ -27,22 +26,11 @@ public class ForumMyRequest {
}
/**
* @return 加载我的帖子
* 加载我的帖子
*/
public ForumMyResponse loadForumMy() {
List<Forum> forumList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
Forum forum = new Forum();
forum.setTitle("标题" + i);
forum.setContent("内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容");
forum.setUsername("发帖人" + i);
forum.setTime(System.currentTimeMillis());
forum.setReply(new Random().nextInt(100));
forumList.add(forum);
}
ForumMyResponse response = new ForumMyResponse();
response.setSuccess(R.string.load_forum_my_ok);
response.setBody(forumList);
return response;
@HttpRequest("/forum/my")
public void loadForumMy(MutableLiveData<ForumMyResponse> liveData) {
HttpUtil.getRequest(HttpUtil.Method.GET,
new SimpleHttpParse<ForumMyResponse>(liveData, R.string.load_forum_my_ok, R.string.load_forum_my_fail).getInterface(ForumMyResponse.class));
}
}

@ -1,16 +1,15 @@
package com.community.pocket.data.main.forum;
import androidx.lifecycle.MutableLiveData;
import com.community.pocket.R;
import com.community.pocket.data.model.Forum;
import com.community.pocket.ui.main.ui.forum.news.ForumNewResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import com.community.pocket.util.HttpRequest;
import com.community.pocket.util.HttpUtil;
import com.community.pocket.util.SimpleHttpParse;
/**
* 最新帖子列表数据接口
* TODO 完善逻辑
*/
public class ForumNewRequest {
private static volatile ForumNewRequest instance;
@ -25,20 +24,9 @@ public class ForumNewRequest {
return instance;
}
public ForumNewResponse loadForumNew() {
List<Forum> forumList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
Forum forum = new Forum();
forum.setTitle("标题" + i);
forum.setContent("内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容");
forum.setUsername("发帖人" + i);
forum.setTime(System.currentTimeMillis());
forum.setReply(new Random().nextInt(100));
forumList.add(forum);
}
ForumNewResponse response = new ForumNewResponse();
response.setSuccess(R.string.load_forum_new_ok);
response.setBody(forumList);
return response;
@HttpRequest("/forum/new")
public void loadForumNew(MutableLiveData<ForumNewResponse> liveData) {
HttpUtil.getRequest(HttpUtil.Method.GET,
new SimpleHttpParse<ForumNewResponse>(liveData, R.string.load_forum_new_ok, R.string.load_forum_new_fail).getInterface(ForumNewResponse.class));
}
}

@ -1,13 +1,16 @@
package com.community.pocket.data.main.forum;
import androidx.lifecycle.MutableLiveData;
import com.community.pocket.R;
import com.community.pocket.ui.main.ui.forum.post.ForumPostResponse;
import com.community.pocket.util.HttpRequest;
import com.community.pocket.util.HttpUtil;
import com.community.pocket.util.SimpleHttpParse;
import com.community.pocket.util.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import okhttp3.FormBody;
/**
* 发表帖子请求接口
@ -42,11 +45,10 @@ public class ForumPostRequest {
}
//检索投诉人
public ForumPostResponse<List<String>> searchPeople(String name) {
return new ForumPostResponse<List<String>>().setSuccess(R.string.search_complain_name).setBody(new ArrayList<String>() {{
add("a" + new Random().nextInt(100000));
add("a" + new Random().nextInt(100000));
add("a" + new Random().nextInt(100000));
}});
@HttpRequest("/query/user")
public void searchPeople(MutableLiveData<ForumPostResponse> liveData, String name) {
HttpUtil.getRequest(HttpUtil.Method.GET,
new SimpleHttpParse<ForumPostResponse>(liveData, R.string.search_complain_name, R.string.search_complain_name).getInterface(ForumPostResponse.class),
new FormBody.Builder().add("name", name).build());
}
}

@ -1,15 +1,15 @@
package com.community.pocket.data.main.forum;
import androidx.lifecycle.MutableLiveData;
import com.community.pocket.R;
import com.community.pocket.data.model.Notice;
import com.community.pocket.ui.main.ui.forum.main.ForumNoticeResponse;
import java.util.ArrayList;
import java.util.List;
import com.community.pocket.util.HttpRequest;
import com.community.pocket.util.HttpUtil;
import com.community.pocket.util.SimpleHttpParse;
/**
* 论坛接口管理
* TODO 完善逻辑
*/
public class ForumRequest {
private static volatile ForumRequest instance;
@ -25,21 +25,10 @@ public class ForumRequest {
}
//加载公告数据
public ForumNoticeResponse loadNotices() {
List<Notice> notices = new ArrayList<Notice>() {{
for (int i = 0; i < 10; i++) {
Notice notice = new Notice();
notice.setTime(System.currentTimeMillis());
notice.setAuthor("作者" + i);
notice.setTitle("标题" + i);
notice.setContent("内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容" + i);
add(notice);
}
@HttpRequest("/forum/notice")
public void loadNotices(MutableLiveData<ForumNoticeResponse> liveData) {
}};
ForumNoticeResponse forumNoticeResponse = new ForumNoticeResponse();
forumNoticeResponse.setSuccess(R.string.load_notice_ok);
forumNoticeResponse.setBody(notices);
return forumNoticeResponse;
HttpUtil.getRequest(HttpUtil.Method.GET,
new SimpleHttpParse<ForumNoticeResponse>(liveData, R.string.load_notice_ok, R.string.load_notice_fail).getInterface(ForumNoticeResponse.class));
}
}

@ -1,16 +1,18 @@
package com.community.pocket.data.main.garbage;
import androidx.lifecycle.MutableLiveData;
import com.community.pocket.R;
import com.community.pocket.data.model.GarbageSorting;
import com.community.pocket.ui.main.ui.garbage.sorting.GarbageSortingResponse;
import com.community.pocket.util.HttpRequest;
import com.community.pocket.util.HttpUtil;
import com.community.pocket.util.SimpleHttpParse;
import java.util.ArrayList;
import java.util.List;
import okhttp3.FormBody;
/**
* 垃圾分类请求接口
* TODO 完善逻辑
*/
public class GarbageSortingRequest {
private static volatile GarbageSortingRequest instance;
@ -26,17 +28,10 @@ public class GarbageSortingRequest {
}
//检索垃圾
public GarbageSortingResponse search() {
List<GarbageSorting> garbageSortings = new ArrayList<>();
for (int i = 0; i < 10; i++) {
GarbageSorting garbageSorting = new GarbageSorting();
garbageSorting.setName("test" + i);
garbageSorting.setSorting("有害垃圾");
garbageSortings.add(garbageSorting);
}
GarbageSortingResponse garbageSortingResponse = new GarbageSortingResponse();
garbageSortingResponse.setSuccess(R.string.garbage_search_ok);
garbageSortingResponse.setBody(garbageSortings);
return garbageSortingResponse;
@HttpRequest("/garbage/sorting")
public void search(MutableLiveData<GarbageSortingResponse> liveData, String name) {
HttpUtil.getRequest(HttpUtil.Method.GET,
new SimpleHttpParse<GarbageSortingResponse>(liveData, R.string.garbage_search_ok, R.string.garbage_search_ok).getInterface(GarbageSortingResponse.class),
new FormBody.Builder().add("name", name).build());
}
}

@ -1,18 +1,17 @@
package com.community.pocket.data.main.garbage;
import androidx.lifecycle.MutableLiveData;
import com.community.pocket.R;
import com.community.pocket.data.model.GarbageWaste;
import com.community.pocket.data.model.GarbageWasteManage;
import com.community.pocket.data.model.GarbageWastePrice;
import com.community.pocket.ui.main.ui.garbage.waste.GarbageWasteResponse;
import com.community.pocket.util.HttpRequest;
import com.community.pocket.util.HttpUtil;
import com.community.pocket.util.SimpleHttpParse;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import okhttp3.FormBody;
/**
* 废品回收接口
* TODO 完善逻辑
*/
public class GarbageWasteRequest {
private static volatile GarbageWasteRequest instance;
@ -28,45 +27,19 @@ public class GarbageWasteRequest {
}
//检索默认废品信息
public GarbageWasteResponse searchDefault() {
GarbageWasteManage manage = new GarbageWasteManage();
manage.setMobie("10086");
manage.setName("Test");
List<GarbageWastePrice> priceList = new ArrayList<>();
for (int i = 0; i < 30; i++) {
GarbageWastePrice price = new GarbageWastePrice();
price.setName("waste" + i);
price.setPrice(new Random().nextInt(10) * 1F);
priceList.add(price);
}
GarbageWaste waste = new GarbageWaste();
waste.setDefaultList(priceList);
waste.setGarbageWasteManage(manage);
GarbageWasteResponse response = new GarbageWasteResponse();
response.setSuccess(R.string.garbage_waste_search_ok);
response.setBody(waste);
return response;
@HttpRequest("/garbage/waste")
public void searchDefault(MutableLiveData<GarbageWasteResponse> liveData) {
HttpUtil.getRequest(HttpUtil.Method.GET,
new SimpleHttpParse<GarbageWasteResponse>(liveData, R.string.garbage_waste_search_ok, R.string.garbage_waste_search_ok).getInterface(GarbageWasteResponse.class));
}
//检索废品
public GarbageWasteResponse search(String name) {
List<GarbageWastePrice> priceList = new ArrayList<>();
for (int i = 0; i < 5; i++) {
GarbageWastePrice price = new GarbageWastePrice();
price.setName("waste" + i);
price.setPrice(new Random().nextInt(10) * 1F);
priceList.add(price);
}
@HttpRequest("/garbage/waste/query")
public void search(MutableLiveData<GarbageWasteResponse> liveData, String name) {
HttpUtil.getRequest(HttpUtil.Method.GET,
new SimpleHttpParse<GarbageWasteResponse>(liveData, R.string.garbage_waste_search_ok, R.string.garbage_waste_search_ok).getInterface(GarbageWasteResponse.class),
new FormBody.Builder().add("name", name).build());
GarbageWaste waste = new GarbageWaste();
waste.setSearchList(priceList);
GarbageWasteResponse response = new GarbageWasteResponse();
response.setSuccess(R.string.garbage_waste_search_ok);
response.setBody(waste);
return response;
}
}

@ -1,12 +1,13 @@
package com.community.pocket.data.main.info;
import androidx.lifecycle.MutableLiveData;
import com.community.pocket.R;
import com.community.pocket.data.model.MyInfo;
import com.community.pocket.ui.main.ui.info.InfoResponse;
import java.util.ArrayList;
import java.util.Random;
import com.community.pocket.util.HttpRequest;
import com.community.pocket.util.HttpUtil;
import com.community.pocket.util.SimpleHttpParse;
/**
* 个人信息请求接口
@ -26,25 +27,14 @@ public class InfoRequest {
}
//修改密码
public InfoResponse<String> modifyPwd(String oldpwd, String newpwd) {
return new InfoResponse<String>().setSuccess(R.string.modify_pwd_ok);
}
public void modifyPwd(String oldpwd, String newpwd) {
//获取个人信息
public InfoResponse<MyInfo> loadInfo() {
MyInfo myInfo = new MyInfo();
myInfo.setUsername("fff");
myInfo.setCreditScore(new Random().nextInt(100));
myInfo.setRecentPosts(new Random().nextInt(100));
myInfo.setRecentVisitors(new Random().nextInt(100));
myInfo.setMobie("123456");
myInfo.setEmail("abc@qq.com");
myInfo.setScoreHistory(new ArrayList<Integer>() {{
for (int i = 0; i < 100; i++) {
add(new Random().nextInt(100));
}
}});
return new InfoResponse<MyInfo>().setSuccess(R.string.load_info_ok).setBody(myInfo);
//获取个人信息
@HttpRequest("/my/info")
public void loadInfo(MutableLiveData<InfoResponse> liveData) {
HttpUtil.getRequest(HttpUtil.Method.GET,
new SimpleHttpParse<InfoResponse>(liveData, R.string.load_info_ok, R.string.load_info_fail).getInterface(InfoResponse.class));
}
}

@ -1,15 +1,15 @@
package com.community.pocket.data.main.visitor;
import androidx.lifecycle.MutableLiveData;
import com.community.pocket.R;
import com.community.pocket.data.model.Visitor;
import com.community.pocket.ui.main.ui.visitor.my.VisitorMyResponse;
import java.util.ArrayList;
import java.util.List;
import com.community.pocket.util.HttpRequest;
import com.community.pocket.util.HttpUtil;
import com.community.pocket.util.SimpleHttpParse;
/**
* 我的访客请求接口
* TODO 完善逻辑
*/
public class VisitorMyRequest {
private static volatile VisitorMyRequest instance;
@ -27,15 +27,9 @@ public class VisitorMyRequest {
/**
* 加载我的访客
*/
public VisitorMyResponse loadMy() {
List<Visitor> visitors = new ArrayList<>();
for (int i = 0; i < 30; i++) {
Visitor visitor = new Visitor("1231", System.currentTimeMillis(), "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890");
visitors.add(visitor);
}
VisitorMyResponse response = new VisitorMyResponse();
response.setSuccess(R.string.load_visitor_my_ok);
response.setBody(visitors);
return response;
@HttpRequest("/visitor/my")
public void loadMy(MutableLiveData<VisitorMyResponse> liveData) {
HttpUtil.getRequest(HttpUtil.Method.GET,
new SimpleHttpParse<VisitorMyResponse>(liveData, R.string.load_visitor_my_ok, R.string.load_visitor_my_fail).getInterface(VisitorMyResponse.class));
}
}

@ -1,14 +1,15 @@
package com.community.pocket.data.main.visitor;
import androidx.lifecycle.MutableLiveData;
import com.community.pocket.R;
import com.community.pocket.ui.main.ui.visitor.appointment.VisitorResponse;
import com.community.pocket.util.HttpRequest;
import com.community.pocket.util.HttpUtil;
import com.community.pocket.util.SimpleHttpParse;
import com.community.pocket.util.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import okhttp3.FormBody;
/**
* 访客接口
@ -34,23 +35,11 @@ public class VisitorRequest {
}
//获取预约人
public VisitorResponse<Map<String, List<String>>> searchPeople(String name) {
return new VisitorResponse<Map<String, List<String>>>().setSuccess(R.string.visitor_appointment_people_ok).setBody(new HashMap<String, List<String>>() {{
put("ac123" + new Random().nextInt(10), new ArrayList<String>() {{
add("11:00");
add("12:00");
}});
put("ab123" + new Random().nextInt(10), new ArrayList<String>() {{
add("11:00");
add("13:00");
}});
put("b123123", new ArrayList<String>() {{
add("11:00");
add("14:00");
}});
}});
@HttpRequest("/visitor/people")
public void searchPeople(MutableLiveData<VisitorResponse> liveData, String name) {
HttpUtil.getRequest(HttpUtil.Method.GET,
new SimpleHttpParse<VisitorResponse>(liveData, R.string.visitor_appointment_people_ok, R.string.visitor_appointment_people_fail).getInterface(VisitorResponse.class),
new FormBody.Builder().add("name", name).build());
}

@ -1,15 +1,15 @@
package com.community.pocket.data.main.visitor;
import androidx.lifecycle.MutableLiveData;
import com.community.pocket.R;
import com.community.pocket.data.model.Visitor;
import com.community.pocket.ui.main.ui.visitor.reservation.VisitorReservationResponse;
import java.util.ArrayList;
import java.util.List;
import com.community.pocket.util.HttpRequest;
import com.community.pocket.util.HttpUtil;
import com.community.pocket.util.SimpleHttpParse;
/**
* 我的预约请求接口
* TODO 完善逻辑
*/
public class VisitorReservationRequest {
private static volatile VisitorReservationRequest instance;
@ -25,17 +25,12 @@ public class VisitorReservationRequest {
}
/**
* @return 我的预约
* 我的预约
*/
public VisitorReservationResponse loadReservation() {
List<Visitor> visitors = new ArrayList<>();
for (int i = 0; i < 30; i++) {
Visitor visitor = new Visitor("1231", System.currentTimeMillis(), "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890");
visitors.add(visitor);
}
VisitorReservationResponse response = new VisitorReservationResponse();
response.setSuccess(R.string.load_visitor_my_ok);
response.setBody(visitors);
return response;
@HttpRequest("/visitor/reservation")
public void loadReservation(MutableLiveData<VisitorReservationResponse> liveData) {
HttpUtil.getRequest(HttpUtil.Method.GET,
new SimpleHttpParse<VisitorReservationResponse>(liveData, R.string.load_visitor_my_ok, R.string.load_visitor_my_fail).getInterface(VisitorReservationResponse.class));
}
}

@ -8,9 +8,15 @@ public class Visitor {
private long time;
private String notes;
public Visitor(String name, long time, String notes) {
public void setName(String name) {
this.name = name;
}
public void setTime(long time) {
this.time = time;
}
public void setNotes(String notes) {
this.notes = notes;
}

@ -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,7 +1,6 @@
package com.community.pocket.data.resetpwd;
import com.community.pocket.util.Result;
import com.community.pocket.util.Valid;
import com.community.pocket.ui.resetpwd.ResetPwdResponse;
/**
* 重置密码请求
@ -22,30 +21,34 @@ public class ResetPwdRequest {
/**
* 检查用户邮箱
* TODO R.string.resetpwd_step1_ok
*/
public Result<String> checkUser(String username, String email) {
return new Result<>(Valid.ok, null);
public ResetPwdResponse checkUser(String username, String email) {
return new ResetPwdResponse();
}
/**
* 发送验证码
* TODO R.string.resetpwd_step2_ok
*/
public Result<String> sendCode(String username, String email) {
return new Result<>(Valid.ok, null);
public ResetPwdResponse sendCode(String username, String email) {
return new ResetPwdResponse();
}
/**
* 检查验证码
* TODO R.string.resetpwd_step2_valid_ok
*/
public Result<String> checkCode(String username, String email, String code) {
return new Result<>(Valid.ok, null);
public ResetPwdResponse checkCode(String username, String email, String code) {
return new ResetPwdResponse();
}
/**
* 重置密码
* TODO R.string.resetpwd_step3_ok
*/
public Result<String> resetPwd(String username, String password) {
return new Result<>(Valid.ok, null);
public ResetPwdResponse resetPwd(String username, String password) {
return new ResetPwdResponse();
}
}

@ -114,9 +114,9 @@ public class ForumDataActivity extends BaseActivity {
});
//监听帖子详情数据
viewModel.getForumContentResponse().observe(this, new Observer<ForumDataResponse<List<ForumContent>>>() {
viewModel.getForumContentResponse().observe(this, new Observer<ForumDataResponse>() {
@Override
public void onChanged(ForumDataResponse<List<ForumContent>> listForumDataResponse) {
public void onChanged(ForumDataResponse listForumDataResponse) {
if (listForumDataResponse == null) {
return;
}

@ -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 class ForumDataResponse extends Response<List<ForumContent>> {
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 ForumDataResponse<T> setBody(T body) {
this.body = body;
return this;
}
}

@ -5,12 +5,8 @@ import androidx.lifecycle.ViewModel;
import com.community.pocket.R;
import com.community.pocket.data.main.forum.ForumDataRequest;
import com.community.pocket.data.model.ForumContent;
import com.community.pocket.util.Valid;
import com.community.pocket.util.ValidUtil;
import java.util.List;
/**
* 帖子详情数据管理
*/
@ -22,7 +18,7 @@ class ForumDataViewModel extends ViewModel {
private MutableLiveData<ForumDataResponse> replayResponse = new MutableLiveData<>();
//帖子详情数据状态
private MutableLiveData<ForumDataResponse<List<ForumContent>>> forumContentResponse = new MutableLiveData<>();
private MutableLiveData<ForumDataResponse> forumContentResponse = new MutableLiveData<>();
private ForumDataRequest forumDataRequest;
@ -38,7 +34,7 @@ class ForumDataViewModel extends ViewModel {
return replayResponse;
}
MutableLiveData<ForumDataResponse<List<ForumContent>>> getForumContentResponse() {
MutableLiveData<ForumDataResponse> getForumContentResponse() {
return forumContentResponse;
}
@ -57,16 +53,16 @@ class ForumDataViewModel extends ViewModel {
/**
* 回帖状态
*
*TODO
* @param content 内容
*/
void sendReply(String content) {
Valid valid = forumDataRequest.sendReply(content);
if (valid == Valid.ok) {
replayResponse.setValue(new ForumDataResponse().setSuccess(R.string.forum_reply_ok));
} else {
replayResponse.setValue(new ForumDataResponse().setError(R.string.forum_reply_fail));
}
// Valid valid = forumDataRequest.sendReply(content);
// if (valid == Valid.ok) {
// replayResponse.setValue(new ForumDataResponse().setSuccess(R.string.forum_reply_ok));
// } else {
// replayResponse.setValue(new ForumDataResponse().setError(R.string.forum_reply_fail));
// }
}
/**
@ -75,8 +71,7 @@ class ForumDataViewModel extends ViewModel {
* @param forumId 帖子id
*/
void loadData(int forumId) {
ForumDataResponse<List<ForumContent>> forumDataResponse = forumDataRequest.loadData(forumId);
forumContentResponse.setValue(forumDataResponse);
forumDataRequest.loadData(forumContentResponse, forumId);
}

@ -17,8 +17,7 @@ public class ForumHotViewModel extends BaseViewModel<ForumHotRequest> {
//加载热门信息
void loadHot() {
ForumHotResponse response = getRequest().loadHot();
forumHotResponse.setValue(response);
getRequest().loadHot(forumHotResponse);
}
@Override

@ -17,8 +17,7 @@ public class ForumViewModel extends BaseViewModel<ForumRequest> {
}
void loadNotice() {
ForumNoticeResponse response = getRequest().loadNotices();
noticeDataResponse.setValue(response);
getRequest().loadNotices(noticeDataResponse);
}
@Override

@ -16,8 +16,7 @@ public class ForumMyViewModel extends BaseViewModel<ForumMyRequest> {
}
void loadForumMy() {
ForumMyResponse response = getRequest().loadForumMy();
forumMyResponse.setValue(response);
getRequest().loadForumMy(forumMyResponse);
}
@Override

@ -18,8 +18,7 @@ public class ForumNewViewModel extends BaseViewModel<ForumNewRequest> {
}
void loadForumNew() {
ForumNewResponse response = getRequest().loadForumNew();
forumNewResponse.setValue(response);
getRequest().loadForumNew(forumNewResponse);
}
@Override

@ -4,7 +4,6 @@ import androidx.lifecycle.MutableLiveData;
import com.community.pocket.R;
import com.community.pocket.data.main.forum.ForumPostRequest;
import com.community.pocket.util.Valid;
import com.community.pocket.util.ValidUtil;
class ForumPostActiveViewModel extends ForumPostViewModel {
@ -39,12 +38,13 @@ class ForumPostActiveViewModel extends ForumPostViewModel {
}
//发帖请求状态
//TODO
void sendPost(String type, String title, String content, String activeStartTime, String activeEndTime, String activeScore) {
Valid valid = forumPostRequest.sendActive(type, title, content, activeStartTime, activeEndTime, activeScore);
if (valid == Valid.ok) {
forumPostResponse.setValue(new ForumPostResponse().setSuccess(R.string.forum_post_ok));
} else {
forumPostResponse.setValue(new ForumPostResponse().setError(R.string.forum_post_fail));
}
// Valid valid = forumPostRequest.sendActive(type, title, content, activeStartTime, activeEndTime, activeScore);
// if (valid == Valid.ok) {
// forumPostResponse.setValue(new ForumPostResponse().setSuccess(R.string.forum_post_ok));
// } else {
// forumPostResponse.setValue(new ForumPostResponse().setError(R.string.forum_post_fail));
// }
}
}

@ -79,9 +79,9 @@ public class ForumPostComplainFragment extends ForumPostContent {
//监听发帖状态
sendPost(viewModel);
viewModel.getSearchPeople().observe(getViewLifecycleOwner(), new Observer<ForumPostResponse<List<String>>>() {
viewModel.getSearchPeople().observe(getViewLifecycleOwner(), new Observer<ForumPostResponse>() {
@Override
public void onChanged(ForumPostResponse<List<String>> listForumPostResponse) {
public void onChanged(ForumPostResponse listForumPostResponse) {
if (listForumPostResponse == null) {
return;
}
@ -101,6 +101,7 @@ public class ForumPostComplainFragment extends ForumPostContent {
public void afterTextChanged(Editable s) {
if (nameList != null && nameList.containsKey(s.toString())) {
viewModel.complainDataChanged(postTitle.getText().toString(), postContent.getText().toString(), searchName.getText().toString());
return;
}
myAutoCompleteTextView.searchHandler();

@ -5,11 +5,8 @@ import androidx.lifecycle.MutableLiveData;
import com.community.pocket.R;
import com.community.pocket.data.main.forum.ForumPostRequest;
import com.community.pocket.ui.main.ui.share.SearchViewModel;
import com.community.pocket.util.Valid;
import com.community.pocket.util.ValidUtil;
import java.util.List;
/**
* 投诉贴数据管理
*/
@ -19,7 +16,7 @@ class ForumPostComplainViewModel extends ForumPostViewModel implements SearchVie
private MutableLiveData<ForumPostFormState.Complain> forumPostFormState = new MutableLiveData<>();
//检索投诉人
private MutableLiveData<ForumPostResponse<List<String>>> searchPeople = new MutableLiveData<>();
private MutableLiveData<ForumPostResponse> searchPeople = new MutableLiveData<>();
ForumPostComplainViewModel(ForumPostRequest forumPostRequest) {
super(forumPostRequest);
@ -29,7 +26,7 @@ class ForumPostComplainViewModel extends ForumPostViewModel implements SearchVie
return forumPostFormState;
}
MutableLiveData<ForumPostResponse<List<String>>> getSearchPeople() {
MutableLiveData<ForumPostResponse> getSearchPeople() {
return searchPeople;
}
@ -54,14 +51,15 @@ class ForumPostComplainViewModel extends ForumPostViewModel implements SearchVie
* @param title 标题
* @param content 正文
* @param complain 投诉人
* TODO
*/
void sendComplain(String title, String content, String complain) {
Valid valid = forumPostRequest.sendComplain(title, content, complain);
if (valid == Valid.ok) {
forumPostResponse.setValue(new ForumPostResponse().setSuccess(R.string.forum_post_ok));
} else {
forumPostResponse.setValue(new ForumPostResponse().setError(R.string.forum_post_fail));
}
// Valid valid = forumPostRequest.sendComplain(title, content, complain);
// if (valid == Valid.ok) {
// forumPostResponse.setValue(new ForumPostResponse().setSuccess(R.string.forum_post_ok));
// } else {
// forumPostResponse.setValue(new ForumPostResponse().setError(R.string.forum_post_fail));
// }
}
/**
@ -70,8 +68,7 @@ class ForumPostComplainViewModel extends ForumPostViewModel implements SearchVie
* @param name 投诉人
*/
private void searchPeople(String name) {
ForumPostResponse<List<String>> forumPostResponse = forumPostRequest.searchPeople(name);
searchPeople.setValue(forumPostResponse);
forumPostRequest.searchPeople(searchPeople, name);
}
@Override

@ -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;
}
}

@ -4,7 +4,6 @@ import androidx.lifecycle.MutableLiveData;
import com.community.pocket.R;
import com.community.pocket.data.main.forum.ForumPostRequest;
import com.community.pocket.util.Valid;
import com.community.pocket.util.ValidUtil;
/**
@ -34,12 +33,13 @@ class ForumPostTopicViewModel extends ForumPostViewModel {
}
//发送动态贴
//TODO
void sendTopic(String title, String content) {
Valid valid = forumPostRequest.sendTopic(title, content);
if (valid == Valid.ok) {
forumPostResponse.setValue(new ForumPostResponse().setSuccess(R.string.forum_post_ok));
} else {
forumPostResponse.setValue(new ForumPostResponse().setError(R.string.forum_post_fail));
}
// Valid valid = forumPostRequest.sendTopic(title, content);
// if (valid == Valid.ok) {
// forumPostResponse.setValue(new ForumPostResponse().setSuccess(R.string.forum_post_ok));
// } else {
// forumPostResponse.setValue(new ForumPostResponse().setError(R.string.forum_post_fail));
// }
}
}

@ -79,6 +79,7 @@ public class GarbageSortingFragment extends BaseFragment {
public void afterTextChanged(Editable s) {
if (garbageSortingAdpter != null && garbageSortingAdpter.containsKey(s.toString())) {
initView(garbageSortingAdpter.get(s.toString()));
return;
}
myAutoCompleteTextView.searchHandler();

@ -25,7 +25,6 @@ public class GarbageSortingViewModel extends BaseViewModel<GarbageSortingRequest
@Override
public void search(String content) {
GarbageSortingResponse response = getRequest().search();
garbageSortingResponse.setValue(response);
getRequest().search(garbageSortingResponse, content);
}
}

@ -103,6 +103,7 @@ public class GarbageWasteFragment extends BaseFragment {
layout.removeAllViews();
addHead();
addItem(garbageWasteAdpter.get(s.toString()));
return;
}
if (s.length() == 0) {

@ -29,14 +29,12 @@ public class GarbageWasteViewModel extends BaseViewModel<GarbageWasteRequest> im
//检索默认废品请求状态
void searchDefault() {
GarbageWasteResponse response = getRequest().searchDefault();
defaultList.setValue(response);
getRequest().searchDefault(defaultList);
}
//检索废品请求状态
@Override
public void search(String content) {
GarbageWasteResponse response = getRequest().search(content);
searchList.setValue(response);
getRequest().search(searchList, content);
}
}

@ -161,9 +161,9 @@ public class InfoFragment extends BaseFragment {
});
//监听个人信息请求状态
viewModel.getInfoResponse().observe(getViewLifecycleOwner(), new Observer<InfoResponse<MyInfo>>() {
viewModel.getInfoResponse().observe(getViewLifecycleOwner(), new Observer<InfoResponse>() {
@Override
public void onChanged(InfoResponse<MyInfo> myInfoInfoResponse) {
public void onChanged(InfoResponse myInfoInfoResponse) {
if (myInfoInfoResponse == null) {
return;
}
@ -280,5 +280,7 @@ public class InfoFragment extends BaseFragment {
lineData.addDataSet(lineDataSet);
lineChart.getDescription().setEnabled(false);
lineChart.setData(lineData);
lineChart.callOnClick();
}
}

@ -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> {
}

@ -5,7 +5,6 @@ import androidx.lifecycle.ViewModel;
import com.community.pocket.R;
import com.community.pocket.data.main.info.InfoRequest;
import com.community.pocket.data.model.MyInfo;
import com.community.pocket.util.ValidUtil;
//个人信息UI数据管理
@ -18,7 +17,7 @@ class InfoViewModel extends ViewModel {
private MutableLiveData<InfoResponse> modifyResponse = new MutableLiveData<>();
//个人信息请求状态
private MutableLiveData<InfoResponse<MyInfo>> infoResponse = new MutableLiveData<>();
private MutableLiveData<InfoResponse> infoResponse = new MutableLiveData<>();
//请求接口管理
private InfoRequest infoRequest;
@ -35,7 +34,7 @@ class InfoViewModel extends ViewModel {
return modifyResponse;
}
MutableLiveData<InfoResponse<MyInfo>> getInfoResponse() {
MutableLiveData<InfoResponse> getInfoResponse() {
return infoResponse;
}
@ -55,14 +54,14 @@ class InfoViewModel extends ViewModel {
}
//修改密码
//TODO
void modifyPwd(String oldpwd, String newpwd) {
InfoResponse<String> infoResponse = infoRequest.modifyPwd(oldpwd, newpwd);
modifyResponse.setValue(infoResponse);
// InfoResponse<String> infoResponse = infoRequest.modifyPwd(oldpwd, newpwd);
// modifyResponse.setValue(infoResponse);
}
//获取个人信息
void loadInfo() {
InfoResponse<MyInfo> myInfoInfoResponse = infoRequest.loadInfo();
infoResponse.setValue(myInfoInfoResponse);
infoRequest.loadInfo(infoResponse);
}
}

@ -2,8 +2,11 @@ package com.community.pocket.ui.main.ui.share;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.gson.Gson;
/**
* 响应体父类
*
@ -21,6 +24,27 @@ public abstract class Response<T> {
//响应体
private T body;
//响应信息
private String message;
private Result result;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Result getResult() {
return result;
}
public void setResult(Result result) {
this.result = result;
}
@Nullable
public Integer getSuccess() {
return success;
@ -70,4 +94,16 @@ public abstract class Response<T> {
public void setArgs(Object[] args) {
this.args = args;
}
public enum Result {
OK,
FAIL
}
@NonNull
@Override
public String toString() {
return new Gson().toJson(body);
}
}

@ -19,6 +19,7 @@ import androidx.lifecycle.ViewModelProvider;
import com.community.pocket.R;
import com.community.pocket.data.adapter.VisitorAdpter;
import com.community.pocket.data.model.VisitorPeople;
import com.community.pocket.ui.BaseFragment;
import com.community.pocket.ui.listener.MyTextChange;
import com.community.pocket.ui.main.ui.share.MyAutoCompleteTextView;
@ -33,7 +34,6 @@ import org.xutils.view.annotation.ViewInject;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
@ -129,9 +129,9 @@ public class VisitorAppointmentFragment extends BaseFragment {
});
//检索预约人请求结果
appointmentViewModel.getSearchPeopleReq().observe(getViewLifecycleOwner(), new Observer<VisitorResponse<Map<String, List<String>>>>() {
appointmentViewModel.getSearchPeopleReq().observe(getViewLifecycleOwner(), new Observer<VisitorResponse>() {
@Override
public void onChanged(VisitorResponse<Map<String, List<String>>> visitorResponse) {
public void onChanged(VisitorResponse visitorResponse) {
if (visitorResponse == null) {
return;
}
@ -160,6 +160,7 @@ public class VisitorAppointmentFragment extends BaseFragment {
if (visitorAdpter != null && visitorAdpter.containsKey(s.toString())) {
appointmentViewModel.appointmentDataChanged(appointment.getText(), notes.getText());
initTime(s);
return;
}
appointment.setTag(R.string.visitor_tag, s.toString());
@ -181,7 +182,7 @@ public class VisitorAppointmentFragment extends BaseFragment {
* 搜索框
*/
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
private void initSearch(Map<String, List<String>> arrays) {
private void initSearch(List<VisitorPeople> arrays) {
// 设置适配器
if (visitorAdpter == null) {
visitorAdpter = new VisitorAdpter(arrays, getContext());

@ -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>> {
}

@ -8,12 +8,8 @@ import androidx.lifecycle.ViewModel;
import com.community.pocket.R;
import com.community.pocket.data.main.visitor.VisitorRequest;
import com.community.pocket.ui.main.ui.share.SearchViewModel;
import com.community.pocket.util.Valid;
import com.community.pocket.util.ValidUtil;
import java.util.List;
import java.util.Map;
/**
* 管理访客UI数据
*/
@ -26,7 +22,7 @@ class VisitorViewModel extends ViewModel implements SearchViewModel {
private MutableLiveData<VisitorResponse> visitorResponse = new MutableLiveData<>();
//检索预约人
private MutableLiveData<VisitorResponse<Map<String, List<String>>>> searchPeopleReq = new MutableLiveData<>();
private MutableLiveData<VisitorResponse> searchPeopleReq = new MutableLiveData<>();
//访客请求
private VisitorRequest visitorRequest;
@ -43,24 +39,24 @@ class VisitorViewModel extends ViewModel implements SearchViewModel {
return visitorResponse;
}
MutableLiveData<VisitorResponse<Map<String, List<String>>>> getSearchPeopleReq() {
MutableLiveData<VisitorResponse> getSearchPeopleReq() {
return searchPeopleReq;
}
//访客预约
//TODO
void appointment(String appointment, String chooseTime, String notes) {
Valid valid = visitorRequest.appointment(appointment, chooseTime, notes);
if (valid == Valid.ok) {
visitorResponse.setValue(new VisitorResponse().setSuccess(R.string.visitor_appointment_ok));
} else {
visitorResponse.setValue(new VisitorResponse().setError(R.string.visitor_appointment_fail));
}
// Valid valid = visitorRequest.appointment(appointment, chooseTime, notes);
// if (valid == Valid.ok) {
// visitorResponse.setValue(new VisitorResponse().setSuccess(R.string.visitor_appointment_ok));
// } else {
// visitorResponse.setValue(new VisitorResponse().setError(R.string.visitor_appointment_fail));
// }
}
//检索预约人
private void searchPeople(String name) {
VisitorResponse<Map<String, List<String>>> result = visitorRequest.searchPeople(name);
searchPeopleReq.setValue(result);
visitorRequest.searchPeople(searchPeopleReq, name);
}
//校验访客预约表单

@ -21,8 +21,7 @@ public class VisitorMyViewModel extends BaseViewModel<VisitorMyRequest> {
* 加载我的访客
*/
void loadMy() {
VisitorMyResponse response = getRequest().loadMy();
visitorMyResponse.setValue(response);
getRequest().loadMy(visitorMyResponse);
}
@Override

@ -18,8 +18,7 @@ public class VisitorReservationViewModel extends BaseViewModel<VisitorReservatio
}
void loadReservation() {
VisitorReservationResponse response = getRequest().loadReservation();
visitorReservationResponse.setValue(response);
getRequest().loadReservation(visitorReservationResponse);
}

@ -5,7 +5,7 @@ import androidx.annotation.Nullable;
/**
* 重置密码结果
*/
class ResetPwdResponse {
public class ResetPwdResponse {
@Nullable
private Integer success;
@Nullable

@ -5,8 +5,6 @@ import androidx.lifecycle.ViewModel;
import com.community.pocket.R;
import com.community.pocket.data.resetpwd.ResetPwdRequest;
import com.community.pocket.util.Result;
import com.community.pocket.util.Valid;
import com.community.pocket.util.ValidUtil;
/**
@ -60,43 +58,26 @@ class ResetPwdViewModel extends ViewModel {
//重置密码第一步
void checkUser(String username, String email) {
Result<String> result = resetPwdRequest.checkUser(username, email);
if (result.getValid() == Valid.ok) {
resetPwdResultStep1.setValue(new ResetPwdResponse().setSuccess(R.string.resetpwd_step1_ok));
} else {
resetPwdResultStep1.setValue(new ResetPwdResponse().setError(R.string.resetpwd_step1_fail));
}
ResetPwdResponse response = resetPwdRequest.checkUser(username, email);
resetPwdResultStep1.setValue(response);
}
//重置密码第二步
void sendCode(String username, String email) {
Result<String> result = resetPwdRequest.sendCode(username, email);
if (result.getValid() == Valid.ok) {
resetPwdResultStep2.setValue(new ResetPwdResponse().setSuccess(R.string.resetpwd_step2_ok));
resetpwdStep2Changed();
} else {
resetPwdResultStep2.setValue(new ResetPwdResponse().setError(R.string.resetpwd_step2_fail));
}
ResetPwdResponse response = resetPwdRequest.sendCode(username, email);
resetPwdResultStep2.setValue(response);
}
//重置密码第二步
void checkCode(String username, String email, String code) {
Result<String> result = resetPwdRequest.checkCode(username, email, code);
if (result.getValid() == Valid.ok) {
resetPwdResultStep2.setValue(new ResetPwdResponse().setSuccess(R.string.resetpwd_step2_valid_ok));
} else {
resetPwdResultStep2.setValue(new ResetPwdResponse().setError(R.string.resetpwd_step2_valid_fail));
}
ResetPwdResponse response = resetPwdRequest.checkCode(username, email, code);
resetPwdResultStep2.setValue(response);
}
//重置密码第三步
void resetPwd(String username, String password) {
Result<String> result = resetPwdRequest.resetPwd(username, password);
if (result.getValid() == Valid.ok) {
resetPwdResultStep3.setValue(new ResetPwdResponse().setSuccess(R.string.resetpwd_step3_ok));
} else {
resetPwdResultStep3.setValue(new ResetPwdResponse().setError(R.string.resetpwd_step3_fail));
}
ResetPwdResponse response = resetPwdRequest.resetPwd(username, password);
resetPwdResultStep3.setValue(response);
}
//监听重置密码第一步表单触发校验

@ -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);
}
}

@ -1,5 +1,6 @@
package com.community.pocket.util;
//TODO 测试类
public enum Valid {
empty_err,
password_err,

@ -2,7 +2,7 @@ package com.community.pocket;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
/**
* Example local unit test, which will execute on the development machine (host).
@ -14,4 +14,11 @@ public class ExampleUnitTest {
public void addition_isCorrect() {
assertEquals(4, 2 + 2);
}
@Test
public void class_tuil() {
// ForumRequest.getInstance().loadNotices(null);
//
// TimeUnit.SECONDS.sleep(100);
}
}
Loading…
Cancel
Save