Retrofit2

添加权限

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

导包

implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
implementation 'com.squareup.retrofit2:retrofit:2.3.0'


1.定义一个接口类

 **
 *   @Headers   添加请求头
     @Path         替换路径
     @Query    替代参数值,通常是结合get请求的
     @FormUrlEncoded   用表单数据提交
     @Field     替换参数值,是结合post请求的 
     @part
     @body 
     @FieldMap  键和值都不能为空,发送表单的请求
  */

//请求网络
@POST("user/getUserInfo")
Call<Bean> postservice(@Query("uid") int uid);


Retrofit retrofit=new Retrofit.Builder()
        .baseUrl("http://120.27.23.105/")
        //compile 'com.squareup.retrofit2:converter-gson:2.1.0' 不能配置谷歌的gson
        .addConverterFactory(GsonConverterFactory.create())---》gson包
        .build();
BlogService service = retrofit.create(BlogService.class);
Call<Bean> call = service.postservice(145);
call.enqueue(new Callback<Bean>() {
    @Override
    public void onResponse(Call<Bean> call, Response<Bean> response) {
        Toast.makeText(MainActivity.this, "请求成功", Toast.LENGTH_SHORT).show();
    }

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

    }
});


//上传图片 http://120.27.23.105/file/upload
 @Multipart @POST("file/upload")
Call<ResponseBody> uploadIcon(@Query("uid") int uid, @Part("file") RequestBody body);

File file=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/zw.jpg");  //图片地址
Retrofit retrofit=new Retrofit.Builder()
        .baseUrl("http://120.27.23.105/")
        .build();
BlogService service = retrofit.create(BlogService.class);
//文件MultipartBody  表单FormBody()
RequestBody body=new MultipartBody.Builder()
        .addFormDataPart("file","zw.jpg",MultipartBody.create(MediaType.parse("multipart/from-data"),file))
        .build();
Call<ResponseBody> call = service.uploadIcon(145, body);
call.enqueue(new Callback<ResponseBody>() {
    @Override
    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
        Toast.makeText(MainActivity.this, "成功", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onFailure(Call<ResponseBody> call, Throwable t) {
        Toast.makeText(MainActivity.this, "失败", Toast.LENGTH_SHORT).show();
    }
});

//post的多个参数请求
@FormUrlEncoded
@POST("user/getUserInfo")  //-->变换的路径
Call<Bean> dd(@FieldMap Map<String,String> map);

Retrofit retrofit=new Retrofit.Builder()
        .baseUrl("http://120.27.23.105/")
        .addConverterFactory(GsonConverterFactory.create())
        .build();
BlogService service = retrofit.create(BlogService.class);
Map<String,String> map=new HashMap<>();
map.put("uid",145+"");
Call<Bean> call = service.dd(map);
call.enqueue(new Callback<Bean>() {
    @Override
    public void onResponse(Call<Bean> call, Response<Bean> response) {
        Toast.makeText(MainActivity.this,"成功", Toast.LENGTH_SHORT).show();
    }
    @Override
    public void onFailure(Call<Bean> call, Throwable t) {
    }
});


//retrofit加Okhttp添加拦截器
implementation 'com.squareup.okhttp3:okhttp:3.9.0'

OkHttpClient build = new OkHttpClient.Builder()
        //添加拦截器
        .addNetworkInterceptor(new LogInterceptor())
        .build();

Retrofit retrofit=new Retrofit.Builder()
        .baseUrl("http://120.27.23.105/")
        //compile 'com.squareup.retrofit2:converter-gson:2.1.0' 不能配置谷歌的gson
        .addConverterFactory(GsonConverterFactory.create())
        .client(build)--》okhttp
        .build();
BlogService service = retrofit.create(BlogService.class);
RequestBody body=new MultipartBody.Builder()
        .addFormDataPart("uid",145+"")
        .build();
Call<Bean> call = service.postservice(body);
call.enqueue(new Callback<Bean>() {
    @Override
    public void onResponse(Call<Bean> call, Response<Bean> response) {
        Toast.makeText(MainActivity.this, "请求成功", Toast.LENGTH_SHORT).show();
    }

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

    }
});


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值