1、Retrofit+Okhttp的基本使用
2016-12-01 01:49
453 查看
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请求①:
GET 请求②:
POST请求: (@POST中的参数是:与baseUrl组合形成请求url)
3、创建Retrofit对象
4、上面的String泛型可以改为实体类
在使用Gson数据的时候,字符串转换成实体类在这个网站上进行转换:http://www.jsonschema2pojo.org/
访问网址:的时候会有这种异常
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); }
GET 请求②:
public interface RequestServerInterface { @GET("xx网址后缀") 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泛型可以改为实体类
在使用Gson数据的时候,字符串转换成实体类在这个网站上进行转换:http://www.jsonschema2pojo.org/
访问网址:的时候会有这种异常
11-27 12:09:58.909 6031-6031/com.example.administrator.retrofit_ W/System.err: java.io.EOFException 11-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)
相关文章推荐
- OkHttp,Retrofit 1.x - 2.x 基本使用
- Retrofit+okhttp基本使用
- Retrofit+Rxjava+okhttp基本使用
- OkHttp,Retrofit 1.x - 2.x 基本使用
- OkHttp基本使用方式
- Okhttp插入日志及结合Retrofit使用
- Retrofit基本使用
- Retrofit的基本使用详解(一)
- 使用Retrofit和Okhttp实现网络缓存。无网读缓存,有网根据过期时间重新请求 (转)
- Retrofit 基本使用教程
- okhttp基本使用
- Android Retrofit+rxjava+okhttp请求网络的基本用法
- RxJava+Retrofit+OkHttp 懒人方式使用一
- Android Retrofit 2.0(二)使用教程OkHttp3 + Gson + RxJava
- OKHttp的基本使用
- Retrofit 基本使用教程
- Retrofit基本介绍和简单的使用方式
- Retrofit2+RxJava+OkHttp项目直接使用(简单快捷)
- OkHttp3.0的基本使用
- OkHttp基本使用(五)拦截器