Skip to content

Commit ec48628

Browse files
解析器转kotlin编写
1 parent 77efca2 commit ec48628

21 files changed

Lines changed: 386 additions & 450 deletions

app/src/main/java/com/example/httpsender/parser/ResponseListParser.java

Lines changed: 0 additions & 58 deletions
This file was deleted.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.example.httpsender.parser
2+
3+
import com.example.httpsender.entity.Response
4+
import rxhttp.wrapper.annotation.Parser
5+
import rxhttp.wrapper.entity.ParameterizedTypeImpl
6+
import rxhttp.wrapper.exception.ParseException
7+
import rxhttp.wrapper.parse.AbstractParser
8+
import java.io.IOException
9+
import java.lang.reflect.Type
10+
11+
/**
12+
* 输入T,输出List<T>,并对code统一判断
13+
* User: ljx
14+
* Date: 2018/10/23
15+
* Time: 13:49
16+
</T> */
17+
@Parser(name = "ResponseList")
18+
open class ResponseListParser<T> : AbstractParser<MutableList<T>> {
19+
/**
20+
* 此构造方法适用于任意Class对象,但更多用于带泛型的Class对象,如:List<Student>
21+
*
22+
* 用法:
23+
* Java: .asParser(new ResponseListParser<List<Student>>(){})
24+
* Kotlin: .asParser(object : ResponseListParser<List<Student>>() {})
25+
*
26+
* 注:此构造方法一定要用protected关键字修饰,否则调用此构造方法将拿不到泛型类型
27+
</Student></Student></Student> */
28+
protected constructor() : super()
29+
30+
/**
31+
* 此构造方法仅适用于解析不带泛型的Class对象,如: Student.class
32+
*
33+
* 用法
34+
* Java: .asParser(new ResponseListParser<>(Student.class)) 或者 .asResponseList(Student.class)
35+
* Kotlin: .asParser(ResponseListParser(Student::class.java)) 或者 .asResponseList(Student::class.java)
36+
*/
37+
constructor(type: Class<T>) : super(type)
38+
39+
@Throws(IOException::class)
40+
override fun onParse(response: okhttp3.Response): MutableList<T> {
41+
val type: Type = ParameterizedTypeImpl.get(Response::class.java, MutableList::class.java, mType) //获取泛型类型
42+
val data: Response<MutableList<T>> = convert(response, type)
43+
val list = data.data //获取data字段
44+
if (data.code != 0 || list == null) { //code不等于0,说明数据不正确,抛出异常
45+
throw ParseException(data.code.toString(), data.msg, response)
46+
}
47+
return list
48+
}
49+
}

app/src/main/java/com/example/httpsender/parser/ResponsePageListParser.java

Lines changed: 0 additions & 58 deletions
This file was deleted.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.example.httpsender.parser
2+
3+
import com.example.httpsender.entity.PageList
4+
import com.example.httpsender.entity.Response
5+
import rxhttp.wrapper.annotation.Parser
6+
import rxhttp.wrapper.entity.ParameterizedTypeImpl
7+
import rxhttp.wrapper.exception.ParseException
8+
import rxhttp.wrapper.parse.AbstractParser
9+
import java.io.IOException
10+
import java.lang.reflect.Type
11+
12+
/**
13+
* 输入T,输出PageList<T>,并对code统一判断
14+
* User: ljx
15+
* Date: 2018/10/23
16+
* Time: 13:49
17+
</T> */
18+
@Parser(name = "ResponsePageList")
19+
open class ResponsePageListParser<T> : AbstractParser<PageList<T>> {
20+
/**
21+
* 此构造方法适用于任意Class对象,但更多用于带泛型的Class对象,如:List<Student>
22+
*
23+
* 用法:
24+
* Java: .asParser(new ResponsePageListParser<List<Student>>(){})
25+
* Kotlin: .asParser(object : ResponsePageListParser<List<Student>>() {})
26+
*
27+
* 注:此构造方法一定要用protected关键字修饰,否则调用此构造方法将拿不到泛型类型
28+
</Student></Student></Student> */
29+
protected constructor() : super()
30+
31+
/**
32+
* 此构造方法仅适用于解析不带泛型的Class对象,如: Student.class
33+
*
34+
* 用法
35+
* Java: .asParser(new ResponsePageListParser<>(Student.class)) 或者 .asResponsePageList(Student.class)
36+
* Kotlin: .asParser(ResponsePageListParser(Student::class.java)) 或者 .asResponsePageList(Student::class.java)
37+
*/
38+
constructor(type: Class<T>) : super(type)
39+
40+
@Throws(IOException::class)
41+
override fun onParse(response: okhttp3.Response): PageList<T> {
42+
val type: Type = ParameterizedTypeImpl.get(Response::class.java, PageList::class.java, mType) //获取泛型类型
43+
val data: Response<PageList<T>> = convert(response, type)
44+
val pageList = data.data //获取data字段
45+
if (data.code != 0 || pageList == null) { //code不等于0,说明数据不正确,抛出异常
46+
throw ParseException(data.code.toString(), data.msg, response)
47+
}
48+
return pageList
49+
}
50+
}

