Retrofit
A type-safe REST client for Android and Java
Android中非常有名的网络框架
官网 :http://square.github.io/retrofit/
参考项目: http://square.github.io/okhttp/
Android 示例项目: https://github.com/goodev/RetrofitDemo
Retrofit vs Volley
http://instructure.github.io/blog/2013/12/09/volley-vs-retrofit/
提取一张图片
//API
public interface ApiService {
@GET("/jobs")
public void getJobs(Callback<Data> callback);
}
//RestClient
public class RestClient {
private static final String BASE_URL = "https://yourapi.host.com";
private ApiService apiService;
public RestClient() {
Gson gson = new GsonBuilder()
.registerTypeAdapterFactory(new ItemTypeAdapterFactory())
.setDateFormat("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSS'Z'")
.create();
RestAdapter restAdapter = new RestAdapter.Builder()
.setLogLevel(RestAdapter.LogLevel.FULL)
.setEndpoint(BASE_URL)
.setConverter(new GsonConverter(gson))
.setRequestInterceptor(new SessionRequestInterceptor())
.setClient(new OkClient(MySSLTrust.getUnsafeOkHttpClient()))
.build();
apiService = restAdapter.create(ApiService.class);
}
public ApiService getApiService() {
return apiService;
}
}
<