Java原生的Http网络框架,底层基于HttpNet,动态代理+构建的!
#Elegant项目结构如下
Elegant采用Retrofit动态代理+构建的思想,本身并不做网络请求,网络部分基于HttpNet实现,本着简洁清晰的思想,保持了和Retrofit相似的API
##gradle
compile 'com.haibin:elegant:1.1.9'
##创建API接口
public interface LoginService {
//普通POST
@Headers({"Cookie:cid=adcdefg;"})
@POST("api/users/login")
Call<BaseModel<User>> login(@Form("email") String email,
@Form("pwd") String pwd,
@Form("versionNum") int versionNum,
@Form("dataFrom") int dataFrom);
// 上传文件
@POST("action/apiv2/user_edit_portrait")
@Headers("Cookie:xxx=hbbb;")
Call<String> postAvatar(@File("portrait") String file);
//JSON POST
@POST("action/apiv2/user_edit_portrait")
@Headers("Cookie:xxx=hbbb;")
Call<String> postJson(@Json String file);
//PATCH
@PATCH("mobile/user/{uid}/online")
Call<ResultBean<String>> handUp(@Path("uid") long uid);
}
##执行请求
public static final String API = "http://www.oschina.net/";
public static Elegant elegant = new Elegant();
static {
elegant.registerApi(API);
}
LoginService service = elegant.from(LoginService.class)
.login("xxx@qq.com", "123456", 2, 2);
.withHeaders(Headers...)
.execute(new CallBack<BaseModel<User>>() {
@Override
public void onResponse(Response<BaseModel<User>> response) {
}
@Override
public void onFailure(Exception e) {
} });