app/src/main/java/com/example/httpsender/parser/ResponseParser.java renamed to app/src/main/java/com/example/httpsender/parser/ResponseParser.kt

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
package com.example.httpsender.parser;
1+
package com.example.httpsender.parser
22

3-
4-
import com.example.httpsender.entity.Response;
5-
6-
import java.io.IOException;
7-
import java.lang.reflect.Type;
8-
9-
import rxhttp.wrapper.annotation.Parser;
10-
import rxhttp.wrapper.entity.ParameterizedTypeImpl;
11-
import rxhttp.wrapper.exception.ParseException;
12-
import rxhttp.wrapper.parse.AbstractParser;
3+
import android.graphics.Point
4+
import com.example.httpsender.entity.Response
5+
import rxhttp.wrapper.annotation.Parser
6+
import rxhttp.wrapper.entity.ParameterizedTypeImpl
7+
import rxhttp.wrapper.exception.ParseException
8+
import rxhttp.wrapper.parse.AbstractParser
9+
import java.io.IOException
10+
import java.lang.reflect.Type
1311

1412
/**
1513
* 输入T,输出T,并对code统一判断
@@ -18,8 +16,7 @@
1816
* Time: 13:49
1917
*/
2018
@Parser(name = "Response")
21-
public class ResponseParser<T> extends AbstractParser<T> {
22-
19+
open class ResponseParser<T> : AbstractParser<T> {
2320
/**
2421
* 此构造方法适用于任意Class对象,但更多用于带泛型的Class对象,如:List<Student>
2522
*
@@ -28,10 +25,8 @@ public class ResponseParser<T> extends AbstractParser<T> {
2825
* Kotlin: .asParser(object : ResponseParser<List<Student>>() {})
2926
*
3027
* 注:此构造方法一定要用protected关键字修饰,否则调用此构造方法将拿不到泛型类型
31-
*/
32-
protected ResponseParser() {
33-
super();
34-
}
28+
</Student></Student></Student> */
29+
protected constructor() : super()
3530

3631
/**
3732
* 此构造方法仅适用于不带泛型的Class对象,如: Student.class
@@ -40,27 +35,24 @@ protected ResponseParser() {
4035
* Java: .asParser(new ResponseParser<>(Student.class)) 或者 .asResponse(Student.class)
4136
* Kotlin: .asParser(ResponseParser(Student::class.java)) 或者 .asResponse(Student::class.java)
4237
*/
43-
public ResponseParser(Class<T> type) {
44-
super(type);
45-
}
46-
47-
@SuppressWarnings("unchecked")
48-
@Override
49-
public T onParse(okhttp3.Response response) throws IOException {
50-
final Type type = ParameterizedTypeImpl.get(Response.class, mType); //获取泛型类型
51-
Response<T> data = convert(response, type);
52-
T t = data.getData(); //获取data字段
53-
if (t == null && mType == String.class) {
38+
constructor(type: Class<T>) : super(type)
39+
40+
@Throws(IOException::class)
41+
override fun onParse(response: okhttp3.Response): T {
42+
val type: Type = ParameterizedTypeImpl.get(Response::class.java, mType) //获取泛型类型
43+
val data: Response<T> = convert(response, type)
44+
var t = data.data //获取data字段
45+
if (t == null && mType === String::class.java) {
5446
/*
5547
* 考虑到有些时候服务端会返回:{"errorCode":0,"errorMsg":"关注成功"} 类似没有data的数据
5648
* 此时code正确,但是data字段为空,直接返回data的话,会报空指针错误,
5749
* 所以,判断泛型为String类型时,重新赋值,并确保赋值不为null
5850
*/
59-
t = (T) data.getMsg();
51+
t = data.msg as T
6052
}
61-
if (data.getCode() != 0 || t == null) {//code不等于0,说明数据不正确,抛出异常
62-
throw new ParseException(String.valueOf(data.getCode()), data.getMsg(), response);
53+
if (data.code != 0 || t == null) { //code不等于0,说明数据不正确,抛出异常
54+
throw ParseException(data.code.toString(), data.msg, response)
6355
}
64-
return t;
56+
return t
6557
}
66-
}
58+
}

rxhttp/src/main/java/rxhttp/wrapper/parse/AbstractParser.java

Lines changed: 0 additions & 27 deletions
This file was deleted.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package rxhttp.wrapper.parse
2+
3+
import com.google.gson.internal.`$Gson$Preconditions`
4+
import com.google.gson.internal.`$Gson$Types`
5+
import rxhttp.wrapper.utils.TypeUtil
6+
import java.lang.reflect.Type
7+
8+
/**
9+
* User: ljx
10+
* Date: 2019/1/21
11+
* Time: 15:32
12+
*/
13+
abstract class AbstractParser<T> : Parser<T> {
14+
@JvmField
15+
protected var mType: Type
16+
17+
constructor() {
18+
mType = TypeUtil.getActualTypeParameter(javaClass, 0)
19+
}
20+
21+
constructor(type: Type) {
22+
mType = `$Gson$Types`.canonicalize(`$Gson$Preconditions`.checkNotNull(type))
23+
}
24+
}

rxhttp/src/main/java/rxhttp/wrapper/parse/BitmapParser.java

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)