Retrofit網路架構

來源:互聯網
上載者:User

標籤:響應   2.4.1   internet   nts   compile   body   str   text   tle   

JDK 7以上 Android 2.3以上

自動實現json到javabean的裝換,是對OKHttp的增強版,底層封裝了Gson架構,但是必須new 一個Gson對象

主要掌握註解 @GET("home")  @Query("index")  @FormUrlEncoded  @Field("username")等

自己的思路步驟:

  1,建立retrofit對象,並設定retrofit的參數 和轉換的Gson

  2,提取工程所需要的網路要求方法 get( )  login( ) 等,到介面Api中,retrofit對介面進行處理

  3,retrofit 處理介面中(包括介面中的方法),得到請求對象

  4,編寫回調對象邏輯,callback

 

具體實現Demo :

    compile ‘com.squareup.retrofit2:retrofit:2.1.0‘    compile ‘com.squareup.retrofit2:converter-gson:2.1.0‘  //json到javaBean的轉換

配置許可權

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

MyApi 介面

public interface GoogleMarketApi {    //http://127.0.0.1:8090/home?index=0    //註解@GET:指定求方式 與請求路徑    //@Query是用來設定GET 請求參數變數名    //傳回值泛型是用來指定new Gson().fromJson(json,clz)    //指定Call 該方法就可以運行子線程,代表非同步請求    @GET("home")    Call<HomeData> getHomeData(                  @Query("index")                  String index);    //@QueryMap指定map集合為get請求參數    @GET("home")    Call<HomeData> getHomeData(            @QueryMap            HashMap<String,String> map);    //http://192.168.79.28:8080/webapi/post?username=itheima&password=123    //@POST標註當前方法 使用POST請求    //@Field用來標註post請求參數名    //@FormUrlEncoded與@Post配合使用,要求對post的參數進行網路編碼    @FormUrlEncoded    @POST("post")    Call<LoginData> login(@Field("username")String username,@Field("password") String pwd);    //    @FieldMap用來標註map為Post請求的參數    @FormUrlEncoded    @POST("post")    Call<LoginData> login(@FieldMap HashMap<String,String> params);}

 

MainActivity.java 中的方法

 public void request(View view) {        //步驟二。開始使用retrofit發送請求        //2.1.初始化架構  AlertDialog.Builder setTitle與  Retrofit.Builder比較 前者 快速建立對話  後者 快速初始化retrofit架構        String baseUrl="http://192.168.79.28:8080/webapi/";//retrofit要求以/結束,否則會產生錯誤        Retrofit retrofit = new Retrofit.Builder()                .baseUrl(baseUrl)//設定項目路徑 其它路徑都改成相對路徑 與ApiUrls   HOST    HOME=HOST+"/home"/                .addConverterFactory(GsonConverterFactory.create(new Gson()))//convert就是將json解析成javaBean                .build();//執行建立方法        //2.2.調用retrofit架構去讀到一個請求需要的全部參數,參數都是註解或者泛型來標註        GoogleMarketApi googleMarketApi = retrofit.create(GoogleMarketApi.class);//讀取請求方式 請求頁面 返回參數        //2.3.可以擷取一個非同步方法呼叫       // Call<LoginData> method = googleMarketApi.login("itheima", "123");        HashMap<String, String> params=new HashMap<>();        params.put("username","itheima");        params.put("password","123");        Call<LoginData> method = googleMarketApi.login(params);        //2.4.執行子線程,處理服務端返回資料 都是將資料傳給回調對象(空方法,給開發人員編寫商務邏輯,注意條件)        Callback<LoginData> callback=new Callback<LoginData>() {            //2.4.1.處理請求成功的商務邏輯            @Override            public void onResponse(Call<LoginData> call, Response<LoginData> response) {                //2.4.2.從響應裡面擷取解析後的javaBean                LoginData data = response.body();                if (data != null) {                    textView.setText(data.data);                }            }            //2.4.3.處理請求失敗的商務邏輯            @Override            public void onFailure(Call<LoginData> call, Throwable t) {                t.printStackTrace();                textView.setText(t.getMessage());            }        };        method.enqueue(callback);}

 

Retrofit網路架構

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.