在了解学些hilt之前,先了解一下dagger2的知识
Dagger2详解——最直白易懂、详细的dagger2注入流程原理
hilt是对draager封装的改进版本,更容易进行注入,更少写代码
封装一个简易的hilt网络请求框架,这个框架可以随时切换使用的框架,框架就是用了hilt进行注入使用
okhttp注解解析器
public class OkHttpProcessor implements IHttpProcessor{
private OkHttpClient mOkHttpClient;
private Handler myHandler;
//注入构造函数,提供使用
@Inject
public OkHttpProcessor(){
mOkHttpClient=new OkHttpClient();
myHandler=new Handler();
}
//公共post请求
@Override
public void post(String url, Map<String, Object> params, final ICallback callback) {
final RequestBody requestBody=appendBody(params);
Request request=new Request.Builder()
.url(url)
.post(requestBody)
.build();
mOkHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) throws IOException {
final String result=response.body().string();
if(response.isSuccessful()){
myHandler.post(new Runnable() {
@Override
public void run() {
callback.onSuccess(result);
}
});
}else{
myHandler.post(new Runnable() {
@Override
public void run() {
callback.onFailure(result);
}
});
}
}
@Override
public void onFailure(Call call, IOException e) {
myHandler.post(new Runnable() {
@Override
public void run() {
callback.onFailure("onFailure");
}
});
}
});
}
private RequestBody appendBody(Map<String, Object> params){
FormBody.Builder body=new FormBody.Builder();
if(params==null || params.isEmpty()){
return body.build();
}
for(Map.Entry<String, Object> entry:params.entrySet()){
body.add(entry.getKey(),entry.getValue().toString());
}
return body.build();
}
}
volley框架
public class VolleyProcessor implements IHttpProcessor{
private static RequestQueue mQueue=null;
@Inject
public VolleyProcessor(@ApplicationContext Context context){
mQueue= Volley.newRequestQueue(context);
}
@Override
public void post(String url, Map<String, Object> params, final ICallback callback) {
StringRequest stringRequest=new StringRequest(Request.Method.POST,
url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
callback.onSuccess(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
mQueue.add(stringRequest);
}
}
XUtils
public class XUtilsProcessor implements IHttpProcessor {
@Inject
public XUtilsProcessor(Application app){
x.Ext.init((MyApplication)app);
}
@Override
public void post(String url, Map<String, Object> params, final ICallback callback) {
RequestParams requestParams=new RequestParams(url);
x.http().post(requestParams, new Callback.CommonCallback<String>() {
@Override
public void onSuccess(String result) {
callback.onSuccess(result);
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
}
});
}
}
同时提供自己的注解
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
public @interface BindOkhttp {
}
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
public @interface BindVolley {
}
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
public @interface BindXUtils {
}
myapplication统一注入
@HiltAndroidApp
public class MyApplication extends Application {
@BindXUtils
@Inject
IHttpProcessor iHttpProcessor;
public IHttpProcessor getiHttpProcessor(){
return iHttpProcessor;
}
}
提供module使用,注入到application里
@Module
@InstallIn(ApplicationComponent.class)
public abstract class HttpProcessorModule {
@BindOkhttp
@Binds
@Singleton
abstract IHttpProcessor bindOkhttp(OkHttpProcessor okHttpProcessor);
@BindVolley
@Binds
@Singleton
abstract IHttpProcessor bindVolley(VolleyProcessor volleyProcessor);
@BindXUtils
@Binds
@Singleton
abstract IHttpProcessor bindXUtils(XUtilsProcessor xUtilsProcessor);
}
使用
@AndroidEntryPoint
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iHttpProcessor=((MyApplication)getApplication()).getiHttpProcessor();
}
public void click(View view) {
String url = "https://v.juhe.cn/historyWeather/citys";
HashMap<String, Object> params = new HashMap<>();
params.put("province_id", "2");
params.put("key", "bb52107206585ab074f5e59a8c73875b");
iHttpProcessor.post(url, params, new HttpCallback<ResponceData>() {
@Override
public void onSuccess(ResponceData objResult) {
Toast.makeText(MainActivity.this, objResult.getResultcode(), Toast.LENGTH_SHORT).show();
}
});
}
}
hilt封装的非常完整,但也是基于dragger2来实现,在使用apt等技术,编译器生成注入代码,并提供使用