美文网首页
retrofit2.1(正式版)简单使用

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

作者: lhl_012 | 来源:发表于2016-04-11 13:40 被阅读550次

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.1(正式版)简单使用

      本文链接:https://www.haomeiwen.com/subject/ynrilttx.html