Retrofit Network Request Framework, Introduction


Retrofit and okhttp come from Square, and retrofit is a layer of encapsulation of okhttp. All network requests are handed over to Okhttp, we only need to use retrofit to make network requests through simple configuration

To use Retrofit, first add dependencies in the app's build.gradle

compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4'//Retrofit2所需要的包
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'//ConverterFactory的Gson依赖包
compile 'com.squareup.retrofit2:converter-scalars:2.0.0-beta4'//ConverterFactory的String依赖包
* It is worth noting here that the version of the imported retrofit2 package must be the same , otherwise an error will be reported


We assume that the

http://106.3.227.33/pulamsi/mobileLogin/submit.html
as the request address.

Unlike Okhttp, Retrofit needs to define an interface to return our Call object. Here is the Post request:

public interface RequestServes {
    @POST("mobileLogin/submit.html")
    Call<String> getString(@Query("loginname") String loginname,
@Query("nloginpwd") String nloginpwd);
}                           
The request method annotations provided by Retrofit include @GET and @POST , and the parameter annotations include @PATH and @Query , etc. We only introduce the commonly used ones; the first two, as the name implies, are to define your request method Get or Post , and the latter @PATH refers to filling the complete path through parameters. General usage:

@GET("{name}")
Call<User>getUser(@Path("name") String name);

The parameter username here will be filled {name}in to form a complete Url request address, which {name}is equivalent to a placeholder;

@QueryIt is the setting of the key-value pair of our request. When we construct the Call object, we will pass in this parameter.

1
2
3
@POST("mobileLogin/submit.html")
   	Call<String> getString(@Query("loginname") String loginname,
                          @Query("nloginpwd") String nloginpwd);
这里 @Query("loginname") 就是键,后面的 loginname 就是具体的值了,值得注意的是Get和Post请求,都是这样填充参数的;

参数定义好了之后,我们使用Retrofit进行网络请求

创建一个Retrofit 对象

Retrofit retrofit = new Retrofit.Builder() 
               .baseUrl( "http://106.3.227.33/pulamsi/" ) 
//Add support for return value of                String.addConverterFactory(ScalarsConverterFactory.create()) //Add return value to Gson Support (returned as entity class)                .addConverterFactory(GsonConverterFactory.create()) //Add support for return value Oservable<T>                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())                .build();               

               

               

The baseurl here is spliced ​​into the previously @POST("mobileLogin/submit.html") defined form a complete url


addConverterFactory(ScalarsConverterFactory.create())It means to build a return support, if the generic type of your Call object receives another format and needs to add additional support, the above code has been listed;

Next, we use this Retrofitobject to create an RequestSerivesinterface object, which is the interface we defined earlier, and get our Call object;

 

//This is the dynamic proxy mode of Java
RequestSerives requestSerives = retrofit.create(RequestSerives.class);
//pass in the value of the key-value pair we requested 
Call<String> call = requestSerives.getString("userName", "1234");

利用得到的Call对象,然后我们就可以进行网络请求了:

call.enqueue(new Callback<String>() {
//实现Callback中的两个方法
    @Override
    public void onResponse(Call<String> call, Response<String> response) {
        //成功的方法中打印请求到的数据,注意的是这里是response.body.toString()而不是OkHttp中的response.body.String()。
        Log.e("===","return:"+response.body().toString());
    }

    @Override
    public void onFailure(Call<String> call, Throwable t) {
        //在失败的方法中打印一个失败,作为标记就好了。
        Log.e("===","失败");
    }
});

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325528085&siteId=291194637