美文网首页
Android Retrofit的使用

Android Retrofit的使用

作者: mage1160 | 来源:发表于2021-10-29 15:54 被阅读0次

前提:

添加联网权限

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

添加依赖,同时添加GSON数据解析器

implementation 'com.squareup.retrofit2:converter-gson:2.4.0'

implementation 'com.squareup.retrofit2:retrofit:2.7.1'

GET请求:

例:请求如下接口:

(1)地址:http://49.232.114.172:8080/yunying/qufumanage/getList.json

(2)请求方式:get

(3)请求参数:无

(4)返回结果:

{

"code":200,

"msg":"success",

"list":["赵钱孙李","周吴郑王","冯陈褚卫","蒋沈韩杨","朱秦尤许","何吕施张"]

}

第一步:构建Retrofit 

Retrofit retrofit = new Retrofit.Builder().baseUrl("http://49.232.114.172:8080/")

                .addConverterFactory(GsonConverterFactory.create())

                .build();

第二步:接口定义,Call回调里放数据对应的Bean

public interface APIRetrofit {

  @GET ("/yunying/qufumanage/getList.json")

  Call<GetBean> getBeanCall();

}

public class GetBean {

private int code;

private Stringmsg;

private Listlist;

public int getCode() {

return code;

}

public void setCode(int code) {

this.code = code;

}

public String getMsg() {

return msg;

}

public void setMsg(String msg) {

this.msg = msg;

}

public List getList() {

return list;

}

public void setList(List list) {

this.list = list;

}

}

第三步:利用retrofit创建APIRetrofit代理对象

APIRetrofit call = retrofit.create(APIRetrofit.class);

第四步:调用接口

        Call<GetBean> beanCall = call.getBeanCall();

        beanCall.enqueue(new Callback<GetBean>() {

            @Override

            public void onResponse(Call<GetBean> call, Response<GetBean> response) {

                //在获取之前我们可以先判断一下状态

               //  if(response.code()== HttpURLConnection.HTTP_OK)

                GetBean body = response.body();

                Log.d("TAG",body.getMsg());

                Log.d("TAG",body.getCode()+"");

            }

            @Override

            public void onFailure(Call<GetBean> call, Throwable t) {

            }

        });

POST请求:

例:请求如下接口:

(1) 地址:http://49.232.114.172:8080/yunying/qufumanage/login.php

(2)请求方式:post

(3)请求参数:”username”: “root”  “password”:”root123456” (FormUrlEncoded表单形式)

(4)返回结果:

 {

    "code":200,

    "message":"success",

}

第一步:构建Retrofit

Retrofit retrofit = new Retrofit.Builder().baseUrl("http://49.232.114.172:8080/")

                .addConverterFactory(GsonConverterFactory.create())

                .build();

第二步:接口定义,Call回调里放数据对应的Bean

public interface APIRetrofit {

@FormUrlEncoded

@POST ("/yunying/qufumanage/login.php")

Call<LoginBean> getLogin(@Field("username") String username,@Field("password") String password);

}

public class LoginBean {

private int code;

private Stringmessage;

public int getCode() {

return code;

}

public void setCode(int code) {

this.code = code;

}

public String getMessage() {

return message;

}

public void setMessage(String message) {

this.message = message;

}

}

第三步:利用retrofit创建APIRetrofit代理对象

APIRetrofit call = retrofit.create(APIRetrofit.class);

第四步:调用接口

call .enqueue(new Callback() {

@Override

    public void onResponse(Call call, Response response) {

//在获取之前我们可以先判断一下状态

        //if(response.code()== HttpURLConnection.HTTP_OK)

        LoginBean body = response.body();

        Log.d("TAG",body.getMessage());

        Log.d("TAG",body.getCode()+"");

}

@Override

    public void onFailure(Call call, Throwable t) {

}

});

问题:

Error: Invoke-customs are only supported starting with Android O (--min-api 26)

Stack trace:

com.android.tools.r8.a: Invoke-customs are only supported starting with Android O (--min-api 26)

at com.android.tools.r8.dex.r.a(:289)

at com.android.tools.r8.dex.r.a(:98)

at com.android.tools.r8.dex.r.b(:188)

at com.android.tools.r8.dex.b.a(:63)

at com.google.commoncute(LoadExecutionStateStep.java:40)

仅从Android O开始支持调用自定义

在APP gradle 的android 里配置

  compileOptions{

        sourceCompatibility  JavaVersion.VERSION_1_8

        targetCompatibility JavaVersion.VERSION_1_8

    }

//TODO 加个todo 有时间继续完善

看完给个赞吧~

笔芯~~~

相关文章

网友评论

      本文标题:Android Retrofit的使用

      本文链接:https://www.haomeiwen.com/subject/nnzpcqtx.html