听到 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了 。 很简单的一个操作吧 希望小伙伴们看完觉得不错 帮忙点个赞!