Retrofit2.0使用

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 爲請求到的數據
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章