public interface UserServiceApi {
@POST("api/login")
Call> login(@Body WechatLoginRequest request);
@POST("version/version")
Call> update(@Body Update request);
@POST("api/logout")
Call logout(@Body BaseRequest request);
@POST("api/user/info")
Call> getOtherUserInfo(@Body BaseRequest request);
}
public interface Api {
String PATH = "path";
@POST("{" + PATH + "}")
Observable runPost(@Path(value = PATH, encoded = true) String path,
@Body String json);
/**
* form表单提交
*/
@GET("{" + PATH + "}")
Observable runGetII(@Path(value = PATH, encoded = true) String path, @QueryMap Map json);
}
/**
* @ReqTags("")
*
* @ReqTags(value="注册",extdParams={"user/register"},extdCmdParams={"1"})
*/
@Inherited
@Documented
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface ReqTags {
String value() default "";
/**
* 扩展string参数,如有需要
* @return
*/
String[] extdParams() default {};
/**
* 扩展命令,如有需要
*/
int[] extdCmdParams() default {};
}
public interface PostfixImpl {
/**
* 后缀
*/
String postfix();
/**
* 跳过携带参数吗,默认false
* @return
*/
boolean skipParams();
}
@ReqTags("")
public abstract class CommonRequest implements Serializable, PostfixImpl {
// 这里可以构造一些通用参数,如有需要的话
// public String channel;//来源渠道
public CommonRequest() {
}
@Override
public boolean skipParams() {
return false; // 不跳过携带参数
}
}
@ReqTags("注册")
public class RegisterReq extends CommonRequest {
public String username;
public String password;
public String repassword;
public RegisterReq(String username, String password, String repassword) {
this.username = username;
this.password = password;
this.repassword = repassword;
}
public RegisterReq() {
}
@Override
public String postfix() {
return "user/register";
}
}
@ReqTags("登录")
public class LoginReq extends CommonRequest {
public String username;
public String password;
public String repassword;
public LoginReq(String username, String password, String repassword) {
this.username = username;
this.password = password;
this.repassword = repassword;
}
public LoginReq() {
}
@Override
public String postfix() {
return "user/login";
}
}
@ReqTags("列表")
public class ListReq extends CommonRequest {
public int page=1;
public ListReq() {
}
@Override
public String postfix() {
return "user/list";
}
}
@ReqTags("更新")
public class UpdateReq extends CommonRequest {
public UpdateReq() {
}
@Override
public String postfix() {
return "user/update";
}
}
@ReqTags("测试")
public class TstReq extends CommonRequest {
public String username;
@Override
public String postfix() {
return "user/test";
}
@Override
public boolean skipParams() {
return true; // 不带请求参数,返回true
}
}
final RegisterReq json = new RegisterReq("12132322333", "123456", "123456");
HttpRequestFactory.doPost(json, new ResultCallbackAdapt>() {
@Override
public void doOnResponse(HttpCommObjResp response) {
System.out.println(response + "");
mContent.setText("\n方式一返回:" + response);
}
@Override
public void doOnError(ApiException ex) {
System.out.println(ex + "");
mContent.setText("\n方式一返回:" + ex);
}
}, iLoading);
// 方式二
HttpRequestFactory.doPost(json, new ResultCallbackAdapt() {
@Override
public void doOnResponse(String response) {
System.out.println(response + "");
mContent.setText("\n方式二返回:" + response);
}
@Override
public void doOnError(ApiException ex) {
System.out.println(ex + "");
mContent.setText("\n方式二返回:" + ex);
}
});
初始化函数
public class MyApplication extends Application
{
@Override
public void onCreate()
{
super.onCreate();
HttpRequestFactory.doCreateHttpReqManager(this, ResLibConfig.DEBUG ? "http://www.wanandroid.com/" : "http://www.wanandroid.com/")
.registerCallbacks(new HttpHeadersImpl() {
@Override
public Map generateHeaders() {
Map keys = new HashMap<>();
// keys.put("versionCode", "" + IUtil.obtainAppVersionCode(ResLibConfig.CONTEXT));
// keys.put("version", IUtil.obtainAppVersion(ResLibConfig.CONTEXT));
// keys.put("channel", "");
// keys.put("imei", "");
// keys.put("platform", "2");//1:ios 2:Android
// keys.put("model", IUtil.MODEL);
// keys.put("vendor", IUtil.BRAND);
return keys; // 请求头注入,如有需要
}
}).registerCallbacks(new HttpInterceptor() {
@Override
public void doInterceptor(int... cmds) {
if (cmds != null && cmds.length > 0) {
int code = cmds[0];
switch (code) {
case HttpInterceptor.KICKED_OFF_LINE:
//Tools.showToast("用户已在其他设备登录");
int value = AUTO_INCREMENT.incrementAndGet();
if (value == 1) {
// relogin(); // 调用一次
}
Logger.e("http", "用户已在其他设备登录|" + value + "|");
break;
case HttpInterceptor.PROHIBIT_USED:
//Tools.showToast("您被禁止使用");
break;
case HttpInterceptor.PROHIBIT_USEDII:
//Tools.showToast("用户被封禁,不能登录");
break;
case HttpInterceptor.UN_LOGIN:
//Tools.showToast("请先登录");
//relogin(false);
break;
}
}
}
}).asEncryptFuncs(new EncryptFuncs() {
@Override
public String encrypt(String plainText) {
//return AesEncryptionUtil.encrypt(plainText);
return "";
}
@Override
public String decrypt(String cipherText) {
//return AesEncryptionUtil.decrypt(cipherText);
return "";
}
/**
* {@link ReqTags}
* {@link kaiqi.cn.httpx.req.RegisterReq}
* 请给予tag,否则取消不掉请求,拦截不了加密解密赛~
*/
@Override
public boolean accept(String httpReaTag) {
if ("注册".equals(httpReaTag)) {
// return false; // 你们都同意,我注册第一个不答应
}
return true; // 同意加密
}
}).asSubmmitForm(true); // true = 表单提交 false = json提交
}
}
{
"code":200,
"msg":"成功",
"data":{}
}
{
"code":200,
"msg":"成功",
"data":[]
}
{
"data": {
"collectIds": [],
"email": "",
"icon": "",
"id": 9073,
"password": "123456",
"token": "",
"type": 0,
"username": "19910222"
},
"errorCode": 0,
"errorMsg": ""
}
{
"msg":"",
"code"100,
"data":{}
}
{
"errorMsg":"",
"errorCode"100,
"data":{}
}
{
"message":"",
"error"100,
"infos":{}
}
{
....
"results":{}
}
HttpRequestFactory.doPost(json, new ResultCallbackAdapt() {
@Override
public void doOnResponse(String response) {
System.out.println(response + "");
mContent.setText("\n方式二返回:" + response);
}
@Override
public void doOnError(ApiException ex) {
System.out.println(ex + "");
mContent.setText("\n方式二返回:" + ex);
}
});
HttpRequestFactory.cancel("注册");//取消单个
HttpRequestFactory.cancel("注册","首页文章列表"); // 取消多个
混淆配置
为什么要这样写?看下服务端代马
@RequestMapping(value = "/showUser", method = { RequestMethod.GET,
RequestMethod.POST }, produces = "application/json;charset=UTF-8")
public JSONObject toIndex(@RequestBody(required = false) String data) {
return callHttpReqTask(data, TO_INDEX);
}
@RequestMapping(value = "/addUser", method = { RequestMethod.GET,
RequestMethod.POST }, produces = "application/json;charset=UTF-8")
public JSONObject addUser(@RequestBody(required = false) String data) {
return callHttpReqTask(data, ADD_USER);
}
Map<String, String> params = new HashMap<String, String>();
params.put("name", "tsy");
params.put("name", "tsy");
params.put("name", "tsy");
params.put("name", "tsy");
params.put("name", "tsy");
params.put("name", "tsy");
params.put("name", "tsy");
params.put("name", "tsy");
params.put("name", "tsy"); // 每一个接口都初始化 hashmap~ What Fuck ~有点累阿,交给gson多好
MyOkHttp.get().post(this, "http://baidu.com/", params, new JsonResponseHandler() {
@Override
public void onSuccess(int statusCode, JSONObject response) {
LogUtils.v(TAG, statusCode + " " + response);
}
@Override
public void onFailure(int statusCode, String error_msg) {
LogUtils.v(TAG, statusCode + " " + error_msg);
}
});
@ReqTags("接口姓甚名谁") // 他还有2个兄弟拓展参数,保留着没用,如果你愿意,可以对他动手脚
public class X extends CommonRequest{
public String name;
public String pahone;
public X(String name,String phone){}
@Override
public String postfix() {
return "user/register";
}
}
HttpRequestFactory.doPost(new X("xx","yy"), new ResultCallbackAdapt>() {
@Override
public void doOnResponse(HttpCommObjResp response) {
System.out.println(response + "");
mContent.setText("\n方式一返回:" + response);
}
@Override
public void doOnError(ApiException ex) {
System.out.println(ex + "");
mContent.setText("\n方式一返回:" + ex);
}
); //不要loading时
//}, iLoading); // loading,这东西如有需要
get请求呢,改个名字....即可!!饥渴~~
HttpRequestFactory.doGet(new X("xx","yy"), new ResultCallbackAdapt>() {
@Override
public void doOnResponse(HttpCommObjResp response) {
System.out.println(response + "");
mContent.setText("\n方式一返回:" + response);
}
@Override
public void doOnError(ApiException ex) {
System.out.println(ex + "");
mContent.setText("\n方式一返回:" + ex);
}
); //不要loading时
//}, iLoading); // loading,这东西如有需要
使用方式,如有需要
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.majunm:HttpX:v1.0.0-gamma'
}