如果你对Retrofit 不了解请看本博主的
Retrofit一依赖于OkHttp 的请求库.了解了相关理论后再看
一.GET:
1.在URL中没有参数
//完整的url
//
http://result.eolinker.com/Xbiu2Ui13158320a01833d7edf4208fc506fe42578ca5c7?uri=data3
1.1定义请求接口
package
com.retro.demo;
import
okhttp3.ResponseBody;
import
retrofit2.Call;
import
retrofit2.http.GET;
public interface
EoApiInterface {
@GET(
"Xbiu2Ui13158320a01833d7edf4208fc506fe42578ca5c7?uri=data3"
)
//注意导包和泛型
Call contributorsBySimpleGetCall();
}
1.2调用
//
实例化
Retrofit
Retrofit retrofit =
new
Retrofit.Builder()
//baseUrl 为最基础的地址
.baseUrl(
"http://result.eolinker.com/"
)
.build();
//
调用接口
EoApiInterface eoApiInterface = retrofit.create(EoApiInterface.
class
);
Call requestBodyCall = eoApiInterface.contributorsBySimpleGetCall();
requestBodyCall.enqueue(
new
Callback() {
@Override
public void
onResponse(Call call, Response response) {
String s =
null
;
try
{
s = response.body().string();
}
catch
(IOException e) {
e.printStackTrace();
}
Log.
d
(
"wwwwww"
,s);
}
@Override
public void
onFailure(Call call, Throwable t) {
}
});
2.URL中有参数
//完整地址,为内部测试接口,练习请自行找接口
//
http://169.254.217.5:8080/bullking1/cart?userID=26
2.1定义请求接口
public interface
GetAgInterface {
@GET
(
"bullking1/cart?"
)
//
@Query关键字和GET连用 表示追加
//
userID key键
//uid values 值
//如果多个参数 不用考虑'&'
Call getCart(
@Query
(
"userID"
)String uid);
}
2.2调用
private void
getAr(String uid){
Retrofit retrofit =
new
Retrofit.Builder()
.baseUrl(
"http://169.254.217.5:8080/"
)
.build();
//
调用请求接口
GetAgInterface getAgInterface=retrofit.create(GetAgInterface.
class
);
Call call= getAgInterface.getCart(uid);
call.enqueue(
new
Callback() {
@Override
public void
onResponse(Call call, Response response) {
try
{
String s = response.body().string();
Log.
d
(
"GetAgActivity------"
,s);
}
catch
(IOException e) {
e.printStackTrace();
}
}
@Override
public void
onFailure(Call call, Throwable t) {
}
});
}
二.POST
2.1一般的post
//完整地址,为内部测试接口,练习请自行找接口
//
http://169.254.217.5:8080/"bullking1/login
//请求参数 name
// pwd
public interface
PostInter {
String
url
=
"http://169.254.217.5:8080/"
;
@POST
(
"bullking1/login"
)
//POST请求必须添加的注解 避免出现乱码
@FormUrlEncoded
//关键字@Field 用于一般的POST请求 封装参数
Call login(
@Field
(
"name"
) String name,
@Field
(
"pwd"
) String pwd);
}
//调用
private void
login(String name,String pwd){
Retrofit retrofit =
new
Retrofit.Builder().baseUrl(
"http://169.254.217.5:8080/"
).build();
PostInter postInter = retrofit.create(PostInter.
class
);
Call login = postInter.login(name, pwd);
login.enqueue(
new
Callback() {
@Override
public void
onResponse(Call call, Response response) {
try
{
Toast.
makeText
(PostActivity.
this
, response.body().string(), Toast.
LENGTH_SHORT
).show();
}
catch
(IOException e) {
e.printStackTrace();
}
}
@Override
public void
onFailure(Call call, Throwable t) {
}
});
}
2.2.POST 上传
/**
* http://192.168.0.104:8080/08web/FileUploadServlet
* post
* 方式实现上传头像
*/
//上传时必须加 @Multipart 注解
@Multipart
@POST("08web/FileUploadServlet")
//参数的封装用@Part注解
//参数类型MultipartBody.Part
Call upload(@Part MultipartBody.Part file);
调用
private void uploadPic() {
//创建retrofit
Retrofit retrofit = new Retrofit.Builder().baseUrl("http://192.168.0.104:8080/").build();
//这里采用的是Java的动态代理模式
RetrofitService retrofitService = retrofit.create(RetrofitService.class);
//拿到图片 file
File file = new File(Environment.getExternalStorageDirectory(), "/Pictures/Screenshots/a.jpg");
//封装到请求体里
RequestBody requestFile =
RequestBody.create(MediaType.parse("application/otcet-stream"), file);
//封装到 MultipartBody.Part 里
MultipartBody.Part body =
MultipartBody.Part.createFormData("file", file.getName(), requestFile);
final Call upload = retrofitService.upload(body);
upload.enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {
}
@Override
public void onFailure(Call call, Throwable t) {
}
});
}