1.build.gradle 裏面加入依賴包:compile ‘com.squareup.retrofit2:retrofit:2.3.0’
2.新建Java Interface
請求url = http://baidu.com/search?key=圖片
public interface APIInterface {
@GET("search")
Call<ResponseBody> search(@QueryMap Map<String, String> map,
@Query("key") String key);
}
3.加載數據
.加載數據
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(“http://baidu.com/”)
.addConverterFactory(GsonConverterFactory.create()).build();
APIInterface pApiService = retrofit.create(APIInterface.class);
Call<ResponseBody> call = null;
call = pApiService.search(map,“圖片”);//map或list 來存放數據
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> arg0,
Response<ResponseBody> response) {
ResponseBody body = response.body();//body 爲請求到的數據
}