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);
}
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)