Android Retrofit 入門教程

首先添加依賴

compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'//請求結果直接轉化為實體類,省略gson轉化

創(chuàng)建一個接口

// 完整地址: http://www.wuhaojun.com/api/android/customer?type=1
public interface CustomerService {
    @GET("/api/android/customer")//Get請求地址
    Call<Customer> getCustomer(@Query("type") int type);//定義參數(shù)type的當前是第幾頁 1,2,3 ...
}

請求方法

        String baseUrl = "http://www.wuhaojun.com/";//請求地址,固定的一部分
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())//請求結果直接轉化實體類
                .build();

        CustomerService movieService = retrofit.create(CustomerService.class);//創(chuàng)建對象
        Call<Customer> call = movieService.getCustomer(1);//傳遞請求參數(shù) 對應接口中的定義
        call.enqueue(new Callback<Customer>() {
            @Override
            public void onResponse(Call<Customer> call, Response<Customer> response) {
                Log.i(TAG, "onResponse: "+response.body().getMessage());//返回的就是實體類,不需要Gson轉換
            }

            @Override
            public void onFailure(Call<Customer> call, Throwable t) {
                Log.i(TAG, "onFailure: "+t.getMessage());
            }
        });

最后,別忘了添加網(wǎng)絡權限

<uses-permission android:name="android.permission.INTERNET" />

附加其他上傳類型

//post json
    @POST
    Call<String> reJson(@Url String url, @Body RequestBody body);

    HashMap<String, String> params = new HashMap<>();
    params.put("phone_num", "123");
    JSONObject json = new JSONObject(params);
    RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=utf-8"), json);//轉化類型


//post params
    @POST
    @FormUrlEncoded
    Call<String> reParams(@Url String url, @FieldMap Map<String, String> map);

    HashMap<String, String> hashMap = new HashMap<>();
    hashMap.put("user", "test");    
    
    
//post file map
    @POST
    @Multipart
    Call<String> reUploadFile(@Url String url, @PartMap Map<String, RequestBody> params);
    
    File file = new File("");
    Map<String, RequestBody> hashMap = new HashMap<>();
    hashMap.put("json", RequestBody.create(MediaType.parse("text/plain"), "jsonargs"));//鍵值對
    hashMap.put("file\"; filename=\"" + file.getName(), RequestBody.create(MediaType.parse("multipart/form-data"), file));//文件

大神tough1985文章:http://gank.io/post/56e80c2c677659311bed9841

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,523評論 25 708
  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,991評論 19 139
  • 最近在一邊學習iOS開發(fā),一邊在嘗試集成Vungle的SDK。 以下是一些有關Vungle SDK獎勵回調(diào)機制的詳...
    賬房先生2016閱讀 1,913評論 0 50
  • Efficient Usage 大多數(shù)介紹eclipse高效操作技巧的文章都是在介紹快捷鍵,本文也不例外!但如果僅...
    MagicBowen閱讀 3,880評論 4 11
  • 年華啊時光啊,青春啊愛情啊,突然就想這么喊喊,好像這么喊喊它們就又倏爾流淌過。 在某一個記不清的時間節(jié)點之前,特別...
    薄情的世界深情地活著閱讀 357評論 0 1