天天看點

Retrofit使用總結

附:Retrofit首頁及官方教程

使用首先要導入Retrofit的依賴:

compile 'com.squareup.retrofit2:retrofit:2.1.0' //Retrofit依賴
 compile 'com.squareup.retrofit2:converter-gson:2.1.0' //Retrofit資料轉換的依賴,可以直接将json轉化為實體類
           

在建立項目并建立依賴之後就可以開始使用Retrofit了。

建立一個用于發送請求的Service:

這裡使用聚合資料提供的接口來擷取資料:請求位址:http://op.juhe.cn/onebox/weather/query

請求參數:cityname=%E5%8C%97%E4%BA%AC&dtype=json&key=19f4708ebed559eac920bbaa51b24a12

請求方式:GET

這裡邊用到了WeatherInform這個JavaBean,可以使用AS的GsonFormat将json資料轉換成對應的Java類:

(因為json轉成的Java類太長,放在檔案中供下載下傳檢視)

WeatherInform.java類下載下傳位址

初始化Retrofit:

package com.wei.retrofitdemo.Interface;

import com.wei.retrofitdemo.JavaBean.WeatherInform;

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;

/**
 * Created by WQC on 2016/7/15.
 */
public interface WeatherService {
     //用于指定請求的方式
    @GET("weather/query")
    //具體的請求回調的方法,可以使用@Query注解來添加url中的參數
    Call<WeatherInform> getWeather(@Query("cityname") String cityname, @Query("dtype") String dtype, @Query("key") String key);
}
           

請求的Url

  • path 是相對路徑,baseUrl 是目錄形式:

    path = “path”,baseUrl = “URL:http://host:port/a/b/”

    Url = “URL:http://host:port/a/b/path”

以本文使用的URL為例(使用的是第二種方式):

path : “weather/query”

baseUrl:”http://op.juhe.cn/onebox/”

是以最終的Url就是:“http://op.juhe.cn/onebox/weather/query“

執行個體化一個天氣請求的執行個體:

mWeatherService = mRetrofit.create(WeatherService.class);
           

在需要的時候請求資料:

1. 異步:

mWeatherCall = mWeatherService.getWeather("北京","json","19f4708ebed559eac920bbaa51b24a12");
        mWeatherCall.enqueue(new Callback<WeatherInform>() {
            @Override
            public void onResponse(Call<WeatherInform> call, Response<WeatherInform> response) {
                if (response != null) {
                    WeatherInform weatherInform = response.body();
                    if (weatherInform != null) {
                        Log.i(TAG, "onResponse: " + weatherInform.getReason());
                    }
                }else{
                    Log.i(TAG, "onResponse: response is null");

                }

            }

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

2. 同步

try {
            Response<WeatherInform> weatherInform = mWeatherCall.execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
           

以上就是Retrofit的簡單使用方法,他使用okhttp作為底層網絡請求方式,在上層進行封裝,使用注解的方式來使用。Retrofit還可以配合RxJava,RxAndroid來使用,提供更加簡潔的網絡請求方式。

  相關文章推薦

  • 上一篇《Android開發藝術探索》--Android中的IPC機制
  • 下一篇Volley源碼解讀(上)
  • • RxJava+Retrofit+OkHttp深入淺出-終極封裝六特殊篇(變種String替換Gson自由擴充)
  • • retrofit2 使用完全解析(1)
  • • 學習筆記——Retrofit2.0的簡單使用
  • • Retrofit源碼分析以及MVP架構封裝使用
  • • 學會Retrofit+OkHttp+RxAndroid三劍客的使用,讓自己緊跟Android潮流的步伐
  • • RxJava+Retrofit+OkHttp深入淺出-終極封裝二(網絡請求)
  • • Retrofit2 完全解析 探索與okhttp之間的關系
  • • Android 使用RxJava+Retrofit +Realm 組合加載資料 <讀取緩存 顯示 請求網絡資料 緩存最新資料 更新界面>(一)
  • • Android MVP+RXJava+Retrofit架構的初步建構
  • • Retrofit 2.0 超能實踐(四),完成大檔案斷點下載下傳