1. 程式人生 > >Retrofit網路框架封裝

Retrofit網路框架封裝

聽到 Retrofit 大傢伙應該都很熟悉吧 , 近期研究了一下  其實很簡單 , 本人對他進行了 一個 簡單的封裝 。

網路許可權不必多說了吧 

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

首頁  我們先來加入 Gradle中的配置

compile 'com.squareup.retrofit2:retrofit:2.0.0-beta2'
compile 'com.squareup.retrofit2:converter-scalars:2.0.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'

新增好許可權以後我們來看程式碼

package com.example.myapplication.http;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by 徐嘉健 on 2018/11/9.
 */

public class HttpManger {
    private static MediaType MEDIA_TYPE_PNG;

    public static void getMethod(String baseUrl, String url, final Callback<String> callback) {
        Retrofit retrofit = new Retrofit.Builder().baseUrl(baseUrl).addConverterFactory(ScalarsConverterFactory.create()).build();
        ProjectAPI projectAPI = retrofit.create(ProjectAPI.class);
        Call<String> call = projectAPI.getMethod(url);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                callback.onResponse(call, response);
            }
            @Override
            public void onFailure(Call<String> call, Throwable t) {
                callback.onFailure(call, t);
            }
        });
    }

    //post
    public static void postMethod(String baseUrl, String url, Map<String, String> map, final Callback<String> callback) {
//        //新增請求頭
//        OkHttpClient httpClient = null;
//        httpClient = new OkHttpClient.Builder()
//                .addInterceptor(new Interceptor() {
//                    @Override
//                    public okhttp3.Response intercept(Chain chain) throws IOException {
//                        Request newquest =chain.request().newBuilder().
//                                addHeader("Accept-Language","zh-CN,zh")
//                                .build();
//                        return chain.proceed(newquest);
//                    }
//                })
//                .build();
        Retrofit retrofit = new Retrofit.Builder().baseUrl(baseUrl)
                //  .client(httpClient)
                .addConverterFactory(ScalarsConverterFactory.create())
                .build();
        ProjectAPI projectAPI = retrofit.create(ProjectAPI.class);
        Call<String> call = projectAPI.postMethod(url, map);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                callback.onResponse(call, response);
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                callback.onFailure(call, t);
            }
        });
    }
}

此程式碼中有個介面是Retrofit自己本身的介面 用來回調資料 
package com.example.myapplication.http;

import android.os.Build;
import android.support.annotation.RequiresApi;

import com.example.myapplication.bean.MainBean;

import java.util.List;
import java.util.Map;

import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.FieldMap;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.Multipart;
import retrofit2.http.POST;
import retrofit2.http.Part;
import retrofit2.http.PartMap;
import retrofit2.http.Url;

/**
 * Created by 徐嘉健 on 2018/11/9.
 */

public interface ProjectAPI {
    @GET
    Call<String> getMethod(@Url String url);

    @FormUrlEncoded
    @POST
    Call<String> postMethod(@Url String url, @FieldMap Map<String, String> map);

}

這樣基本就Ok了 。 很簡單的一個操作吧  希望小夥伴們看完覺得不錯 幫忙點個贊!