1.定义一个类,写根接口
//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/";
}
2.定义一个接口,写子接口
//1.定义接口(封装URL地址和数据请求)
//子接口存放的内容
public interface 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);
}
3.Activity
public class MainActivity extends AppCompatActivity {
@BindView(R.id.btn_requestGetData)
Button btnRequestGetData;
@BindView(R.id.et_pageCount)
EditText etPageCount;
@BindView(R.id.btn_requestGetDataWithQueryInfo)
Button btnRequestGetDataWithQueryInfo;
@BindView(R.id.btn_requestGetDataWithQueryMapInfo)
Button btnRequestGetDataWithQueryMapInfo;
@BindView(R.id.tv_content)
TextView tvContent;
@BindView(R.id.btn_requestGetDataWithQueryMapInfoUpdatePath)
Button btnRequestGetDataWithQueryMapInfoUpdatePath;
@BindView(R.id.btn_requestPostDataWithQueryInfo)
Button btnRequestPostDataWithQueryInfo;
@BindView(R.id.et_userName)
EditText etUserName;
@BindView(R.id.et_pwd)
EditText etPwd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
}
/**
* 以最基本的形式,进行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());
}
});
}
}