Android(安卓)Retrofit网络框架 (一)
16lz
2021-01-24
1、基础使用
a、Retrfit2的GET请求
第一步,配置Retrfit的基本配置
Retrofit retrofit = new Retrofit.Builder() .baseUrl("http://gc.ditu.aliyun.com/")//服务端共用的baseUrl .addConverterFactory(GsonConverterFactory.create())//如果将服务端返回的json格式转为JavaBean,需要添加这一行代码 .build(); Api api = retrofit.create(Api.class); Map map = new HashMap<>(); map.put("a","郑州市"); Call call = api.getUserinfoMap(map);//第一种map传参 //Call call = api.getUserinfo("郑州市");//第二种直接传参 //Call call = api.getUser();//第三种直接写死参数 call.enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { UserBean userBean = response.body(); Log.e("TAG", userBean.toString()); showToast(userBean.toString()); } @Override public void onFailure(Call call, Throwable t) { Log.e("TAG", t.toString()); showToast(t.toString()); } });
第二步配置接口地址
public interface Api { /** * 第一种GET传参写法(参数直接写死) * a : 参数名 * city : 传进来的值是参数值 */ @GET("geocoding") Call getUserinfo(@Query("a") String city); /** * 第二种GET传参写法(参数写活) */ @GET("geocoding?a=郑州市") Call getUser(); /** * 第三种GET传参方式(用map写活) * */ @GET("geocoding") Call getUserinfoMap(@QueryMap Map map);}
第三步添加Javabean
public class UserBean { /** * lon : 120.58531 * level : 2 * address : * cityName : * alevel : 4 * lat : 31.29888 */ private double lon; private int level; private String address; private String cityName; private int alevel; private double lat; public double getLon() { return lon; } public void setLon(double lon) { this.lon = lon; } public int getLevel() { return level; } public void setLevel(int level) { this.level = level; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getCityName() { return cityName; } public void setCityName(String cityName) { this.cityName = cityName; } public int getAlevel() { return alevel; } public void setAlevel(int alevel) { this.alevel = alevel; } public double getLat() { return lat; } public void setLat(double lat) { this.lat = lat; } @Override public String toString() { return "UserBean{" + "lon=" + lon + ", level=" + level + ", address='" + address + '\'' + ", cityName='" + cityName + '\'' + ", alevel=" + alevel + ", lat=" + lat + '}'; }}
2、Retrofit2的post使用
第一步配置Retrofit
Retrofit retrofit = new Retrofit.Builder() .baseUrl("http://fanyi.youdao.com/") .addConverterFactory(GsonConverterFactory.create()) .build(); Api api = retrofit.create(Api.class); Call beanCall = api.postInfo("你好"); beanCall.enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { showToast(response.body().getTranslateResult().get(0).get(0).toString()); } @Override public void onFailure(Call call, Throwable t) { Log.e("TAG", t.toString()); showToast(t.toString()); } });
第二步创建接口
/** * 第一种POST传参 */ @POST("translate?doctype=json&jsonversion=&type=&keyfrom=&model=&mid=&imei=&vendor=&screen=&ssid=&network=&abtest=") @FormUrlEncoded Call postInfo(@Field("i") String targetSentence);
3、form-data表单post提交文件携带参数上传
1)、创建接口
@POST("xxx")Observable postLog(@Body RequestBody body);
2、具体上传操作
Api api = RetrofitClint.getApi();File file = new File(file);RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM) .addFormDataPart("key1", "value1")//参数1 .addFormDataPart("key2", "value2")//参数2 .addFormDataPart("filedata", file.getName(), RequestBody.create(MediaType.parse("multipart/form-data"), file)) .build(); Observable observable = api.postLog(requestBody);//第二种直接传参 Subscription subscription = observable.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Subscriber() { @Override public void onCompleted() { } @Override public void onStart() { super.onStart(); } @Override public void onError(Throwable e) { Logger.d(TAG, "334-e:" + e.toString()); if (listener != null) { listener.onError(e.toString()); } } @Override public void onNext(LogBean bean) { if (bean != null) { Logger.d(TAG, "bean:" + bean.toString()); if (listener != null) { listener.onSuccess(bean); } } } });
更多相关文章
- Android中定义接口的方法
- Android(安卓)OTA升级包制作脚本详解(二,解压缩)
- AsyncTaskLoader使用方法
- android 带参数广播示例
- android中的回调
- android 设置对话框的宽度和高度
- Android错误日志转换脚本(mapping)
- Android(安卓)StagefrightPlayer调用流程
- Android返回数据给上一个活动-startActivityForResult