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