Retrofit+okhttp基本使用
来源:互联网 发布:米桶什么牌子好 知乎 编辑:程序博客网 时间:2024/05/17 03:08
1、导入必要jar包
compile ‘com.squareup.retrofit2:retrofit:2.0.0-beta4’//Retrofit2所需要的包
compile ‘com.squareup.retrofit2:converter-gson:2.0.0-beta4’//ConverterFactory的Gson依赖包
compile ‘com.squareup.retrofit2:converter-scalars:2.0.0-beta4’//ConverterFactory的String依赖包
compile ‘com.google.code.gson:gson:2.2.4’ //gson解析依赖jar包
compile ‘com.squareup.okhttp3:okhttp:3.4.2’ //okhttp 的依赖包
compile ‘com.squareup.okio:okio:1.11.0’ //okhttp 的依赖包
2、创建请求接口
GET请求:
public interface RequestServerInterface { @GET("{ie}{oe}{wd}{tn}{ch}") Call<String> getString(@Path("ie") String ie, @Path("oe") String oe, @Path("wd") String wd, @Path("tn") String tn, @Path("ch") String ch);}
POST请求: (@POST中的参数是:与baseUrl组合形成请求url)
public interface RequestServerInterface { @POST("/xxx") Call<String> getString(@Query("username" String username),@Query("password" String password));}
3、创建Retrofit对象
Retrofit retrofit = new Retrofit.Builder() //要以/结尾 .baseUrl("https://www.baidu.com/") //字符串返回值 .addConverterFactory(ScalarsConverterFactory.create()) //Json实体类返回值 .addConverterFactory(GsonConverterFactory.create()) .build(); RequestServerInterface serverInterface = retrofit.create(RequestServerInterface.class); Call<String> call = serverInterface.getString("utf8", "utf8", "Android开发", "98012088_5_dg", "1"); call.enqueue(new Callback<String>() { @Override public void onResponse(Call<String> call, Response<String> response) { Log.i(TAG, "sucess:" + response.body().toString()); } @Override public void onFailure(Call<String> call, Throwable t) { t.printStackTrace(); } });
4、上面的String泛型可以改为实体类
访问网址:的时候会有这种异常
11-27 12:09:58.909 6031-6031/com.example.administrator.retrofit_ W/System.err: java.io.EOFException11-27 12:09:58.909 6031-6031/com.example.administrator.retrofit_ W/System.err: at okio.RealBufferedSource.require(RealBufferedSource.java:59)11-27 12:09:58.909 6031-6031/com.example.administrator.retrofit_ W/System.err: at okio.RealBufferedSource.readIntLe(RealBufferedSource.java:251)11-27 12:09:58.909 6031-6031/com.example.administrator.retrofit_ W/System.err: at okio.GzipSource.consumeTrailer(GzipSource.java:173)11-27 12:09:58.909 6031-6031/com.example.administrator.retrofit_ W/System.err: at okio.GzipSource.read(GzipSource.java:92)11-27 12:09:58.909 6031-6031/com.example.administrator.retrofit_ W/System.err: at okio.RealBufferedSource.read(RealBufferedSource.java:45)11-27 12:09:58.909 6031-6031/com.example.administrator.retrofit_ W/System.err: at okio.ForwardingSource.read(ForwardingSource.java:35)11-27 12:09:58.910 6031-6031/com.example.administrator.retrofit_ W/System.err: at retrofit2.OkHttpCall$ExceptionCatchingRequestBody$1.read(OkHttpCall.java:281)11-27 12:09:58.910 6031-6031/com.example.administrator.retrofit_ W/System.err: at okio.Buffer.writeAll(Buffer.java:996)11-27 12:09:58.910 6031-6031/com.example.administrator.retrofit_ W/System.err: at okio.RealBufferedSource.readByteArray(RealBufferedSource.java:106)11-27 12:09:58.910 6031-6031/com.example.administrator.retrofit_ W/System.err: at okhttp3.ResponseBody.bytes(ResponseBody.java:128)11-27 12:09:58.910 6031-6031/com.example.administrator.retrofit_ W/System.err: at okhttp3.ResponseBody.string(ResponseBody.java:154)11-27 12:09:58.910 6031-6031/com.example.administrator.retrofit_ W/System.err: at retrofit2.converter.scalars.ScalarResponseBodyConverters$StringResponseBodyConverter.convert(ScalarResponseBodyConverters.java:30)11-27 12:09:58.910 6031-6031/com.example.administrator.retrofit_ W/System.err: at retrofit2.converter.scalars.ScalarResponseBodyConverters$StringResponseBodyConverter.convert(ScalarResponseBodyConverters.java:26)11-27 12:09:58.910 6031-6031/com.example.administrator.retrofit_ W/System.err: at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:213)11-27 12:09:58.910 6031-6031/com.example.administrator.retrofit_ W/System.err: at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:109)
0 0
- Retrofit+okhttp基本使用
- Retrofit+Rxjava+okhttp基本使用
- 1、Retrofit+Okhttp的基本使用
- android:retrofit+OKHttp使用
- Retrofit okhttp使用
- OkHttp,Retrofit 1.x - 2.x 基本使用
- Retrofit、RxJava和OkHttp使用
- 【Retrofit 】Retrofit 基本使用教程
- OKhttp+Retrofit
- 在安卓上使用RxJava Retrofit OKHttp GSON
- Retrofit使用OkHttp保存和添加cookie
- Retrofit和okhttp的了解和使用
- RxJava+Retrofit+OkHttp 懒人方式使用一
- RxJava+Retrofit+OkHttp 懒人方式使用一
- Android使用Retrofit+OkHttp实现网络请求
- Okhttp插入日志及结合Retrofit使用
- 如何使用OkHttp/Retrofit重试HTTP请求
- Retrofit 2 okhttp 3 使用笔记
- 区间和问题
- 如何切换fragement,不重新实例化
- CentOS 时间设置 时区设置
- 浅谈一下我所看到的独立游戏存在的问题---文/疯王子
- Ajax的使用
- Retrofit+okhttp基本使用
- 深入理解Android之Java虚拟机Dalvik
- Eclipse 安装Subclipse,获取spring源码
- Could not use external editor to fetch log message; consider setting the $SVN_EDITOR environment va
- Visual C++ 2015 中的重大更改
- Markov Model 马可夫模型 & Hidden Markov Model 隐马可夫模型
- Yii2.0 探究三 :用户登录机制
- 10种检测Python程序运行时间、CPU和内存占用的方法
- 什么情况下会导致内存泄露