Commit eb171a063423a83a29345307d36072dd70ac18d4
1 parent
5c3224d448
Exists in
master
大模型批改
Showing
10 changed files
with
141 additions
and
12 deletions
Show diff stats
app/src/main/java/com/hjx/parent/JudgeActivity.java
1 | package com.hjx.parent; | 1 | package com.hjx.parent; |
2 | 2 | ||
3 | import android.annotation.SuppressLint; | 3 | import android.annotation.SuppressLint; |
4 | import android.graphics.Bitmap; | 4 | import android.graphics.Bitmap; |
5 | import android.graphics.Rect; | 5 | import android.graphics.Rect; |
6 | import android.os.Bundle; | 6 | import android.os.Bundle; |
7 | import android.util.Log; | ||
7 | 8 | ||
8 | import com.bumptech.glide.Glide; | 9 | import com.bumptech.glide.Glide; |
10 | import com.hjx.parent.api.JudgeRepository; | ||
9 | import com.hjx.parent.databinding.ActivityJudgeBinding; | 11 | import com.hjx.parent.databinding.ActivityJudgeBinding; |
10 | import com.hjx.parent.databinding.LayoutJudgeRectBinding; | 12 | import com.hjx.parent.databinding.LayoutJudgeRectBinding; |
11 | import com.hjx.parent.rx.BaseRxActivity; | 13 | import com.hjx.parent.rx.BaseRxActivity; |
12 | import com.hjx.parent.utils.CutUtil; | 14 | import com.hjx.parent.utils.CutUtil; |
13 | 15 | ||
14 | import java.io.File; | 16 | import java.io.File; |
15 | import java.util.ArrayList; | 17 | import java.util.ArrayList; |
16 | import java.util.List; | 18 | import java.util.List; |
17 | 19 | ||
20 | import io.reactivex.Observable; | ||
18 | import io.reactivex.Single; | 21 | import io.reactivex.Single; |
19 | import top.zibin.luban.Luban; | 22 | import top.zibin.luban.Luban; |
20 | 23 | ||
21 | public class JudgeActivity extends BaseRxActivity<ActivityJudgeBinding> { | 24 | public class JudgeActivity extends BaseRxActivity<ActivityJudgeBinding> { |
22 | 25 | ||
23 | private Bitmap bitmap; | 26 | private Bitmap bitmap; |
24 | 27 | ||
25 | private final List<JudgeCut> mList = new ArrayList<>(); | 28 | private final List<JudgeCut> mList = new ArrayList<>(); |
26 | 29 | ||
27 | @SuppressLint("CheckResult") | 30 | @SuppressLint("CheckResult") |
28 | @Override | 31 | @Override |
29 | public void initView(Bundle savedInstanceState) { | 32 | public void initView(Bundle savedInstanceState) { |
30 | binding.ivBack.setOnClickListener(v -> { | 33 | binding.ivBack.setOnClickListener(v -> { |
31 | onBackPressed(); | 34 | onBackPressed(); |
32 | }); | 35 | }); |
33 | String path = getIntent().getStringExtra("path"); | 36 | String path = getIntent().getStringExtra("path"); |
34 | if (path == null) return; | 37 | if (path == null) return; |
35 | final ArrayList<Rect> rects = getIntent().getParcelableArrayListExtra("rects"); | 38 | final ArrayList<Rect> rects = getIntent().getParcelableArrayListExtra("rects"); |
36 | if (rects == null) return; | 39 | if (rects == null) return; |
37 | 40 | ||
38 | Single.just(path) | 41 | Single.just(path) |
39 | .map(origin -> Luban.with(this).load(origin).ignoreBy(300).get().get(0)) | 42 | .map(origin -> Luban.with(this).load(origin).ignoreBy(300).get().get(0)) |
40 | .map(file -> Glide.with(this).asBitmap().load(file).submit().get()) | 43 | .map(file -> Glide.with(this).asBitmap().load(file).submit().get()) |
41 | .compose(transformSingle()) | 44 | .compose(transformSingle()) |
42 | .subscribe((bitmap, th) -> { | 45 | .subscribe((bitmap, th) -> { |
43 | if (th != null) th.printStackTrace(); | 46 | if (th != null) th.printStackTrace(); |
44 | if (bitmap == null) return; | 47 | if (bitmap == null) return; |
45 | this.bitmap = bitmap; | 48 | this.bitmap = bitmap; |
46 | 49 | ||
47 | binding.ivPic.setImageBitmap(bitmap); | 50 | binding.ivPic.setImageBitmap(bitmap); |
48 | CutUtil.onLayoutReady(binding.ivPic, v -> { | 51 | CutUtil.onLayoutReady(binding.ivPic, v -> { |
49 | prepareRects(rects); | 52 | prepareRects(rects); |
50 | }); | 53 | }); |
51 | }); | 54 | }); |
52 | } | 55 | } |
53 | 56 | ||
54 | private void prepareRects(List<Rect> rects) { | 57 | private void prepareRects(List<Rect> rects) { |
55 | mList.clear(); | 58 | mList.clear(); |
56 | binding.flRects.removeAllViews(); | 59 | binding.flRects.removeAllViews(); |
57 | float[] measuredSize = CutUtil.measureBitmap(binding.ivPic, bitmap); | 60 | float[] measuredSize = CutUtil.measureBitmap(binding.ivPic, bitmap); |
58 | for (int i = 0; i < rects.size(); i++) { | 61 | for (int i = 0; i < rects.size(); i++) { |
59 | Rect it = rects.get(i); | 62 | Rect it = rects.get(i); |
60 | Bitmap bitmap = CutUtil.cut(this.bitmap, it.left, it.top, it.width(), it.height(), this); | 63 | Bitmap bitmap = CutUtil.cut(this.bitmap, it.left, it.top, it.width(), it.height(), this); |
61 | Rect rect = measureRect(it, measuredSize); | 64 | Rect rect = measureRect(it, measuredSize); |
62 | LayoutJudgeRectBinding vb = showRect(rect); | 65 | LayoutJudgeRectBinding vb = showRect(rect); |
63 | mList.add(new JudgeCut(i, bitmap, rect, vb)); | 66 | mList.add(new JudgeCut(i, bitmap, rect, vb)); |
64 | } | 67 | } |
68 | |||
69 | judge(); | ||
70 | } | ||
71 | |||
72 | @SuppressLint("CheckResult") | ||
73 | private void judge() { | ||
74 | showLoadingDialog(""); | ||
75 | Observable.fromIterable(mList) | ||
76 | .map(it -> { | ||
77 | if (it.url != null && !it.url.isEmpty()) return it; | ||
78 | String base64 = CutUtil.bitmapToBase64(it.bitmap, true); | ||
79 | it.url = base64; | ||
80 | return it; | ||
81 | }) | ||
82 | .flatMap(it -> JudgeRepository.singleJudge(it.url) | ||
83 | .map(result -> { | ||
84 | it.correctResult = result; | ||
85 | return it; | ||
86 | }) | ||
87 | .toObservable() | ||
88 | ) | ||
89 | .compose(transform()) | ||
90 | .subscribe(it -> { | ||
91 | if (it.correctResult == 1) { | ||
92 | it.vb.ivResult.setImageResource(R.drawable.png_ic_judge_correct); | ||
93 | it.vb.getRoot().setSelected(false); | ||
94 | } else if (it.correctResult == 2) { | ||
95 | it.vb.ivResult.setImageResource(R.drawable.png_ic_judge_wrong); | ||
96 | it.vb.getRoot().setSelected(true); | ||
97 | } | ||
98 | }, th -> { | ||
99 | cancelLoadingDialog(); | ||
100 | Log.e(getClass().getName(), "", th); | ||
101 | }, this::cancelLoadingDialog) | ||
102 | ; | ||
65 | } | 103 | } |
66 | 104 | ||
67 | private LayoutJudgeRectBinding showRect(Rect rect) { | 105 | private LayoutJudgeRectBinding showRect(Rect rect) { |
68 | LayoutJudgeRectBinding vb = LayoutJudgeRectBinding.inflate(getLayoutInflater(), binding.flRects, false); | 106 | LayoutJudgeRectBinding vb = LayoutJudgeRectBinding.inflate(getLayoutInflater(), binding.flRects, false); |
69 | 107 | ||
70 | vb.getRoot().setMinimumWidth(rect.width()); | 108 | vb.getRoot().setMinimumWidth(rect.width()); |
71 | vb.getRoot().setMinimumHeight(rect.height()); | 109 | vb.getRoot().setMinimumHeight(rect.height()); |
72 | vb.getRoot().setTranslationX((float) rect.left); | 110 | vb.getRoot().setTranslationX((float) rect.left); |
73 | vb.getRoot().setTranslationY((float) rect.top); | 111 | vb.getRoot().setTranslationY((float) rect.top); |
74 | vb.getRoot().setOnClickListener(v -> { | 112 | vb.getRoot().setOnClickListener(v -> { |
75 | v.setSelected(!v.isSelected()); | 113 | v.setSelected(!v.isSelected()); |
76 | checkCount(); | 114 | checkCount(); |
77 | }); | 115 | }); |
78 | binding.flRects.addView(vb.getRoot()); | 116 | binding.flRects.addView(vb.getRoot()); |
79 | 117 | ||
80 | return vb; | 118 | return vb; |
81 | } | 119 | } |
82 | 120 | ||
83 | private void checkCount() { | 121 | private void checkCount() { |
84 | // | 122 | // |
85 | } | 123 | } |
86 | 124 | ||
87 | /** bitmapRect 转换为 viewRect */ | 125 | /** bitmapRect 转换为 viewRect */ |
88 | private Rect measureRect(Rect rect, float[] measuredSize) { | 126 | private Rect measureRect(Rect rect, float[] measuredSize) { |
89 | float realW = measuredSize[0]; | 127 | float realW = measuredSize[0]; |
90 | float realH = measuredSize[1]; | 128 | float realH = measuredSize[1]; |
91 | float offsetX = measuredSize[2]; | 129 | float offsetX = measuredSize[2]; |
92 | float offsetY = measuredSize[3]; | 130 | float offsetY = measuredSize[3]; |
93 | float percentX = realW / bitmap.getWidth(); | 131 | float percentX = realW / bitmap.getWidth(); |
94 | float percentY = realH / bitmap.getHeight(); | 132 | float percentY = realH / bitmap.getHeight(); |
95 | 133 | ||
96 | float left = rect.left * percentX + offsetX; | 134 | float left = rect.left * percentX + offsetX; |
97 | float right = rect.right * percentX + offsetX; | 135 | float right = rect.right * percentX + offsetX; |
98 | float top = rect.top * percentY + offsetY; | 136 | float top = rect.top * percentY + offsetY; |
99 | float bottom = rect.bottom * percentY + offsetY; | 137 | float bottom = rect.bottom * percentY + offsetY; |
100 | return new Rect((int) left, (int) top, (int) right, (int) bottom); | 138 | return new Rect((int) left, (int) top, (int) right, (int) bottom); |
101 | } | 139 | } |
102 | 140 | ||
103 | @Override | 141 | @Override |
104 | protected ActivityJudgeBinding getViewBinding() { | 142 | protected ActivityJudgeBinding getViewBinding() { |
105 | return ActivityJudgeBinding.inflate(getLayoutInflater()); | 143 | return ActivityJudgeBinding.inflate(getLayoutInflater()); |
106 | } | 144 | } |
107 | 145 | ||
108 | static class JudgeCut { | 146 | static class JudgeCut { |
109 | final int index; | 147 | final int index; |
110 | final Bitmap bitmap; | 148 | final Bitmap bitmap; |
111 | final Rect rect; | 149 | final Rect rect; |
112 | final LayoutJudgeRectBinding vb; | 150 | final LayoutJudgeRectBinding vb; |
113 | 151 | ||
152 | String url; | ||
114 | int correctResult = 0; | 153 | int correctResult = 0; |
115 | 154 | ||
116 | public JudgeCut(int index, Bitmap bitmap, Rect rect, LayoutJudgeRectBinding vb) { | 155 | public JudgeCut(int index, Bitmap bitmap, Rect rect, LayoutJudgeRectBinding vb) { |
117 | this.index = index; | 156 | this.index = index; |
118 | this.bitmap = bitmap; | 157 | this.bitmap = bitmap; |
119 | this.rect = rect; | 158 | this.rect = rect; |
120 | this.vb = vb; | 159 | this.vb = vb; |
121 | } | 160 | } |
122 | } | 161 | } |
123 | } | 162 | } |
124 | 163 |
app/src/main/java/com/hjx/parent/api/ChatApi.java
1 | package com.hjx.parent.api; | 1 | package com.hjx.parent.api; |
2 | 2 | ||
3 | import io.reactivex.Single; | ||
4 | import retrofit2.http.Body; | ||
3 | import retrofit2.http.Header; | 5 | import retrofit2.http.Header; |
4 | import retrofit2.http.POST; | 6 | import retrofit2.http.POST; |
5 | import retrofit2.http.Url; | 7 | import retrofit2.http.Url; |
6 | 8 | ||
7 | public interface ChatApi { | 9 | public interface ChatApi { |
8 | @POST | 10 | @POST |
9 | void aiChat(@Url String url, @Header("Authorization") String token); | 11 | Single<ChatResponse> aiChat( |
12 | @Url String url, | ||
13 | @Header("Authorization") String token, | ||
14 | @Body ChatRequest body | ||
15 | ); | ||
16 | |||
10 | } | 17 | } |
11 | 18 |
app/src/main/java/com/hjx/parent/api/ChatMessage.java
1 | package com.hjx.parent.api; | 1 | package com.hjx.parent.api; |
2 | 2 | ||
3 | import java.util.ArrayList; | 3 | import java.util.ArrayList; |
4 | import java.util.List; | 4 | import java.util.List; |
5 | import java.util.Objects; | 5 | import java.util.Objects; |
6 | 6 | ||
7 | public class ChatMessage { | 7 | public class ChatMessage { |
8 | public ChatMessage() { | 8 | public ChatMessage() { |
9 | } | 9 | } |
10 | public ChatMessage(String role, Object content) { | 10 | public ChatMessage(String role, Object content) { |
11 | this.role = role; | 11 | this.role = role; |
12 | this.content = content; | 12 | this.content = content; |
13 | } | 13 | } |
14 | 14 | ||
15 | public String role; | 15 | public String role; |
16 | public Object content; | 16 | public Object content; |
17 | 17 | ||
18 | public static ChatMessage fromImage(String role, String text, String... urls) { | 18 | public static ChatMessage fromImage(String role, String text, String... urls) { |
19 | List<ContentItem> items = new ArrayList<>(); | 19 | List<ContentItem> items = new ArrayList<>(); |
20 | if (text != null && !text.isEmpty()) { | 20 | if (text != null && !text.isEmpty()) { |
21 | items.add(new ContentItem(ContentItem.TYPE_TEXT, text)); | 21 | items.add(new ContentItem(ContentItem.TYPE_TEXT, text)); |
22 | } | 22 | } |
23 | for (String it: urls) { | 23 | for (String it: urls) { |
24 | items.add(new ContentItem(ContentItem.TYPE_IMAGE, it)); | 24 | ContentItem.ContentImage image = new ContentItem.ContentImage(it); |
25 | items.add(new ContentItem(ContentItem.TYPE_IMAGE, image)); | ||
25 | } | 26 | } |
26 | return new ChatMessage(role, items); | 27 | return new ChatMessage(role, items); |
27 | } | 28 | } |
28 | 29 | ||
29 | public String getTextContent() { | 30 | public String getTextContent() { |
30 | if (content == null) return null; | 31 | if (content == null) return null; |
31 | if (content instanceof String) return (String) content; | 32 | if (content instanceof String) return (String) content; |
32 | ContentItem item = firstText(); | 33 | ContentItem item = firstText(); |
33 | return item == null ? null : item.text; | 34 | return item == null ? null : item.text; |
34 | } | 35 | } |
35 | 36 | ||
36 | private ContentItem firstText() { | 37 | private ContentItem firstText() { |
37 | if (!(content instanceof List<?>)) return null; | 38 | if (!(content instanceof List<?>)) return null; |
38 | List<?> anyList = (List<?>) content; | 39 | List<?> anyList = (List<?>) content; |
39 | return anyList.stream() | 40 | return anyList.stream() |
40 | .map(ContentItem::reSerialize) | 41 | .map(ContentItem::reSerialize) |
41 | .filter(Objects::nonNull) | 42 | .filter(Objects::nonNull) |
42 | .findFirst().orElse(null); | 43 | .findFirst().orElse(null); |
43 | } | 44 | } |
44 | } | 45 | } |
45 | 46 |
app/src/main/java/com/hjx/parent/api/ChatRequest.java
1 | package com.hjx.parent.api; | 1 | package com.hjx.parent.api; |
2 | 2 | ||
3 | import java.util.List; | 3 | import java.util.List; |
4 | 4 | ||
5 | public class ChatRequest { | 5 | public class ChatRequest { |
6 | public String model; | 6 | public String model; |
7 | public List<ChatMessage> messages; | 7 | public List<ChatMessage> messages; |
8 | public boolean stream; | 8 | public boolean stream; |
9 | |||
10 | public ChatRequest(String model, List<ChatMessage> messages) { | ||
11 | this.model = model; | ||
12 | this.messages = messages; | ||
13 | this.stream = false; | ||
14 | } | ||
9 | } | 15 | } |
10 | 16 |
app/src/main/java/com/hjx/parent/api/ContentItem.java
1 | package com.hjx.parent.api; | 1 | package com.hjx.parent.api; |
2 | 2 | ||
3 | import com.google.gson.Gson; | 3 | import com.google.gson.Gson; |
4 | import com.google.gson.annotations.SerializedName; | 4 | import com.google.gson.annotations.SerializedName; |
5 | 5 | ||
6 | public class ContentItem { | 6 | public class ContentItem { |
7 | public static final String TYPE_TEXT = "text"; | 7 | public static final String TYPE_TEXT = "text"; |
8 | public static final String TYPE_IMAGE = "image_url"; | 8 | public static final String TYPE_IMAGE = "image_url"; |
9 | 9 | ||
10 | 10 | ||
11 | public ContentItem() { | 11 | public ContentItem() { |
12 | } | 12 | } |
13 | public ContentItem(String type, String text) { | 13 | public ContentItem(String type, String text) { |
14 | this.type = type; | 14 | this.type = type; |
15 | this.text = text; | 15 | this.text = text; |
16 | } | 16 | } |
17 | public ContentItem(String type, ContentImage imageUrl) { | 17 | public ContentItem(String type, ContentImage imageUrl) { |
18 | this.type = type; | 18 | this.type = type; |
19 | this.imageUrl = imageUrl; | 19 | this.imageUrl = imageUrl; |
20 | } | 20 | } |
21 | 21 | ||
22 | public String type; | 22 | public String type; |
23 | public String text; | 23 | public String text; |
24 | @SerializedName("image_url") | 24 | @SerializedName("image_url") |
25 | public ContentImage imageUrl; | 25 | public ContentImage imageUrl; |
26 | 26 | ||
27 | public static class ContentImage { | 27 | public static class ContentImage { |
28 | public ContentImage(String url) { | ||
29 | this.url = url; | ||
30 | } | ||
31 | |||
28 | public String url; | 32 | public String url; |
29 | } | 33 | } |
30 | 34 | ||
31 | 35 | ||
32 | private static final Gson gson = new Gson(); | 36 | private static final Gson gson = new Gson(); |
33 | static ContentItem reSerialize(Object any) { | 37 | static ContentItem reSerialize(Object any) { |
34 | if (any == null) return null; | 38 | if (any == null) return null; |
35 | try { | 39 | try { |
36 | String json = gson.toJson(any); | 40 | String json = gson.toJson(any); |
37 | ContentItem it = gson.fromJson(json, ContentItem.class); | 41 | ContentItem it = gson.fromJson(json, ContentItem.class); |
38 | return it; | 42 | return it; |
39 | } catch (Throwable t) { | 43 | } catch (Throwable t) { |
40 | t.printStackTrace(); | 44 | t.printStackTrace(); |
41 | } | 45 | } |
42 | return null; | 46 | return null; |
43 | } | 47 | } |
44 | } | 48 | } |
45 | 49 |
app/src/main/java/com/hjx/parent/api/JudgeRepository.java
File was created | 1 | package com.hjx.parent.api; | |
2 | |||
3 | import android.annotation.SuppressLint; | ||
4 | |||
5 | import com.google.gson.Gson; | ||
6 | import com.prws.common.net.NetWorks; | ||
7 | |||
8 | import java.util.ArrayList; | ||
9 | import java.util.List; | ||
10 | |||
11 | import io.reactivex.Single; | ||
12 | import retrofit2.Retrofit; | ||
13 | |||
14 | public class JudgeRepository { | ||
15 | private static final Gson gson = new Gson(); | ||
16 | private static final ChatApi api; | ||
17 | static { | ||
18 | Retrofit retrofit = NetWorks.retrofit; | ||
19 | api = retrofit.create(ChatApi.class); | ||
20 | } | ||
21 | |||
22 | |||
23 | public static Single<Integer> singleJudge(String url) { | ||
24 | List<ChatMessage> messages = new ArrayList<>(); | ||
25 | messages.add(ChatMessage.fromImage("system", setting, url)); | ||
26 | |||
27 | return api.aiChat(JudgeRepository.url, token, new ChatRequest(model, messages)) | ||
28 | .map(response -> { | ||
29 | if (response.choices == null) return null; | ||
30 | return response.choices.stream().findFirst() | ||
31 | .map(it -> it.message) | ||
32 | .map(ChatMessage::getTextContent) | ||
33 | .orElse(null); | ||
34 | }) | ||
35 | .map(json -> { | ||
36 | JudgeResult result = gson.fromJson(json, JudgeResult.class); | ||
37 | return result.result; | ||
38 | }) | ||
39 | ; | ||
40 | } | ||
41 | |||
42 | |||
43 | private static final String model = "doubao-1-5-thinking-vision-pro-250428"; | ||
44 | private static final String url = "https://ark.cn-beijing.volces.com/api/v3/chat/completions"; | ||
45 | private static final String token = "Bearer a877087e-bb74-471b-8f2a-01e6b2220699"; | ||
46 | private static final String setting = | ||
47 | "批改图片中的题目, 判断作答是否正确,并以Json格式返回结果,需要包含以下字段: \n" + | ||
48 | "result(批改结果, 枚举 0:不确定; 1:正确; 2:错误; 3:未作答).\n" + | ||
49 | "Json示例(供参考,无需复制): {\"result\": 1}\n" + | ||
50 | "一个图片中一般只会有一个题目, 返回一个结果即可."; | ||
51 | |||
52 | private static class JudgeResult { | ||
53 | public int result; | ||
54 | } | ||
55 | } | ||
56 |
app/src/main/java/com/hjx/parent/utils/CutUtil.java
1 | package com.hjx.parent.utils; | 1 | package com.hjx.parent.utils; |
2 | 2 | ||
3 | import android.content.Context; | 3 | import android.content.Context; |
4 | import android.graphics.Bitmap; | 4 | import android.graphics.Bitmap; |
5 | import android.graphics.Canvas; | 5 | import android.graphics.Canvas; |
6 | import android.graphics.Color; | 6 | import android.graphics.Color; |
7 | import android.graphics.Paint; | 7 | import android.graphics.Paint; |
8 | import android.util.Base64; | ||
8 | import android.view.View; | 9 | import android.view.View; |
9 | import android.view.ViewTreeObserver; | 10 | import android.view.ViewTreeObserver; |
10 | 11 | ||
11 | import com.bumptech.glide.Glide; | 12 | import com.bumptech.glide.Glide; |
12 | import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool; | 13 | import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool; |
13 | import com.hjx.parent.function.Function1; | 14 | import com.hjx.parent.function.Function1; |
14 | 15 | ||
16 | import java.io.ByteArrayOutputStream; | ||
17 | |||
15 | public class CutUtil { | 18 | public class CutUtil { |
16 | 19 | ||
17 | public static float[] measureBitmap(View view, Bitmap bitmap) { | 20 | public static float[] measureBitmap(View view, Bitmap bitmap) { |
18 | float realW; | 21 | float realW; |
19 | float realH; | 22 | float realH; |
20 | float offsetX; | 23 | float offsetX; |
21 | float offsetY; | 24 | float offsetY; |
22 | 25 | ||
23 | float vp = 1f * view.getWidth() / view.getHeight(); | 26 | float vp = 1f * view.getWidth() / view.getHeight(); |
24 | float bp = 1f * bitmap.getWidth() / bitmap.getHeight(); | 27 | float bp = 1f * bitmap.getWidth() / bitmap.getHeight(); |
25 | 28 | ||
26 | if (vp > bp) { | 29 | if (vp > bp) { |
27 | realW = view.getHeight() * bp; | 30 | realW = view.getHeight() * bp; |
28 | realH = (float) view.getHeight(); | 31 | realH = (float) view.getHeight(); |
29 | offsetY = 0f; | 32 | offsetY = 0f; |
30 | offsetX = (view.getWidth() - realW) * 0.5f; | 33 | offsetX = (view.getWidth() - realW) * 0.5f; |
31 | } else if (vp < bp) { | 34 | } else if (vp < bp) { |
32 | realW = (float) view.getWidth(); | 35 | realW = (float) view.getWidth(); |
33 | realH = view.getWidth() * (1f / bp); | 36 | realH = view.getWidth() * (1f / bp); |
34 | offsetX = 0f; | 37 | offsetX = 0f; |
35 | offsetY = (view.getHeight() - realH) * 0.5f; | 38 | offsetY = (view.getHeight() - realH) * 0.5f; |
36 | } else { | 39 | } else { |
37 | realW = (float) view.getWidth(); | 40 | realW = (float) view.getWidth(); |
38 | realH = (float) view.getHeight(); | 41 | realH = (float) view.getHeight(); |
39 | offsetX = 0f; | 42 | offsetX = 0f; |
40 | offsetY = 0f; | 43 | offsetY = 0f; |
41 | } | 44 | } |
42 | 45 | ||
43 | return new float[]{realW, realH, offsetX, offsetY}; | 46 | return new float[]{realW, realH, offsetX, offsetY}; |
44 | } | 47 | } |
45 | 48 | ||
46 | public static Bitmap cut(Bitmap source, float x, float y, int w, int h, Context context) { | 49 | public static Bitmap cut(Bitmap source, float x, float y, int w, int h, Context context) { |
47 | // 获取 Glide 的 BitmapPool | 50 | // 获取 Glide 的 BitmapPool |
48 | BitmapPool bitmapPool = Glide.get(context).getBitmapPool(); | 51 | BitmapPool bitmapPool = Glide.get(context).getBitmapPool(); |
49 | 52 | ||
50 | // 从 BitmapPool 中获取可复用的 Bitmap | 53 | // 从 BitmapPool 中获取可复用的 Bitmap |
51 | Bitmap result = bitmapPool.get(w, h, source.getConfig() != null ? source.getConfig() : Bitmap.Config.ARGB_8888); | 54 | Bitmap result = bitmapPool.get(w, h, source.getConfig() != null ? source.getConfig() : Bitmap.Config.ARGB_8888); |
52 | 55 | ||
53 | // 填充白色背景 | 56 | // 填充白色背景 |
54 | result.eraseColor(Color.WHITE); | 57 | result.eraseColor(Color.WHITE); |
55 | 58 | ||
56 | // 创建画布并绘制 | 59 | // 创建画布并绘制 |
57 | Canvas canvas = new Canvas(result); | 60 | Canvas canvas = new Canvas(result); |
58 | Paint paint = new Paint(); | 61 | Paint paint = new Paint(); |
59 | canvas.drawBitmap(source, -x, -y, paint); | 62 | canvas.drawBitmap(source, -x, -y, paint); |
60 | 63 | ||
61 | return result; | 64 | return result; |
62 | } | 65 | } |
63 | 66 | ||
64 | public static void onLayoutReady(final View view, final Function1<View> action) { | 67 | public static void onLayoutReady(final View view, final Function1<View> action) { |
65 | view.getViewTreeObserver().addOnGlobalLayoutListener( | 68 | view.getViewTreeObserver().addOnGlobalLayoutListener( |
66 | new ViewTreeObserver.OnGlobalLayoutListener() { | 69 | new ViewTreeObserver.OnGlobalLayoutListener() { |
67 | @Override | 70 | @Override |
68 | public void onGlobalLayout() { | 71 | public void onGlobalLayout() { |
69 | view.getViewTreeObserver().removeOnGlobalLayoutListener(this); | 72 | view.getViewTreeObserver().removeOnGlobalLayoutListener(this); |
70 | action.invoke(view); | 73 | action.invoke(view); |
71 | } | 74 | } |
72 | } | 75 | } |
73 | ); | 76 | ); |
74 | } | 77 | } |
78 | |||
79 | public static byte[] bitmapToByteArray(Bitmap bitmap) { | ||
80 | ByteArrayOutputStream output = new ByteArrayOutputStream(); | ||
81 | bitmap.compress(Bitmap.CompressFormat.JPEG, 100, output); | ||
82 | return output.toByteArray(); | ||
83 | } | ||
84 | |||
85 | public static String bitmapToBase64(Bitmap bitmap, Boolean usePrefix) { | ||
86 | String base64 = Base64.encodeToString(bitmapToByteArray(bitmap), Base64.NO_WRAP); | ||
87 | if (usePrefix) return "data:image/jpeg;base64," + base64; | ||
88 | else return base64; | ||
89 | } | ||
75 | } | 90 | } |
76 | 91 |
app/src/main/res/drawable/png_ic_judge_wrong.png
1.12 KB
libs/common/src/main/java/com/prws/common/net/NetWorks.java
1 | package com.prws.common.net; | 1 | package com.prws.common.net; |
2 | 2 | ||
3 | 3 | ||
4 | import com.google.gson.Gson; | 4 | import com.google.gson.Gson; |
5 | import com.google.gson.JsonObject; | 5 | import com.google.gson.JsonObject; |
6 | import com.prws.common.BuildConfig; | 6 | import com.prws.common.BuildConfig; |
7 | import com.prws.common.bean.CutPicBean; | 7 | import com.prws.common.bean.CutPicBean; |
8 | import com.prws.common.bean.GradeAndSubject; | 8 | import com.prws.common.bean.GradeAndSubject; |
9 | import com.prws.common.bean.PageInfo; | 9 | import com.prws.common.bean.PageInfo; |
10 | import com.prws.common.bean.ResponseResult; | 10 | import com.prws.common.bean.ResponseResult; |
11 | import com.prws.common.bean.ScheduleBean; | 11 | import com.prws.common.bean.ScheduleBean; |
12 | import com.prws.common.bean.Student; | 12 | import com.prws.common.bean.Student; |
13 | import com.prws.common.bean.Teacher; | 13 | import com.prws.common.bean.Teacher; |
14 | import com.prws.common.bean.TopicBean; | 14 | import com.prws.common.bean.TopicBean; |
15 | import com.prws.common.bean.UpdateBean; | 15 | import com.prws.common.bean.UpdateBean; |
16 | import com.prws.common.bean.User; | 16 | import com.prws.common.bean.User; |
17 | import com.prws.common.bean.baidu.BaiduInput; | 17 | import com.prws.common.bean.baidu.BaiduInput; |
18 | import com.prws.common.bean.homework.HomeWork; | 18 | import com.prws.common.bean.homework.HomeWork; |
19 | import com.prws.common.bean.homework.HomeworkDetail; | 19 | import com.prws.common.bean.homework.HomeworkDetail; |
20 | import com.prws.common.bean.homework.HomeworkList; | 20 | import com.prws.common.bean.homework.HomeworkList; |
21 | import com.prws.common.bean.homework.StDetail; | 21 | import com.prws.common.bean.homework.StDetail; |
22 | import com.prws.common.utils.BitmapUtils; | 22 | import com.prws.common.utils.BitmapUtils; |
23 | import com.prws.common.utils.SharedPreferencesUtil; | 23 | import com.prws.common.utils.SharedPreferencesUtil; |
24 | 24 | ||
25 | import java.io.File; | 25 | import java.io.File; |
26 | import java.util.HashMap; | 26 | import java.util.HashMap; |
27 | import java.util.List; | 27 | import java.util.List; |
28 | import java.util.Map; | 28 | import java.util.Map; |
29 | 29 | ||
30 | import io.reactivex.Observable; | 30 | import io.reactivex.Observable; |
31 | import io.reactivex.Observer; | 31 | import io.reactivex.Observer; |
32 | import io.reactivex.Single; | 32 | import io.reactivex.Single; |
33 | import io.reactivex.android.schedulers.AndroidSchedulers; | 33 | import io.reactivex.android.schedulers.AndroidSchedulers; |
34 | import io.reactivex.schedulers.Schedulers; | 34 | import io.reactivex.schedulers.Schedulers; |
35 | import okhttp3.MediaType; | 35 | import okhttp3.MediaType; |
36 | import okhttp3.MultipartBody; | 36 | import okhttp3.MultipartBody; |
37 | import okhttp3.RequestBody; | 37 | import okhttp3.RequestBody; |
38 | import okhttp3.ResponseBody; | 38 | import okhttp3.ResponseBody; |
39 | import retrofit2.Call; | 39 | import retrofit2.Call; |
40 | import retrofit2.Callback; | 40 | import retrofit2.Callback; |
41 | import retrofit2.Retrofit; | ||
41 | import retrofit2.http.Body; | 42 | import retrofit2.http.Body; |
42 | import retrofit2.http.GET; | 43 | import retrofit2.http.GET; |
43 | import retrofit2.http.Header; | 44 | import retrofit2.http.Header; |
44 | import retrofit2.http.Headers; | 45 | import retrofit2.http.Headers; |
45 | import retrofit2.http.Multipart; | 46 | import retrofit2.http.Multipart; |
46 | import retrofit2.http.POST; | 47 | import retrofit2.http.POST; |
47 | import retrofit2.http.PUT; | 48 | import retrofit2.http.PUT; |
48 | import retrofit2.http.Part; | 49 | import retrofit2.http.Part; |
49 | import retrofit2.http.PartMap; | 50 | import retrofit2.http.PartMap; |
50 | import retrofit2.http.Query; | 51 | import retrofit2.http.Query; |
51 | import retrofit2.http.Url; | 52 | import retrofit2.http.Url; |
52 | 53 | ||
53 | /** | 54 | /** |
54 | * 类名称:NetWorks | 55 | * 类名称:NetWorks |
55 | * 创建人: | 56 | * 创建人: |
56 | * <p> | 57 | * <p> |
57 | * 类描述:网络请求的操作类 | 58 | * 类描述:网络请求的操作类 |
58 | */ | 59 | */ |
59 | public class NetWorks extends RetrofitUtils { | 60 | public class NetWorks extends RetrofitUtils { |
60 | //服务器路径 | 61 | //服务器路径 |
61 | public static final NetService service_url = getMachineRetrofit(BuildConfig.SERVER_URL).create(NetService.class); | 62 | public static final Retrofit retrofit = getMachineRetrofit(BuildConfig.SERVER_URL); |
63 | public static final NetService service_url = retrofit.create(NetService.class); | ||
62 | 64 | ||
63 | //设缓存有效期为1天 | 65 | //设缓存有效期为1天 |
64 | protected static final long CACHE_STALE_SEC = 60 * 60 * 24 * 1; | 66 | protected static final long CACHE_STALE_SEC = 60 * 60 * 24 * 1; |
65 | //查询缓存的Cache-Control设置,使用缓存 | 67 | //查询缓存的Cache-Control设置,使用缓存 |
66 | protected static final String CACHE_CONTROL_CACHE = "only-if-cached, max-stale=" + CACHE_STALE_SEC; | 68 | protected static final String CACHE_CONTROL_CACHE = "only-if-cached, max-stale=" + CACHE_STALE_SEC; |
67 | //查询网络的Cache-Control设置。不使用缓存 | 69 | //查询网络的Cache-Control设置。不使用缓存 |
68 | protected static final String CACHE_CONTROL_NETWORK = "max-age=0"; | 70 | protected static final String CACHE_CONTROL_NETWORK = "max-age=0"; |
69 | 71 | ||
70 | 72 | ||
71 | public interface NetService { | 73 | public interface NetService { |
72 | 74 | ||
73 | 75 | ||
74 | @GET("/api/v1/user/logout") | 76 | @GET("/api/v1/user/logout") |
75 | Observable<ResponseBody> logout(); | 77 | Observable<ResponseBody> logout(); |
76 | 78 | ||
77 | @Multipart | 79 | @Multipart |
78 | @POST("/api/v1/user/upLoadAvatar") | 80 | @POST("/api/v1/user/upLoadAvatar") |
79 | Observable<ResponseBody> upLoadAvatar(@Header("Authorization") String token, @Part List<MultipartBody.Part> partLis); | 81 | Observable<ResponseBody> upLoadAvatar(@Header("Authorization") String token, @Part List<MultipartBody.Part> partLis); |
80 | 82 | ||
81 | 83 | ||
82 | @Headers("Content-Type: application/json") | 84 | @Headers("Content-Type: application/json") |
83 | @POST("/api/v1/user/editUser") | 85 | @POST("/api/v1/user/editUser") |
84 | Observable<ResponseBody> editUser(@Header("Authorization") String token, @Body RequestBody body); | 86 | Observable<ResponseBody> editUser(@Header("Authorization") String token, @Body RequestBody body); |
85 | 87 | ||
86 | @Headers("Content-Type: application/json") | 88 | @Headers("Content-Type: application/json") |
87 | @POST("/api/v1/user/changePassword") | 89 | @POST("/api/v1/user/changePassword") |
88 | Observable<ResponseBody> changePassword(@Header("Authorization") String token, @Body RequestBody body); | 90 | Observable<ResponseBody> changePassword(@Header("Authorization") String token, @Body RequestBody body); |
89 | 91 | ||
90 | 92 | ||
91 | @GET("/api/v1/user/searchById") | 93 | @GET("/api/v1/user/searchById") |
92 | Observable<ResponseBody> searchById(@Header("Authorization") String token, @Query("userId") String userId); | 94 | Observable<ResponseBody> searchById(@Header("Authorization") String token, @Query("userId") String userId); |
93 | 95 | ||
94 | 96 | ||
95 | @Headers("Content-Type: application/json") | 97 | @Headers("Content-Type: application/json") |
96 | @POST("/api/v1/login/userLogin") | 98 | @POST("/api/v1/login/userLogin") |
97 | Observable<ResponseBody> login(@Body RequestBody body); | 99 | Observable<ResponseBody> login(@Body RequestBody body); |
98 | 100 | ||
99 | @GET("/api/v1/resource/listGradeAndSubject") | 101 | @GET("/api/v1/resource/listGradeAndSubject") |
100 | Observable<ResponseResult<List<GradeAndSubject>>> listGradeAndSubject(@Header("Authorization") String token); | 102 | Observable<ResponseResult<List<GradeAndSubject>>> listGradeAndSubject(@Header("Authorization") String token); |
101 | 103 | ||
102 | 104 | ||
103 | @GET("/api/v1/manager/generalQrCode") | 105 | @GET("/api/v1/manager/generalQrCode") |
104 | Observable<ResponseBody> generalQrCode(); | 106 | Observable<ResponseBody> generalQrCode(); |
105 | 107 | ||
106 | @GET("/api/v1/parent/scanAndLogin") | 108 | @GET("/api/v1/parent/scanAndLogin") |
107 | Observable<ResponseBody> scanAndLogin(@Header("Authorization") String token, @Query("code") String code, @Query("stuId") String stuId); | 109 | Observable<ResponseBody> scanAndLogin(@Header("Authorization") String token, @Query("code") String code, @Query("stuId") String stuId); |
108 | 110 | ||
109 | @GET("/api/v1/parent/getChildrenList") | 111 | @GET("/api/v1/parent/getChildrenList") |
110 | Observable<ResponseBody> getChildrenList(@Header("Authorization") String token); | 112 | Observable<ResponseBody> getChildrenList(@Header("Authorization") String token); |
111 | 113 | ||
112 | 114 | ||
113 | @Headers("Content-Type: application/json") | 115 | @Headers("Content-Type: application/json") |
114 | @POST("/api/v1/parent/registerParent") | 116 | @POST("/api/v1/parent/registerParent") |
115 | Observable<ResponseBody> registerParent(@Body RequestBody body); | 117 | Observable<ResponseBody> registerParent(@Body RequestBody body); |
116 | 118 | ||
117 | 119 | ||
118 | @GET("/api/v1/parent/listChildren") | 120 | @GET("/api/v1/parent/listChildren") |
119 | Observable<ResponseBody> listChildren(@Header("Authorization") String token); | 121 | Observable<ResponseBody> listChildren(@Header("Authorization") String token); |
120 | 122 | ||
121 | 123 | ||
122 | @Headers("Content-Type: application/json") | 124 | @Headers("Content-Type: application/json") |
123 | @POST("/api/v1/parent/registerStudent") | 125 | @POST("/api/v1/parent/registerStudent") |
124 | Observable<ResponseBody> registerStudent(@Header("Authorization") String token, @Body RequestBody body); | 126 | Observable<ResponseBody> registerStudent(@Header("Authorization") String token, @Body RequestBody body); |
125 | 127 | ||
126 | @Headers("Content-Type: application/json") | 128 | @Headers("Content-Type: application/json") |
127 | @POST("/api/v1/parent/bindTeacher") | 129 | @POST("/api/v1/parent/bindTeacher") |
128 | Observable<ResponseBody> bindTeacher(@Header("Authorization") String token, @Body RequestBody body); | 130 | Observable<ResponseBody> bindTeacher(@Header("Authorization") String token, @Body RequestBody body); |
129 | 131 | ||
130 | @Multipart | 132 | @Multipart |
131 | @POST("/api/v1/user/upLoadAvatar") | 133 | @POST("/api/v1/user/upLoadAvatar") |
132 | Observable<ResponseResult<Map<String, String>>> uploadAvatar(@Header("Authorization") String token, @Part() MultipartBody.Part file); | 134 | Observable<ResponseResult<Map<String, String>>> uploadAvatar(@Header("Authorization") String token, @Part() MultipartBody.Part file); |
133 | 135 | ||
134 | 136 | ||
135 | @Multipart | 137 | @Multipart |
136 | @POST("/api/v1/student/editStudentAvatar") | 138 | @POST("/api/v1/student/editStudentAvatar") |
137 | Observable<ResponseResult<Map<String, String>>> uploadAvatar(@Header("Authorization") String token, @Part() MultipartBody.Part file, @PartMap Map<String, Object> map); | 139 | Observable<ResponseResult<Map<String, String>>> uploadAvatar(@Header("Authorization") String token, @Part() MultipartBody.Part file, @PartMap Map<String, Object> map); |
138 | 140 | ||
139 | @Headers("Content-Type: application/json") | 141 | @Headers("Content-Type: application/json") |
140 | @POST("/api/v1/parent/editChild") | 142 | @POST("/api/v1/parent/editChild") |
141 | Observable<ResponseResult> editStudent(@Header("Authorization") String token, @Body RequestBody body); | 143 | Observable<ResponseResult> editStudent(@Header("Authorization") String token, @Body RequestBody body); |
142 | 144 | ||
143 | @GET("/api/v1/student/getStudyPlanForThisWeek") | 145 | @GET("/api/v1/student/getStudyPlanForThisWeek") |
144 | Observable<ResponseResult<ScheduleBean>> getWeekPlan(@Header("Authorization") String token, @Query("stuId") String stuId); | 146 | Observable<ResponseResult<ScheduleBean>> getWeekPlan(@Header("Authorization") String token, @Query("stuId") String stuId); |
145 | 147 | ||
146 | @GET("api/v1/parent/searchTeacher") | 148 | @GET("api/v1/parent/searchTeacher") |
147 | Observable<ResponseResult<Teacher>> searchTeacher(@Header("Authorization") String token, @Query("phone") String phone); | 149 | Observable<ResponseResult<Teacher>> searchTeacher(@Header("Authorization") String token, @Query("phone") String phone); |
148 | 150 | ||
149 | @POST("api/v1/question/listErrorBook") | 151 | @POST("api/v1/question/listErrorBook") |
150 | Observable<ResponseResult<PageInfo<TopicBean>>> getError(@Header("Authorization") String token, @Body Map<String, Object> body); | 152 | Observable<ResponseResult<PageInfo<TopicBean>>> getError(@Header("Authorization") String token, @Body Map<String, Object> body); |
151 | 153 | ||
152 | @POST | 154 | @POST |
153 | Observable<JsonObject> removeWriting(@Url String url, @Body RequestBody body); | 155 | Observable<JsonObject> removeWriting(@Url String url, @Body RequestBody body); |
154 | 156 | ||
155 | @POST | 157 | @POST |
156 | Observable<CutPicBean> cut(@Url String url, @Body RequestBody body); | 158 | Observable<CutPicBean> cut(@Url String url, @Body RequestBody body); |
157 | 159 | ||
158 | @POST | 160 | @POST |
159 | Observable<JsonObject> getBaiduToken(@Url String url); | 161 | Observable<JsonObject> getBaiduToken(@Url String url); |
160 | 162 | ||
161 | @Multipart | 163 | @Multipart |
162 | @POST("api/v1/pad/addErrorBook") | 164 | @POST("api/v1/pad/addErrorBook") |
163 | Observable<ResponseResult> addError(@Part() MultipartBody.Part file, @PartMap Map<String, Object> map); | 165 | Observable<ResponseResult> addError(@Part() MultipartBody.Part file, @PartMap Map<String, Object> map); |
164 | 166 | ||
165 | @POST("api/v1/pad/deleteStuErrorBook") | 167 | @POST("api/v1/pad/deleteStuErrorBook") |
166 | Observable<ResponseResult> deleteError(@Header("Authorization") String token, @Body List<String> map); | 168 | Observable<ResponseResult> deleteError(@Header("Authorization") String token, @Body List<String> map); |
167 | 169 | ||
168 | @PUT("api/v1/pad/updateStuErrorBookInfo") | 170 | @PUT("api/v1/pad/updateStuErrorBookInfo") |
169 | Observable<ResponseResult> updateError(@Header("Authorization") String token, @Body List<HashMap<String, Object>> map); | 171 | Observable<ResponseResult> updateError(@Header("Authorization") String token, @Body List<HashMap<String, Object>> map); |
170 | 172 | ||
171 | @POST("api/v1/question/editErrorBook") | 173 | @POST("api/v1/question/editErrorBook") |
172 | Observable<ResponseResult> editError(@Header("Authorization") String Authorization, @Body Map<String, Object> map); | 174 | Observable<ResponseResult> editError(@Header("Authorization") String Authorization, @Body Map<String, Object> map); |
173 | 175 | ||
174 | @GET("api/v1/resource/checkUpdate") | 176 | @GET("api/v1/resource/checkUpdate") |
175 | Call<ResponseResult<UpdateBean>> checkUpdate(@Query("version") int version, @Query("packageName") String packageName, @Query("type") int type); | 177 | Call<ResponseResult<UpdateBean>> checkUpdate(@Query("version") int version, @Query("packageName") String packageName, @Query("type") int type); |
176 | 178 | ||
177 | @GET("api/v1/teacher/getStudentList") | 179 | @GET("api/v1/teacher/getStudentList") |
178 | Observable<ResponseBody> getStudentList(@Header("Authorization") String token, @Query("userId") String id); | 180 | Observable<ResponseBody> getStudentList(@Header("Authorization") String token, @Query("userId") String id); |
179 | 181 | ||
180 | @GET("api/v1/teacher/getStudentList") | 182 | @GET("api/v1/teacher/getStudentList") |
181 | Single<ResponseResult<List<Student>>> getStudentList2(@Header("Authorization") String token, @Query("userId") String id); | 183 | Single<ResponseResult<List<Student>>> getStudentList2(@Header("Authorization") String token, @Query("userId") String id); |
182 | 184 | ||
183 | @GET("api/v1/answer/listRecordForTeacher") | 185 | @GET("api/v1/answer/listRecordForTeacher") |
184 | Observable<ResponseBody> getRecordList(@Header("Authorization") String token, @Query("userId") String id); | 186 | Observable<ResponseBody> getRecordList(@Header("Authorization") String token, @Query("userId") String id); |
185 | 187 | ||
186 | @GET("api/v1/homework/listHomeworkByStuId") | 188 | @GET("api/v1/homework/listHomeworkByStuId") |
187 | Single<ResponseResult<List<HomeworkList>>> getStudentHomework(@Header("Authorization") String token, @Query("stuId") String stuId); | 189 | Single<ResponseResult<List<HomeworkList>>> getStudentHomework(@Header("Authorization") String token, @Query("stuId") String stuId); |
188 | 190 | ||
189 | @POST | 191 | @POST |
190 | Single<BaiduInput> inputImage(@Url String url, @Body RequestBody body); | 192 | Single<BaiduInput> inputImage(@Url String url, @Body RequestBody body); |
191 | 193 | ||
192 | @Multipart | 194 | @Multipart |
193 | @POST("api/v1/homework/uploadHomework") | 195 | @POST("api/v1/homework/uploadHomework") |
194 | Single<ResponseResult> uploadImage(@Header("Authorization") String token, @Part() MultipartBody.Part file, @Query("brief") String id); | 196 | Single<ResponseResult> uploadImage(@Header("Authorization") String token, @Part() MultipartBody.Part file, @Query("brief") String id); |
195 | 197 | ||
196 | @POST("api/v1/homework/uploadHomeworkAction") | 198 | @POST("api/v1/homework/uploadHomeworkAction") |
197 | Single<ResponseResult> uploadHomework(@Header("Authorization") String token, @Body Object map); | 199 | Single<ResponseResult> uploadHomework(@Header("Authorization") String token, @Body Object map); |
198 | 200 | ||
199 | @GET("api/v1/homework/removeHomework") | 201 | @GET("api/v1/homework/removeHomework") |
200 | Single<ResponseResult<Boolean>> deleteHomework( | 202 | Single<ResponseResult<Boolean>> deleteHomework( |
201 | @Header("Authorization") String token, | 203 | @Header("Authorization") String token, |
202 | @Query("homeworkId") String homeworkId | 204 | @Query("homeworkId") String homeworkId |
203 | ); | 205 | ); |
204 | 206 | ||
205 | @GET("api/v1/homework/listHomeworkById") | 207 | @GET("api/v1/homework/listHomeworkById") |
206 | Single<ResponseResult<List<HomeWork>>> getHomeworkDetail(@Header("Authorization") String token, @Query("homeworkId") String homeworkId); | 208 | Single<ResponseResult<List<HomeWork>>> getHomeworkDetail(@Header("Authorization") String token, @Query("homeworkId") String homeworkId); |
207 | 209 | ||
208 | @POST("api/v1/homework/uploadHomeworkFeedback") | 210 | @POST("api/v1/homework/uploadHomeworkFeedback") |
209 | Single<ResponseResult> uploadHomeworkFeedback(@Header("Authorization") String token, @Body Map<String, Object> map); | 211 | Single<ResponseResult> uploadHomeworkFeedback(@Header("Authorization") String token, @Body Map<String, Object> map); |
210 | 212 | ||
211 | @POST("api/v1/homework/editHomeworkInfo") | 213 | @POST("api/v1/homework/editHomeworkInfo") |
212 | Single<ResponseResult<Boolean>> editHomework( | 214 | Single<ResponseResult<Boolean>> editHomework( |
213 | @Header("Authorization") String token, | 215 | @Header("Authorization") String token, |
214 | @Body Object body | 216 | @Body Object body |
215 | ); | 217 | ); |
216 | 218 | ||
217 | @GET("api/v1/homework/listHomeworkDetailByStuId") | 219 | @GET("api/v1/homework/listHomeworkDetailByStuId") |
218 | Single<ResponseResult<HomeworkDetail>> getHomeworkCompleteDetail( | 220 | Single<ResponseResult<HomeworkDetail>> getHomeworkCompleteDetail( |
219 | @Header("Authorization") String token, | 221 | @Header("Authorization") String token, |
220 | @Query("stuId") String stuId, | 222 | @Query("stuId") String stuId, |
221 | @Query("homeworkId") String homeworkId, | 223 | @Query("homeworkId") String homeworkId, |
222 | // type 固定传true | 224 | // type 固定传true |
223 | @Query("type") boolean type | 225 | @Query("type") boolean type |
224 | ); | 226 | ); |
225 | 227 | ||
226 | @GET("api/v1/homework/listHomeworkStatistics") | 228 | @GET("api/v1/homework/listHomeworkStatistics") |
227 | Single<ResponseResult<List<StDetail>>> getHuyouList( | 229 | Single<ResponseResult<List<StDetail>>> getHuyouList( |
228 | @Header("Authorization") String token, | 230 | @Header("Authorization") String token, |
229 | @Query("stuId") String stuId, | 231 | @Query("stuId") String stuId, |
230 | @Query("type") int type //0: 周报, 1:阶段总结 | 232 | @Query("type") int type //0: 周报, 1:阶段总结 |
231 | ); | 233 | ); |
232 | 234 | ||
233 | @GET("api/v1/homework/listHomeworkStatisticsDetailForStudent") | 235 | @GET("api/v1/homework/listHomeworkStatisticsDetailForStudent") |
234 | Single<ResponseResult<StDetail>> getHuyouDetail( | 236 | Single<ResponseResult<StDetail>> getHuyouDetail( |
235 | @Header("Authorization") String token, | 237 | @Header("Authorization") String token, |
236 | @Query("homeworkStatisticsId") String homeworkId | 238 | @Query("homeworkStatisticsId") String homeworkId |
237 | ); | 239 | ); |
238 | 240 | ||
239 | @GET("api/v1/demo/generalStatisticsHomework") | 241 | @GET("api/v1/demo/generalStatisticsHomework") |
240 | Single<ResponseResult<Object>> generalHuyou(); | 242 | Single<ResponseResult<Object>> generalHuyou(); |
241 | 243 | ||
242 | @POST("api/v1/homework/generalHomeworkStageStatistics") | 244 | @POST("api/v1/homework/generalHomeworkStageStatistics") |
243 | Single<ResponseResult<Boolean>> generalStageHuyou( | 245 | Single<ResponseResult<Boolean>> generalStageHuyou( |
244 | @Header("Authorization") String token, | 246 | @Header("Authorization") String token, |
245 | @Body Object body | 247 | @Body Object body |
246 | ); | 248 | ); |
247 | 249 | ||
248 | @GET("api/v1/homework/removeHomeworkStatistics") | 250 | @GET("api/v1/homework/removeHomeworkStatistics") |
249 | Single<ResponseResult<Boolean>> deleteHuyou( | 251 | Single<ResponseResult<Boolean>> deleteHuyou( |
250 | @Header("Authorization") String token, | 252 | @Header("Authorization") String token, |
251 | @Query("homeworkStatisticsId") String homeworkId | 253 | @Query("homeworkStatisticsId") String homeworkId |
252 | ); | 254 | ); |
253 | 255 | ||
254 | @GET("api/v1/login/smsCode") | 256 | @GET("api/v1/login/smsCode") |
255 | Single<ResponseResult<String>> smsCode(@Query("mobile") String mobile); | 257 | Single<ResponseResult<String>> smsCode(@Query("mobile") String mobile); |
256 | 258 | ||
257 | @POST("api/v1/login/smsLogin") | 259 | @POST("api/v1/login/smsLogin") |
258 | Single<ResponseResult<User>> smsLogin(@Body Object body); | 260 | Single<ResponseResult<User>> smsLogin(@Body Object body); |
259 | 261 | ||
260 | } | 262 | } |
261 | 263 | ||
262 | public static String getUserId() { | 264 | public static String getUserId() { |
263 | return (String) SharedPreferencesUtil.getData("userId", ""); | 265 | return (String) SharedPreferencesUtil.getData("userId", ""); |
264 | } | 266 | } |
265 | 267 | ||
266 | public static String getHeader() { | 268 | public static String getHeader() { |
267 | return (String) SharedPreferencesUtil.getData("token", ""); | 269 | return (String) SharedPreferencesUtil.getData("token", ""); |
268 | } | 270 | } |
269 | 271 | ||
270 | public static String getBaiduToken() { | 272 | public static String getBaiduToken() { |
271 | return (String) SharedPreferencesUtil.getData("baiduToken", ""); | 273 | return (String) SharedPreferencesUtil.getData("baiduToken", ""); |
272 | } | 274 | } |
273 | 275 | ||
274 | public static Single<BaiduInput> inputImage(String filePath, String id) { | 276 | public static Single<BaiduInput> inputImage(String filePath, String id) { |
275 | MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded"); | 277 | MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded"); |
276 | String base64 = BitmapUtils.fileToBase64(filePath); | 278 | String base64 = BitmapUtils.fileToBase64(filePath); |
277 | File file = new File(filePath); | 279 | File file = new File(filePath); |
278 | RequestBody body = RequestBody.create(mediaType, "image=" + base64 + "&brief={\"name\":\"" + file.getName() + "\", \"id\":\"" + id + "\"}"); | 280 | RequestBody body = RequestBody.create(mediaType, "image=" + base64 + "&brief={\"name\":\"" + file.getName() + "\", \"id\":\"" + id + "\"}"); |
279 | return getBaiduTokenOcr().map(jsonObject -> jsonObject.get("access_token").getAsString()) | 281 | return getBaiduTokenOcr().map(jsonObject -> jsonObject.get("access_token").getAsString()) |
280 | .flatMap(token -> { | 282 | .flatMap(token -> { |
281 | return service_url.inputImage("https://aip.baidubce.com/rest/2.0/realtime_search/same_hq/add?access_token=" + token, body); | 283 | return service_url.inputImage("https://aip.baidubce.com/rest/2.0/realtime_search/same_hq/add?access_token=" + token, body); |
282 | }); | 284 | }); |
283 | } | 285 | } |
284 | 286 | ||
285 | public static Single<ResponseResult> uploadImage(String path, String id) { | 287 | public static Single<ResponseResult> uploadImage(String path, String id) { |
286 | File file = new File(path); | 288 | File file = new File(path); |
287 | RequestBody requestBody = RequestBody.create(MediaType.parse("image/jpg"), file); | 289 | RequestBody requestBody = RequestBody.create(MediaType.parse("image/jpg"), file); |
288 | MultipartBody.Part part = MultipartBody.Part.createFormData("file", file.getName(), requestBody); | 290 | MultipartBody.Part part = MultipartBody.Part.createFormData("file", file.getName(), requestBody); |
289 | return service_url.uploadImage(getHeader(), part, id); | 291 | return service_url.uploadImage(getHeader(), part, id); |
290 | } | 292 | } |
291 | 293 | ||
292 | 294 | ||
293 | public static void checkUpdate(int code, String packageName, Callback<ResponseResult<UpdateBean>> callback) { | 295 | public static void checkUpdate(int code, String packageName, Callback<ResponseResult<UpdateBean>> callback) { |
294 | service_url.checkUpdate(code, packageName, 0).enqueue(callback); | 296 | service_url.checkUpdate(code, packageName, 0).enqueue(callback); |
295 | } | 297 | } |
296 | 298 | ||
297 | public static void addError(String path, Map<String, String> param, Observer<ResponseResult> observer) { | 299 | public static void addError(String path, Map<String, String> param, Observer<ResponseResult> observer) { |
298 | File file = new File(path); | 300 | File file = new File(path); |
299 | RequestBody requestBody = RequestBody.create(MediaType.parse("image/jpg"), file); | 301 | RequestBody requestBody = RequestBody.create(MediaType.parse("image/jpg"), file); |
300 | MultipartBody.Part part = MultipartBody.Part.createFormData("file", file.getName(), requestBody); | 302 | MultipartBody.Part part = MultipartBody.Part.createFormData("file", file.getName(), requestBody); |
301 | Map<String, Object> map = new HashMap<>(); | 303 | Map<String, Object> map = new HashMap<>(); |
302 | map.put("condition", param); | 304 | map.put("condition", param); |
303 | setSubscribe(service_url.addError(part, map), observer); | 305 | setSubscribe(service_url.addError(part, map), observer); |
304 | } | 306 | } |
305 | 307 | ||
306 | 308 | ||
307 | public static void cut(String base64, Observer<CutPicBean> observer) { | 309 | public static void cut(String base64, Observer<CutPicBean> observer) { |
308 | MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded"); | 310 | MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded"); |
309 | RequestBody body = RequestBody.create(mediaType, "image=" + base64 + "&detect_direction=true"); | 311 | RequestBody body = RequestBody.create(mediaType, "image=" + base64 + "&detect_direction=true"); |
310 | setSubscribe(service_url.cut("https://aip.baidubce.com/rest/2.0/ocr/v1/paper_cut_edu?access_token=" + getBaiduToken(), body), observer); | 312 | setSubscribe(service_url.cut("https://aip.baidubce.com/rest/2.0/ocr/v1/paper_cut_edu?access_token=" + getBaiduToken(), body), observer); |
311 | } | 313 | } |
312 | 314 | ||
313 | public static void editError(Map<String, Object> map, Observer<ResponseResult> observer) { | 315 | public static void editError(Map<String, Object> map, Observer<ResponseResult> observer) { |
314 | setSubscribe(service_url.editError(getHeader(), map), observer); | 316 | setSubscribe(service_url.editError(getHeader(), map), observer); |
315 | } | 317 | } |
316 | 318 | ||
317 | public static void editError(List<HashMap<String, Object>> map, Observer<ResponseResult> observer) { | 319 | public static void editError(List<HashMap<String, Object>> map, Observer<ResponseResult> observer) { |
318 | setSubscribe(service_url.updateError(getHeader(), map), observer); | 320 | setSubscribe(service_url.updateError(getHeader(), map), observer); |
319 | } | 321 | } |
320 | 322 | ||
321 | public static void deleteError(List<String> map, Observer<ResponseResult> observer) { | 323 | public static void deleteError(List<String> map, Observer<ResponseResult> observer) { |
322 | setSubscribe(service_url.deleteError(getHeader(), map), observer); | 324 | setSubscribe(service_url.deleteError(getHeader(), map), observer); |
323 | } | 325 | } |
324 | 326 | ||
325 | public static void getBaiduToken(Observer<JsonObject> observer) { | 327 | public static void getBaiduToken(Observer<JsonObject> observer) { |
326 | setSubscribe(service_url.getBaiduToken("https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=" + BuildConfig.APIKey + "&client_secret=" + BuildConfig.SecretKey), observer); | 328 | setSubscribe(service_url.getBaiduToken("https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=" + BuildConfig.APIKey + "&client_secret=" + BuildConfig.SecretKey), observer); |
327 | } | 329 | } |
328 | 330 | ||
329 | public static Single<JsonObject> getBaiduTokenOcr() { | 331 | public static Single<JsonObject> getBaiduTokenOcr() { |
330 | return service_url.getBaiduToken("https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=" + BuildConfig.APIKey1 + "&client_secret=" + BuildConfig.SecretKey1).firstOrError(); | 332 | return service_url.getBaiduToken("https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=" + BuildConfig.APIKey1 + "&client_secret=" + BuildConfig.SecretKey1).firstOrError(); |
331 | } | 333 | } |
332 | 334 | ||
333 | public static void removeWriting(String base64, Observer<JsonObject> observer) { | 335 | public static void removeWriting(String base64, Observer<JsonObject> observer) { |
334 | MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded"); | 336 | MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded"); |
335 | RequestBody body = RequestBody.create(mediaType, "image=" + base64); | 337 | RequestBody body = RequestBody.create(mediaType, "image=" + base64); |
336 | setSubscribe(service_url.removeWriting(("https://aip.baidubce.com/rest/2.0/ocr/v1/remove_handwriting?access_token=" + getBaiduToken()), body), observer); | 338 | setSubscribe(service_url.removeWriting(("https://aip.baidubce.com/rest/2.0/ocr/v1/remove_handwriting?access_token=" + getBaiduToken()), body), observer); |
337 | } | 339 | } |
338 | 340 | ||
339 | public static void searchTeacher(String phone, Observer<ResponseResult<Teacher>> observer) { | 341 | public static void searchTeacher(String phone, Observer<ResponseResult<Teacher>> observer) { |
340 | setSubscribe(service_url.searchTeacher(getHeader(), phone), observer); | 342 | setSubscribe(service_url.searchTeacher(getHeader(), phone), observer); |
341 | } | 343 | } |
342 | 344 | ||
343 | public static void logout(Observer<ResponseBody> observer) { | 345 | public static void logout(Observer<ResponseBody> observer) { |
344 | setSubscribe(service_url.logout(), observer); | 346 | setSubscribe(service_url.logout(), observer); |
345 | } | 347 | } |
346 | 348 | ||
347 | public static void editStudent(RequestBody body, Observer<ResponseResult> observable) { | 349 | public static void editStudent(RequestBody body, Observer<ResponseResult> observable) { |
348 | setSubscribe(service_url.editStudent(getHeader(), body), observable); | 350 | setSubscribe(service_url.editStudent(getHeader(), body), observable); |
349 | } | 351 | } |
350 | 352 | ||
351 | public static void getWeekPlan(String id, Observer<ResponseResult<ScheduleBean>> observer) { | 353 | public static void getWeekPlan(String id, Observer<ResponseResult<ScheduleBean>> observer) { |
352 | setSubscribe(service_url.getWeekPlan(getHeader(), id), observer); | 354 | setSubscribe(service_url.getWeekPlan(getHeader(), id), observer); |
353 | } | 355 | } |
354 | 356 | ||
355 | public static void uploadStudentAvatar(File file, String stuId, Observer<ResponseResult<Map<String, String>>> observer) { | 357 | public static void uploadStudentAvatar(File file, String stuId, Observer<ResponseResult<Map<String, String>>> observer) { |
356 | RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file); | 358 | RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file); |
357 | MultipartBody.Part part = MultipartBody.Part.createFormData("file", file.getName(), requestBody); | 359 | MultipartBody.Part part = MultipartBody.Part.createFormData("file", file.getName(), requestBody); |
358 | Map<String, Object> map = new HashMap<>(); | 360 | Map<String, Object> map = new HashMap<>(); |
359 | map.put("stuId", stuId); | 361 | map.put("stuId", stuId); |
360 | setSubscribe(service_url.uploadAvatar(getHeader(), part, map), observer); | 362 | setSubscribe(service_url.uploadAvatar(getHeader(), part, map), observer); |
361 | } | 363 | } |
362 | 364 | ||
363 | public static void uploadAvatar(File file, Observer<ResponseResult<Map<String, String>>> observer) { | 365 | public static void uploadAvatar(File file, Observer<ResponseResult<Map<String, String>>> observer) { |
364 | RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file); | 366 | RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file); |
365 | MultipartBody.Part part = MultipartBody.Part.createFormData("file", file.getName(), requestBody); | 367 | MultipartBody.Part part = MultipartBody.Part.createFormData("file", file.getName(), requestBody); |
366 | setSubscribe(service_url.uploadAvatar(getHeader(), part), observer); | 368 | setSubscribe(service_url.uploadAvatar(getHeader(), part), observer); |
367 | } | 369 | } |
368 | 370 | ||
369 | public static void upLoadAvatar(List<MultipartBody.Part> partLis, Observer<ResponseBody> observer) { | 371 | public static void upLoadAvatar(List<MultipartBody.Part> partLis, Observer<ResponseBody> observer) { |
370 | setSubscribe(service_url.upLoadAvatar(getHeader(), partLis), observer); | 372 | setSubscribe(service_url.upLoadAvatar(getHeader(), partLis), observer); |
371 | } | 373 | } |
372 | 374 | ||
373 | 375 | ||
374 | public static void editUser(RequestBody body, Observer<ResponseBody> observer) { | 376 | public static void editUser(RequestBody body, Observer<ResponseBody> observer) { |
375 | setSubscribe(service_url.editUser(getHeader(), body), observer); | 377 | setSubscribe(service_url.editUser(getHeader(), body), observer); |
376 | } | 378 | } |
377 | 379 | ||
378 | public static void changePassword(RequestBody body, Observer<ResponseBody> observer) { | 380 | public static void changePassword(RequestBody body, Observer<ResponseBody> observer) { |
379 | setSubscribe(service_url.changePassword(getHeader(), body), observer); | 381 | setSubscribe(service_url.changePassword(getHeader(), body), observer); |
380 | } | 382 | } |
381 | 383 | ||
382 | 384 | ||
383 | public static void searchById(String userId, Observer<ResponseBody> observer) { | 385 | public static void searchById(String userId, Observer<ResponseBody> observer) { |
384 | setSubscribe(service_url.searchById(getHeader(), userId), observer); | 386 | setSubscribe(service_url.searchById(getHeader(), userId), observer); |
385 | } | 387 | } |
386 | 388 | ||
387 | 389 | ||
388 | public static void login(RequestBody body, Observer<ResponseBody> observer) { | 390 | public static void login(RequestBody body, Observer<ResponseBody> observer) { |
389 | setSubscribe(service_url.login(body), observer); | 391 | setSubscribe(service_url.login(body), observer); |
390 | } | 392 | } |
391 | 393 | ||
392 | public static void listGradeAndSubject(Observer<ResponseResult<List<GradeAndSubject>>> observer) { | 394 | public static void listGradeAndSubject(Observer<ResponseResult<List<GradeAndSubject>>> observer) { |
393 | setSubscribe(service_url.listGradeAndSubject(getHeader()), observer); | 395 | setSubscribe(service_url.listGradeAndSubject(getHeader()), observer); |
394 | } | 396 | } |
395 | 397 | ||
396 | 398 | ||
397 | public static void scanAndLogin(String code, String stuId, Observer<ResponseBody> observer) { | 399 | public static void scanAndLogin(String code, String stuId, Observer<ResponseBody> observer) { |
398 | setSubscribe(service_url.scanAndLogin(getHeader(), code, stuId), observer); | 400 | setSubscribe(service_url.scanAndLogin(getHeader(), code, stuId), observer); |
399 | } | 401 | } |
400 | 402 | ||
401 | public static void getChildrenList(Observer<ResponseBody> observer) { | 403 | public static void getChildrenList(Observer<ResponseBody> observer) { |
402 | setSubscribe(service_url.getChildrenList(getHeader()), observer); | 404 | setSubscribe(service_url.getChildrenList(getHeader()), observer); |
403 | } | 405 | } |
404 | 406 | ||
405 | 407 | ||
406 | public static void registerParent(RequestBody body, Observer<ResponseBody> observer) { | 408 | public static void registerParent(RequestBody body, Observer<ResponseBody> observer) { |
407 | setSubscribe(service_url.registerParent(body), observer); | 409 | setSubscribe(service_url.registerParent(body), observer); |
408 | } | 410 | } |
409 | 411 | ||
410 | 412 | ||
411 | public static void listChildren(Observer<ResponseBody> observer) { | 413 | public static void listChildren(Observer<ResponseBody> observer) { |
412 | setSubscribe(service_url.listChildren(getHeader()), observer); | 414 | setSubscribe(service_url.listChildren(getHeader()), observer); |
413 | } | 415 | } |
414 | 416 | ||
415 | public static void listStudent(Observer<ResponseBody> observer) { | 417 | public static void listStudent(Observer<ResponseBody> observer) { |
416 | setSubscribe(service_url.getStudentList(getHeader(), (String) SharedPreferencesUtil.getData("userId", "")), observer); | 418 | setSubscribe(service_url.getStudentList(getHeader(), (String) SharedPreferencesUtil.getData("userId", "")), observer); |
417 | } | 419 | } |
418 | 420 | ||
419 | public static Single<ResponseResult<List<Student>>> listStudent() { | 421 | public static Single<ResponseResult<List<Student>>> listStudent() { |
420 | return service_url.getStudentList2(getHeader(), getUserId()); | 422 | return service_url.getStudentList2(getHeader(), getUserId()); |
421 | } | 423 | } |
422 | 424 | ||
423 | public static void listRecord(Observer<ResponseBody> observer) { | 425 | public static void listRecord(Observer<ResponseBody> observer) { |
424 | setSubscribe(service_url.getRecordList(getHeader(), (String) SharedPreferencesUtil.getData("userId", "")), observer); | 426 | setSubscribe(service_url.getRecordList(getHeader(), (String) SharedPreferencesUtil.getData("userId", "")), observer); |
425 | } | 427 | } |
426 | 428 | ||
427 | 429 | ||
428 | public static void registerStudent(RequestBody body, Observer<ResponseBody> observer) { | 430 | public static void registerStudent(RequestBody body, Observer<ResponseBody> observer) { |
429 | setSubscribe(service_url.registerStudent(getHeader(), body), observer); | 431 | setSubscribe(service_url.registerStudent(getHeader(), body), observer); |
430 | } | 432 | } |
431 | 433 | ||
432 | public static void bindTeacher(RequestBody body, Observer<ResponseBody> observer) { | 434 | public static void bindTeacher(RequestBody body, Observer<ResponseBody> observer) { |
433 | setSubscribe(service_url.bindTeacher(getHeader(), body), observer); | 435 | setSubscribe(service_url.bindTeacher(getHeader(), body), observer); |
434 | } | 436 | } |
435 | 437 | ||
436 | public static void getError(Map map, Observer<ResponseResult<PageInfo<TopicBean>>> observer) { | 438 | public static void getError(Map map, Observer<ResponseResult<PageInfo<TopicBean>>> observer) { |
437 | setSubscribe(service_url.getError(getHeader(), map), observer); | 439 | setSubscribe(service_url.getError(getHeader(), map), observer); |
438 | } | 440 | } |
439 | 441 | ||
440 | 442 | ||
441 | public static RequestBody getMapRequestBody(Map map) { | 443 | public static RequestBody getMapRequestBody(Map map) { |
442 | return RequestBody.create(MediaType.parse("text/plain"), new Gson().toJson(map)); | 444 | return RequestBody.create(MediaType.parse("text/plain"), new Gson().toJson(map)); |
443 | } | 445 | } |
444 | 446 | ||
445 | 447 | ||
446 | public static RequestBody getArrayRequestBody(List list) { | 448 | public static RequestBody getArrayRequestBody(List list) { |
447 | return RequestBody.create(MediaType.parse("text/plain"), new Gson().toJson(list)); | 449 | return RequestBody.create(MediaType.parse("text/plain"), new Gson().toJson(list)); |
448 | } | 450 | } |
449 | 451 | ||
450 | public static RequestBody getFileRequestBody(File file) { | 452 | public static RequestBody getFileRequestBody(File file) { |
451 | return RequestBody.create(MediaType.parse("application/octet-stream"), file); | 453 | return RequestBody.create(MediaType.parse("application/octet-stream"), file); |
452 | } | 454 | } |
453 | 455 | ||
454 | public static RequestBody getFileRequestBody(byte[] bytes) { | 456 | public static RequestBody getFileRequestBody(byte[] bytes) { |
455 | return RequestBody.create(MediaType.parse("multipart/form-data"), bytes); | 457 | return RequestBody.create(MediaType.parse("multipart/form-data"), bytes); |
456 | } | 458 | } |
457 | 459 | ||
458 | public static RequestBody getObjectRequestBody(Object obj) { | 460 | public static RequestBody getObjectRequestBody(Object obj) { |
459 | return RequestBody.create(MediaType.parse("text/plain"), new Gson().toJson(obj)); | 461 | return RequestBody.create(MediaType.parse("text/plain"), new Gson().toJson(obj)); |
460 | } | 462 | } |
461 | 463 | ||
462 | public static RequestBody getStringRequestBody(String str) { | 464 | public static RequestBody getStringRequestBody(String str) { |
463 | return RequestBody.create(MediaType.parse("text/plain"), str); | 465 | return RequestBody.create(MediaType.parse("text/plain"), str); |
464 | } | 466 | } |
465 | 467 | ||
466 | 468 | ||
467 | /** | 469 | /** |
468 | * 插入观察者 | 470 | * 插入观察者 |
469 | * | 471 | * |
470 | * @param observable | 472 | * @param observable |
471 | * @param observer | 473 | * @param observer |
472 | * @param <T> | 474 | * @param <T> |
473 | */ | 475 | */ |
474 | public static <T> void setSubscribe(Observable<T> observable, Observer<T> observer) { | 476 | public static <T> void setSubscribe(Observable<T> observable, Observer<T> observer) { |
475 | observable.subscribeOn(Schedulers.io())//子线程访问网络 | 477 | observable.subscribeOn(Schedulers.io())//子线程访问网络 |
476 | .observeOn(AndroidSchedulers.mainThread())//回调到主线程 | 478 | .observeOn(AndroidSchedulers.mainThread())//回调到主线程 |
477 | .subscribe(observer); | 479 | .subscribe(observer); |
478 | } | 480 | } |
479 | 481 | ||
480 | } | 482 | } |
481 | 483 |
libs/common/src/main/java/com/prws/common/net/SignInterceptor.java
1 | package com.prws.common.net; | 1 | package com.prws.common.net; |
2 | 2 | ||
3 | import androidx.annotation.NonNull; | ||
4 | |||
5 | import com.prws.common.BuildConfig; | ||
3 | import com.prws.common.utils.SharedPreferencesUtil; | 6 | import com.prws.common.utils.SharedPreferencesUtil; |
4 | 7 | ||
5 | import org.apache.commons.codec.binary.Hex; | 8 | import org.apache.commons.codec.binary.Hex; |
6 | import org.apache.commons.codec.digest.DigestUtils; | 9 | import org.apache.commons.codec.digest.DigestUtils; |
7 | 10 | ||
8 | import java.io.IOException; | 11 | import java.io.IOException; |
9 | import java.util.Date; | 12 | import java.util.Date; |
10 | import java.util.Random; | 13 | import java.util.Random; |
11 | 14 | ||
12 | import okhttp3.Interceptor; | 15 | import okhttp3.Interceptor; |
13 | import okhttp3.Request; | 16 | import okhttp3.Request; |
14 | import okhttp3.Response; | 17 | import okhttp3.Response; |
15 | 18 | ||
16 | public class SignInterceptor implements Interceptor { | 19 | public class SignInterceptor implements Interceptor { |
20 | @NonNull | ||
17 | @Override | 21 | @Override |
18 | public Response intercept(Chain chain) throws IOException { | 22 | public Response intercept(Chain chain) throws IOException { |
19 | // String appId = "1"; | 23 | String host = BuildConfig.SERVER_URL; |
20 | // String APP_SECRET = "a839c40e04477d43705a64ba9db23b0dba969df360c3e428add85a6f5b8d0622"; | 24 | if (!chain.request().url().toString().startsWith(host)) { |
21 | // String timestamp = String.valueOf(new Date().getTime()); | 25 | return chain.proceed(chain.request()); |
22 | // String nonce = String.valueOf(new Random().nextInt(10000)); | 26 | } |
23 | // String sign = DigestUtils.md5Hex(appId + APP_SECRET + timestamp + nonce); // yidao板子报错 采用如下代码 | 27 | |
24 | // byte[] signBytes = DigestUtils.md5(appId + APP_SECRET + timestamp + nonce); | ||
25 | // String sign = new String(Hex.encodeHex(signBytes)); | ||
26 | String token = (String) SharedPreferencesUtil.getData("token",""); | 28 | String token = (String) SharedPreferencesUtil.getData("token",""); |
27 | Request.Builder requestBuilder = chain.request().newBuilder(); | 29 | Request.Builder requestBuilder = chain.request().newBuilder(); |
28 | if (token != null && token.length() > 0) { | 30 | if (token != null && token.length() > 0) { |
29 | requestBuilder = chain.request().newBuilder() | 31 | requestBuilder = chain.request().newBuilder() |
30 | .header("Authorization", token); | 32 | .header("Authorization", token); |
31 | } | 33 | } |
32 | // .header("Content-Type","application/json"); | ||
33 |