1. Singleton Retrofit:
public class NetWork {
private static Retrofit retrofit;
/**Retrofit*/
public static Retrofit getRetrofit(){
if(retrofit==null){
Retrofit.Builder builder = new Retrofit.Builder();//Retrfit
// retrofit = builder.baseUrl("http://apis.juhe.cn/") //baseUrl
retrofit = builder.baseUrl("http://v.juhe.cn/") //baseUrl
.addConverterFactory(GsonConverterFactory.create())//Gson
.build();
}
return retrofit;
}
}
2. Encapsulation interface:
public interface NetInterface {
// @GET("mobile/get")
@GET("toutiao/index")
Call<Bean> getAddress(@Query("type") String type, @Query("key") String key);
}
3. Request data:
//Retrofit,
NetInterface netInterface = NetWork.getRetrofit().create(NetInterface.class);
netInterface.getAddress("15712101108","c727cca6fb450ea08b4b0fa220b72eb1")
.enqueue(new Callback<Bean>() {
@Override
public void onResponse(Call<Bean> call, Response<Bean> response) {
//
Bean bean = response.body();
Log.d(TAG, "1547= "+bean); // com.b.demo8.Bean@6a156c6
}
@Override
public void onFailure(Call<Bean> call, Throwable t) {
//
}
});