retrofit2.1(正式版)简单使用

1.gradle配置:

compile'com.squareup.retrofit2:retrofit:2.0.1'

compile'com.squareup.retrofit2:retrofit-adapters:2.0.1'

compile'com.squareup.retrofit2:converter-gson:2.0.1'// gson

------------------------------------------------

2.简单代码:

import retrofit2.Call;

import retrofit2.Callback;

import retrofit2.Response;

import retrofit2.Retrofit;

import retrofit2.converter.gson.GsonConverterFactory;

----------------------

ApiService service =newRetrofit.Builder()

.addConverterFactory(GsonConverterFactory.create())

.baseUrl("http://wthrcdn.etouch.cn/")

.build()

.create(ApiService.class);

Call newsBeanCall = service.getWeather("北京");

newsBeanCall.enqueue(newCallback() {

@Override

public voidonResponse(Call call, Response response) {

Log.d("vvv",response.body().toString());

}

@Override

public voidonFailure(Call call, Throwable t) {

Log.d("vvv","error");

}

});

--------------------------------------------------------

3.ApiService:

importretrofit2.Call;

importretrofit2.http.GET;

importretrofit2.http.Query;

----------------------

public interfaceApiService {

@GET("weather_mini")

Call getWeather(@Query("city") String type);

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 什么是Retrofit2 Retrofit是一个给Android和Java用的类型安全的HTTP客户端,它将网络请...
    不羁的木木阅读 1,180评论 0 49
  • 前言RxJava和Retrofit也火了一段时间了,不过最近一直在学习ReactNative和Node相关的姿势,...
    AFinalStone阅读 560评论 0 0
  • 原文链接:http://www.jianshu.com/p/a8b88c7fe831http://blog.csd...
    庸碌无为阅读 12,448评论 20 80
  • Retrofit项目Github主页:点击打开链接 Retrofit项目官方文档 :点击打开链接 1.设置 (1...
    千涯秋瑟阅读 619评论 2 3
  • 时间的可怕之处就在于它可以磨平一切。磨平的不是只有记忆,还有感觉,不管多浓烈,多不舍,总会削弱以至于消散。这是人的...
    单影猫阅读 975评论 0 0