老壳兼容

This commit is contained in:
nbwzlyd1 2023-05-01 14:12:32 +08:00
parent 1a6f0f10ae
commit 253c057eb4
2 changed files with 33 additions and 1 deletions

View File

@ -6,7 +6,9 @@ import android.text.TextUtils;
import com.github.catvod.ali.API;
import com.github.catvod.bean.Result;
import com.github.catvod.crawler.Spider;
import com.github.catvod.utils.ReflectUtil;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
@ -21,7 +23,12 @@ public class Ali extends Spider {
@Override
public void init(Context context, String extend) {
String token = getToken();
//适配其他tvbox或者老版本tvbox要先判断有没有getToken方法调用setToken也是一样的
Method method = ReflectUtil.getMethod(this, "getToken");
String token = "";
if (method != null) {
token = getToken();
}
API.get().setRefreshToken(TextUtils.isEmpty(token) ? extend : token);
}

View File

@ -0,0 +1,25 @@
package com.github.catvod.utils;
import java.lang.reflect.Method;
public class ReflectUtil {
/**
* @Description: 判断是否包含某个方法
*/
public static Method getMethod(Object object, String methodName, Class<?> ... parameterTypes){
Method method = null ;
for(Class<?> clazz = object.getClass() ; clazz != Object.class ; clazz = clazz.getSuperclass()) {
try {
method = clazz.getMethod(methodName,parameterTypes) ;
return method ;
} catch (Exception e) {
//这里甚么都不要做并且这里的异常必须这样写不能抛出去
//如果这里的异常打印或者往外抛则就不会执行clazz = clazz.getSuperclass(),最后就不会进入到父类中了
}
}
return null;
}
}