Retrofit一GET,POST

如果你对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<ResponseBody> contributorsBySimpleGetCall();
}

1.2调用
// 实例化 Retrofit
Retrofit retrofit = new Retrofit.Builder()
//baseUrl 为最基础的地址
.baseUrl( "http://result.eolinker.com/" )
.build();

// 调用接口
EoApiInterface eoApiInterface = retrofit.create(EoApiInterface. class );
Call<ResponseBody> requestBodyCall = eoApiInterface.contributorsBySimpleGetCall();
requestBodyCall.enqueue( new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
String s = null ;
try {
s = response.body().string();
} catch (IOException e) {
e.printStackTrace();
}
Log. d ( "wwwwww" ,s);
}

@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {

}
});

2.URL中有参数
//完整地址,为内部测试接口,练习请自行找接口

2.1定义请求接口

public interface GetAgInterface {
@GET ( "bullking1/cart?" )
// @Query关键字和GET连用 表示追加
// userID key键
//uid values 值
//如果多个参数 不用考虑'&'
Call<ResponseBody> 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<ResponseBody> call= getAgInterface.getCart(uid);
call.enqueue( new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
try {
String s = response.body().string();
Log. d ( "GetAgActivity------" ,s);
} catch (IOException e) {
e.printStackTrace();
}
}

@Override
public void onFailure(Call<ResponseBody> 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<ResponseBody> 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<ResponseBody> login = postInter.login(name, pwd);
login.enqueue( new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
try {
Toast. makeText (PostActivity. this , response.body().string(), Toast. LENGTH_SHORT ).show();
} catch (IOException e) {
e.printStackTrace();
}
}

@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {

}
});
}


2.2.POST 上传
/**
* http://192.168.0.104:8080/08web/FileUploadServlet
* post
* 方式实现上传头像
*/
//上传时必须加 @Multipart 注解
@Multipart
@POST("08web/FileUploadServlet")
//参数的封装用@Part注解
//参数类型MultipartBody.Part
Call<UserInfo> 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<UserInfo> upload = retrofitService.upload(body);
upload.enqueue(new Callback<UserInfo>() {
@Override
public void onResponse(Call<UserInfo> call, Response<UserInfo> response) {

}

@Override
public void onFailure(Call<UserInfo> call, Throwable t) {

}
});


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值