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 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 call = service.postservice(145);
call.enqueue(new Callback() {
    @Override
    public void onResponse(Call call, Response response) {
        Toast.makeText(MainActivity.this, "请求成功", Toast.LENGTH_SHORT).show();
    }

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

    }
});


//上传图片 http://120.27.23.105/file/upload
  @Multipart @POST( "file/upload")
Call 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 call = service.uploadIcon(145, body);
call.enqueue(new Callback() {
    @Override
    public void onResponse(Call call, Response response) {
        Toast.makeText(MainActivity.this, "成功", Toast.LENGTH_SHORT).show();
    }

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

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

Retrofit retrofit=new Retrofit.Builder()
        .baseUrl("http://120.27.23.105/")
        .addConverterFactory(GsonConverterFactory.create())
        .build();
BlogService service = retrofit.create(BlogService.class);
Map map=new HashMap<>();
map.put("uid",145+"");
Call call = service.dd(map);
call.enqueue(new Callback() {
    @Override
    public void onResponse(Call call, Response response) {
        Toast.makeText(MainActivity.this,"成功", Toast.LENGTH_SHORT).show();
    }
    @Override
    public void onFailure(Call 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 call = service.postservice(body);
call.enqueue(new Callback() {
    @Override
    public void onResponse(Call call, Response response) {
        Toast.makeText(MainActivity.this, "请求成功", Toast.LENGTH_SHORT).show();
    }

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

    }
});


 

你可能感兴趣的:(Retrofit2)