依赖:
//Retrofit网络请求框架
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
布局文件:
<Button
android:id="@+id/btn_requestGetData"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="以最简单的Get方式进行请求" />
<EditText
android:id="@+id/et_pageCount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入页数" />
<Button
android:id="@+id/btn_requestGetDataWithQueryInfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="以Get方式进行请求,需传入请求页数" />
<Button
android:id="@+id/btn_requestGetDataWithQueryMapInfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="以Get方式进行请求,需传入请求的键值对" />
<Button
android:id="@+id/btn_requestGetDataWithQueryMapInfoUpdatePath"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="以Get方式进行请求,需传入请求的键值对,以及传入文件夹路径" />
<EditText
android:id="@+id/et_userName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="用户名" />
<EditText
android:id="@+id/et_pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="密码" />
<Button
android:id="@+id/btn_requestPostDataWithQueryInfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="以Post方式进行请求" />
<TextView
android:id="@+id/tv_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="默认显示文字" />
**
Constant
**
//1.定义接口(封装URL地址和数据请求)
//常量类中,存放请求地址
public class Constant {
// 原始地址
// "http://m2.qiushibaike.com/article/list/latest?page=1";
//根地址,进行地址拆分,必须以/结尾
public static final String BASE_URL = "http://m2.qiushibaike.com/";
//维度电商请求http://mobile.bwstudent.com/small/user/v1/login
//根地址
public static final String MALL_BASE_URL = "http://mobile.bwstudent.com/";
}
**
ApiService
**
@GET("article/list/latest?page=1")
Call<ResponseBody> getResponseData();
@GET("article/list/latest?")
Call<ResponseBody> getResponseDataWithQueryInfo(@Query("page") String info);
@GET("article/list/latest?")
Call<ResponseBody> getResponseDataWithQueryMapInfo(@QueryMap Map<String, String> map);
@GET("article/list/{Alex}?")
Call<ResponseBody> getResponseDataWithQueryMapInfoUpdatePath(@Path("Alex") String path, @QueryMap Map<String, String> map);
@FormUrlEncoded
@POST("small/user/v1/login")
Call<ResponseBody> postResponseDataWithQueryInfo(@Field("phone") String phone, @Field("pwd") String pwd);
**
MainActivity
**
/**
* 以最基本的形式,进行Get请求
*/
private void requestGetData() {
//2.实例化Retrofit
//拼接根地址
//可以获取到根地址
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Constant.BASE_URL)
.build();
//3.通过Retrofit实例创建接口服务对象
//+子地址
ApiService service = retrofit.create(ApiService.class);
//4.接口服务对象调用接口中的方法,获取Call对象
Call<ResponseBody> call = service.getResponseData();
//5.Call对象执行请求(异步、同步请求)
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
try {
ResponseBody responseBody = response.body();
//响应数据
String responseData = responseBody.string();
tvContent.setText(responseData);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
String errorMessage = t.getMessage();
tvContent.setText(errorMessage);
}
});
}
private void requestGetDataWithQueryInfo() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Constant.BASE_URL)
.build();
ApiService service = retrofit.create(ApiService.class);
//获取输入框的页数
String pageCount = etPageCount.getText().toString();
Call<ResponseBody> call = service.getResponseDataWithQueryInfo(pageCount);
//执行请求
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
try {
String reponseData = response.body().string();
tvContent.setText(reponseData);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
String errorMessage = t.getMessage();
tvContent.setText(errorMessage);
}
});
}
private void requestGetDataWithQueryMapInfo() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Constant.BASE_URL)
.build();
ApiService service = retrofit.create(ApiService.class);
//创建所需键值对
Map<String, String> map = new HashMap<>();
//page=1
//输入框所获取的信息内容
String pageConut = etPageCount.getText().toString();
map.put("page", pageConut);
Call<ResponseBody> call = service.getResponseDataWithQueryMapInfo(map);
//执行请求
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
try {
String reposneData = response.body().string();
tvContent.setText(reposneData);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
tvContent.setText(t.getMessage());
}
});
}
private void requestGetDataWithQueryMapInfoUpdatePath() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Constant.BASE_URL)
.build();
ApiService apiService = retrofit.create(ApiService.class);
//所需键值对集合
HashMap<String, String> map = new HashMap<>();
String page = etPageCount.getText().toString();
map.put("page", page);
//所需内容路径
Call<ResponseBody> call = apiService.getResponseDataWithQueryMapInfoUpdatePath("latest", map);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
try {
String responseData = response.body().string();
tvContent.setText(responseData);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
tvContent.setText(t.getMessage());
}
});
}
@OnClick({R.id.btn_requestGetData, R.id.btn_requestGetDataWithQueryInfo, R.id.btn_requestGetDataWithQueryMapInfo, R.id.btn_requestGetDataWithQueryMapInfoUpdatePath, R.id.btn_requestPostDataWithQueryInfo})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.btn_requestGetData:
//以最简单的Get方式进行请求
requestGetData();
break;
case R.id.btn_requestGetDataWithQueryInfo:
//以Get方式进行请求,需传入请求页数
requestGetDataWithQueryInfo();
break;
case R.id.btn_requestGetDataWithQueryMapInfo:
//以Get方式进行请求,需传入请求的键值对
requestGetDataWithQueryMapInfo();
break;
case R.id.btn_requestGetDataWithQueryMapInfoUpdatePath:
//以Get方式进行请求,需传入请求的键值对,以及传入文件夹路径
requestGetDataWithQueryMapInfoUpdatePath();
break;
case R.id.btn_requestPostDataWithQueryInfo:
requestPostDataWithQueryInfo();
break;
}
}
private void requestPostDataWithQueryInfo() {
//根地址改动
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Constant.MALL_BASE_URL)
.build();
ApiService service = retrofit.create(ApiService.class);
//添加用户名、密码
String userName = etUserName.getText().toString();
String pwd = etPwd.getText().toString();
//添加到请求参数中
Call<ResponseBody> call = service.postResponseDataWithQueryInfo(userName,pwd);
//执行请求
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
try {
String responseData = response.body().string();
tvContent.setText(responseData);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
tvContent.setText(t.getMessage());
}
});
}
}