首先要导依赖包:
compile 'com.squareup.retrofit2:retrofit:2.0.1' compile 'com.squareup.okio:okio:1.5.0' compile 'com.squareup.okhttp3:okhttp:3.2.0' compile 'com.squareup.retrofit2:converter-gson:2.0.1' compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
然后下面代码 是通过注解声明,来请求数据,代码如下:
public interface ApiService {
/**
* 无参get请求
* http://service.meiyinkeqiu.com/service/ads/cptj
*
* @return
*/
//通过注解声明请求方式
@GET("service/ads/cptj")
Call<UserInfo> getNoParmars();
/**
* 有参get请求
* http://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/1
*
* @return
*/
@GET("data/%E7%A6%8F%E5%88%A9/10/{page}")
Call<NewInfo> getHasParmars(@Path("page") int page);
/**
* http://www.93.gov.cn/93app/data.do?channelId=0&startNum=0
* http://www.93.gov.cn/93app/data.do
* channelId
* startNum
* 拼接 ? &
* 为主
*/
@GET("data.do")
Call<StoreInfo> getHasParmars2(@Query("channelId") int channelId, @Query("startNum") int startNum);
}
下面代码是封装的数据的前部分:
public class Api {
public static String BASE_URL="http://service.meiyinkeqiu.com/";
public static String NEW_URL="http://gank.io/api/";
public static String STORE_URL="http://www.93.gov.cn/93app/";
}
下面代码是在Mainactivity里的代码如下:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// getNoParmar();
// getHasParmar();
getHasParmar2();
}
private void getNoParmar() {
//创建HttpLoggingInterceptor日志拦截器
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
@Override
public void log(String message) {
Log.i("xxx", message);
}
});
//创建OkHttpClient
OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(httpLoggingInterceptor).build();
//创建Retrofit
Retrofit retrofit = new Retrofit.Builder().client(okHttpClient).addConverterFactory(GsonConverterFactory.create())
.baseUrl(Api.BASE_URL).build();
//通过动态代理获取代理对象
ApiService apiService = retrofit.create(ApiService.class);
//得到Call
Call<UserInfo> noParmars = apiService.getNoParmars();
noParmars.enqueue(new Callback<UserInfo>() {
//回调在主线程
@Override
public void onResponse(Call<UserInfo> call, Response<UserInfo> response) {
UserInfo userInfo = response.body();
List<List<String>> dropdown = userInfo.getDropdown();
List<String> list = dropdown.get(0);
String str = list.get(0);
Log.i("xxx", str);
}
@Override
public void onFailure(Call<UserInfo> call, Throwable t) {
}
});
}
private void getHasParmar() {
//创建HttpLoggingInterceptor日志拦截器
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
@Override
public void log(String message) {
Log.i("xxx", message);
}
});
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
//创建OkHttpClient
OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(httpLoggingInterceptor).build();
//创建Retrofit
Retrofit retrofit = new Retrofit.Builder().client(okHttpClient).addConverterFactory(GsonConverterFactory.create())
.baseUrl(Api.NEW_URL).build();
//通过动态代理获取代理对象
ApiService apiService = retrofit.create(ApiService.class);
//得到Call
Call<NewInfo> call = apiService.getHasParmars(1);
call.enqueue(new Callback<NewInfo>() {
@Override
public void onResponse(Call<NewInfo> call, Response<NewInfo> response) {
NewInfo newInfo = response.body();
List<NewInfo.ResultsBean> results = newInfo.getResults();
NewInfo.ResultsBean resultsBean = results.get(0);
String source = resultsBean.getSource();
Log.i("xxx", source);
}
@Override
public void onFailure(Call<NewInfo> call, Throwable t) {
}
});
}
private void getHasParmar2() {
//创建HttpLoggingInterceptor日志拦截器
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
@Override
public void log(String message) {
Log.i("xxx", message);
}
});
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
//创建OkHttpClient
OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(httpLoggingInterceptor).build();
//创建Retrofit
Retrofit retrofit = new Retrofit.Builder().client(okHttpClient).addConverterFactory(GsonConverterFactory.create())
.baseUrl(Api.STORE_URL).build();
//通过动态代理获取代理对象
ApiService apiService = retrofit.create(ApiService.class);
//得到Call
Call<StoreInfo> call = apiService.getHasParmars2(1,1);
call.enqueue(new Callback<StoreInfo>() {
@Override
public void onResponse(Call<StoreInfo> call, Response<StoreInfo> response) {
StoreInfo storeInfo = response.body();
List<StoreInfo.DataBean> data = storeInfo.getData();
String fromname = data.get(0).getFROMNAME();
Log.i("xxx",fromname);
}
@Override
public void onFailure(Call<StoreInfo> call, Throwable t) {
}
});
}
}