添加权限
<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) { } });