前提:
添加聯(lián)網(wǎng)權(quán)限
<uses-permission android:name="android.permission.INTERNET" />
添加依賴我衬,同時(shí)添加GSON數(shù)據(jù)解析器
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
implementation 'com.squareup.retrofit2:retrofit:2.7.1'
GET請(qǐng)求:
例:請(qǐng)求如下接口:
(1)地址:http://49.232.114.172:8080/yunying/qufumanage/getList.json
(2)請(qǐng)求方式:get
(3)請(qǐng)求參數(shù):無
(4)返回結(jié)果:
{
"code":200,
"msg":"success",
"list":["趙錢孫李","周吳鄭王","馮陳褚衛(wèi)","蔣沈韓楊","朱秦尤許","何呂施張"]
}
第一步:構(gòu)建Retrofit?
Retrofit retrofit = new Retrofit.Builder().baseUrl("http://49.232.114.172:8080/")
? ? ? ? ? ? ? ? .addConverterFactory(GsonConverterFactory.create())
? ? ? ? ? ? ? ? .build();
第二步:接口定義,Call回調(diào)里放數(shù)據(jù)對(duì)應(yīng)的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創(chuàng)建APIRetrofit代理對(duì)象
APIRetrofit call = retrofit.create(APIRetrofit.class);
第四步:調(diào)用接口
? ? ? ? Call<GetBean> beanCall = call.getBeanCall();
? ? ? ? beanCall.enqueue(new Callback<GetBean>() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onResponse(Call<GetBean> call, Response<GetBean> response) {
? ? ? ? ? ? ? ? //在獲取之前我們可以先判斷一下狀態(tài)
? ? ? ? ? ? ? ?//? 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請(qǐng)求:
例:請(qǐng)求如下接口:
(1) 地址:http://49.232.114.172:8080/yunying/qufumanage/login.php
(2)請(qǐng)求方式:post
(3)請(qǐng)求參數(shù):”username”: “root” ?“password”:”root123456” (FormUrlEncoded表單形式)
(4)返回結(jié)果:
?{
????"code":200,
????"message":"success",
}
第一步:構(gòu)建Retrofit
Retrofit retrofit = new Retrofit.Builder().baseUrl("http://49.232.114.172:8080/")
? ? ? ? ? ? ? ? .addConverterFactory(GsonConverterFactory.create())
? ? ? ? ? ? ? ? .build();
第二步:接口定義橄维,Call回調(diào)里放數(shù)據(jù)對(duì)應(yīng)的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創(chuàng)建APIRetrofit代理對(duì)象
APIRetrofit call = retrofit.create(APIRetrofit.class);
第四步:調(diào)用接口
call?.enqueue(new Callback() {
@Override
? ? public void onResponse(Call call, Response response) {
//在獲取之前我們可以先判斷一下狀態(tài)
? ? ? ? //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開始支持調(diào)用自定義
在APP gradle 的android 里配置
? compileOptions{
? ? ? ? sourceCompatibility? JavaVersion.VERSION_1_8
? ? ? ? targetCompatibility JavaVersion.VERSION_1_8
? ? }
//TODO 加個(gè)todo 有時(shí)間繼續(xù)完善
看完給個(gè)贊吧~
筆芯~~~