build.gradle

compile group: 'com.squareup.retrofit', name: 'converter-gson', version: '2.0.0-beta2'

config

public class RestAdapter {    private final String API = "http://192.168.0.102:8080/";    private Retrofit retrofit;    public RestAdapter() {        // Creates the json object which will manage the information received        GsonBuilder builder = new GsonBuilder();        // Register an adapter to manage the date types as long values//        builder.registerTypeAdapter(Date.class, new JsonDeserializer() {//            public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {//                return new Date(json.getAsJsonPrimitive().getAsLong());//            }//        });////        builder.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");        builder.registerTypeAdapter(Date.class, new GsonUTCDateAdapter());        Gson gson = builder.create();        this.retrofit = new Retrofit.Builder()                .baseUrl(API)                .addConverterFactory(GsonConverterFactory.create(gson))                .build();    }    public Retrofit getRetrofit() {        return retrofit;    }}

api

public interface ApiService {    @GET("article/list")    Call getByDate(@Query("begin")String begin, @Query("end")String end);    @GET("article/tags")    Call> getTags();    @POST("article/{id}")    Call update(@Path("id") String id,@Body Article article);    @POST("article")    Call create(@Body Article article);    @DELETE("article/{id}")    Call delete(@Path("id") String id);}

创建接口实例

ApiService apiService = new RestAdapter().getRetrofit().create(ApiService.class);

之后正常调用即可。同步的话,直接execute,比如

Call call = apiService.getByDate(begin,end);        Response resp = null;        try {            resp = call.execute();        } catch (IOException e) {            e.printStackTrace();            return Collections.emptyList();        }

docs

  • Android Retrofit 2.0使用

更多相关文章

  1. Android与php服务器交互实例
  2. Android sd卡读取数据库实例代码
  3. 在Android中扫描wifi热点演示实例教程
  4. [置顶] [Android] AsyncTask使用实例---加载网络图片
  5. Activity与Fragment通过接口回调进行通信
  6. Android编程之文件的读写实例详解

随机推荐

  1. Android(安卓)给贪吃蛇游戏加背景音乐
  2. Android沉浸式状态栏、导航栏
  3. android:allowUndo
  4. Android中的Context
  5. android 取消标题,程序意外停止
  6. Android(安卓)无法查看外部依赖jar的源码
  7. Android(安卓)TextView中显示单行过长的
  8. Android中ProgressDialog的简单使用.
  9. ANDROID ADB工具使用
  10. android 中使文本(TextView 、button等可