Retrofit Post请求使用

添加依赖

   //网络请求retroifit库
    implementation 'com.squareup.retrofit2:retrofit:2.1.0'

    // Gson 依赖
    implementation 'com.google.code.gson:gson:2.8.6'

    // Retrofit 的 Gson 转换器
    implementation 'com.squareup.retrofit2:converter-gson:2.9.0'

接口定义(最重要的)

public interface ApiServer {
    @POST("/api/getPictureUrl/")
    Call<BackInfo> PostData(@Body PicUrl picUrl);
}

这里这个post注解中可以理解为 可以拼接在你请求的地址后面 作用大概是为了降低耦合度

完整的接口

Call<BackInfo> PostData(@Body PicUrl picUrl);

这段代码之中BackInfo和PicUrl是数据处理类 Backinfo用于处理返回的数据 PicUrl用于请求参数

数据模型类

public class BackInfo {
    int code;
    boolean result;
    String msg;
    ArrayList<DataUrl> data;

    // Getter and Setter methods
    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public boolean isResult() {
        return result;
    }

    public void setResult(boolean result) {
        this.result = result;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public ArrayList<DataUrl> getData() {
        return data;
    }

    public void setData(ArrayList<DataUrl> data) {
        this.data = data;
    }


}
public class DataUrl {
    String name;
    String id;

    public String getUrl() {
        return name;
    }

    public void setUrl(String url) {
        this.name = url;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public DataUrl(String url, String id) {
        this.name = url;
        this.id = id;
    }

    @Override
    public String toString() {
        return "DataUrl{" +
                "url='" + name + '\'' +
                ", id='" + id + '\'' +
                '}';

实现类

public class RequestTest {
    private static String TAG = "Request";
    private BackInfo backInfo;
    private ApiServer apiServer;
    private Retrofit retrofit;
    
    //构建请求实例
    public RequestTest() {
        retrofit = new Retrofit.Builder()
                .baseUrl("https://pixelwallpapers.pics")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        apiServer = retrofit.create(ApiServer.class);
    }
    public void SendPost(){
        //参数赋值
        PicUrl picUrl = new PicUrl("type",much);
        //填入参数
        Call<BackInfo> call =apiServer.PostData(picUrl);
        //发起异步请求
        call.enqueue(new Callback<BackInfo>() {
            @Override
            public void onResponse(Call<BackInfo> call, Response<BackInfo> response) {
                JSONObject jsonObject = new JSONObject();

                if (response.isSuccessful()){
                    Log.e(TAG, "onResponse: "+response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BackInfo> call, Throwable t) {

            }
        });
    }

补充说明

官网翻译:https://www.jianshu.com/p/9bef0a89635a

和ok的使用方法差不多 感觉比较适合大项目 小项目ok够用