除了gson转换器,还有其他的支持json的转换器,或者自定义转换器(不一定要转换对象,而是有时需要对http头中的错误码进行处理)
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>retrofit</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>converter-gson</artifactId>
<version>2.3.0</version>
</dependency>
package gaofeng.http.retrofit;
import java.util.List;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Path;
public interface GitHubService {
@GET("users/{user}/repos")
Call<List<Repo>> listRepos(@Path("user") String user);
}
package gaofeng.http.retrofit;
public class Repo {
private int id;
private String name;
private String full_name;
@Override
public String toString() {
return "Repo [id=" + id + ", name=" + name + ", full_name=" + full_name + "]";
}
}
package gaofeng.http.retrofit;
import java.io.IOException;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class Retrofit1 {
public static void main(String[] args) throws IOException {
Retrofit retrofit = new Retrofit.Builder().baseUrl("https://api.github.com/")
.addConverterFactory(GsonConverterFactory.create()).build();
GitHubService service = retrofit.create(GitHubService.class);
Call<List<Repo>> call = service.listRepos("ggaofengg");
//同步请求
// Response<List<Repo>> re = call.execute();
// System.err.println(re.body().get(0));
//异步请求
call.enqueue(new Callback<List<Repo>>() {
public void onResponse(Call<List<Repo>> call, Response<List<Repo>> response) {
System.err.println(response.code() +" "+ response.message());
System.err.println(response.body().size());
System.err.println(response.body().get(0));
}
public void onFailure(Call<List<Repo>> call, Throwable t) {
t.printStackTrace();
}
});
System.err.println("hehe");
}
}
Retrofit2.1学习:Call接口的使用
http://www.jianshu.com/p/05bbf3aa2269
public class Test {
public static void main(String[] args) throws IOException {
Retrofit retrofit = new Retrofit.Builder().baseUrl("http://127.0.0.1:5000")
.build();//不要转换器,用原始ResponseBody,当返回500错误时,也可以通过errorbody获取响应内容
RestfulClient s = retrofit.create(RestfulClient.class);
Call<ResponseBody> d = s.listRepos("999");
Response<ResponseBody> p= d.execute();
System.out.println(p.code());
System.out.println(p.message());
// System.out.println(p.errorBody().string());
System.out.println(p.body().string());
}
public static void main2(String[] args) throws IOException {
Retrofit retrofit = new Retrofit.Builder().baseUrl("http://127.0.0.1:5000")
.addConverterFactory(new Factory() {//自定义转换器
@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type,
Annotation[] annotations, Retrofit retrofit) {
return new Converter<ResponseBody, String>() {
public String convert(ResponseBody value) throws IOException {
return "value.string()";
}
};
}
}).build();
RestfulClient2 s = retrofit.create(RestfulClient2.class);
Call<String> d = s.listRepos("999");
Response<String> p= d.execute();
System.out.println(p.code());
System.out.println(p.message());
System.out.println(p.errorBody());
// System.out.println(p.body());
}
}
interface RestfulClient {
@GET("/app/")
Call<ResponseBody> listRepos(@Query("user") String user);
}
interface RestfulClient2 {
@GET("/app/")
Call<String> listRepos(@Query("user") String user);
}