Foreword
retrofit package okhttp request based framework network, is OkHttp to complete work on the nature of network requests, and retrofit package interface is only responsible for network requests if you do not know OKhttp suggest that you first understand it is to learn to use retrofit, portal:. Android development framework uses detailed series OkHttp
Retrofit advantage is simple to use, decoupling, scalability, can be used with a variety of Json parsing framework (eg Gson), in addition to support RxJava. However, this does not explain the blog section with use of RxJava, and with the use of RxJava I will explain in another blog post.
In addition retrofit already packaged very well, matching a variety of usage on the maximum extent already, so I do not recommend unnecessary re- package retrofit (most packaged retrofit singleton) . Again package does not look handsome not let you are very Niubi. will only make you look stupid. the expansion has been very very decoupling achieve total destruction.
Github Address
https://github.com/square/retrofit
rely
If you do not use RxJava mode, then you only need to rely on the following two:
implementation 'com.squareup.retrofit2:retrofit:2.6.2' implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
gson is used to parse Json data used (personal preference Gson), Retrofit also supports other analytical tools such fastJson
Simple Demo
The old rules by thinking order to explain demo
1. Create a basic configuration request Retrofit
Retrofit Once configured, you can use a global Retrofit to request to the network (so you can achieve a single globally embodiments use), but of course, the following code demo:
Private Retrofit mRetrofit; Private void initHttpBase () { mRetrofit = new new Retrofit.Builder () .baseUrl ( "http://doclever.cn:8090/mock/5c3c6da33dce46264b24452b/") // Base network address .addConverterFactory (GsonConverterFactory.create ()) // use Gson parsing .build (); }
2. Create Bean class after the data is returned
public class LoginBean { private int code; private String message; public int getCode() { return code; } public void setCode(int code) { this.code = code; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
2. Create a network request interface
public interface HttpList { @FormUrlEncoded // annotation indicates there @Multipart form from a form available course you can not add @POST ( "Test / login_test") // network request path Call <LoginBean> login (@Field ( " Number ") Number String, @Field (" password " ) String password); }
Note that this is an interface class. LoginBean Bean class is returned after the data ( Retrofit automatically imported using analytical Gson)
3. Request Network
private void postHttp(){ HttpList httpList = mRetrofit.create(HttpList.class); Call<LoginBean> call = httpList.login("181234123", "123456"); call.enqueue(new Callback<LoginBean>() { @Override public void onResponse(Call<LoginBean> call, Response<LoginBean> response) { LoginBean bean = response.body(); Log.e(TAG, "onResponse: code="+bean.getCode()); Log.e(TAG, "onResponse: message="+bean.getMessage()); } @Override public void the onFailure (Call <the LoginBean> Call, the Throwable T) { Log.e (the TAG, "the onFailure: Network request failed =" + t.getMessage ()); } }); }
In this way, we completed a network request is not particularly simple
How to Stop the network request
How to Add Header head
Header information is added in the form of fixed data
public interface HttpList { @Headers({"content1:one","content2:two"}) @POST("test/logout_test") Call<LoginBean> logout1(); }
Header information is added in the form of non-fixed data
public interface HttpList { @POST("test/logout_test") Call<LoginBean> logout2(@Header("content") String content); }