use of retrofit2.http in project CustomViews by AndroidStudy233.
the class RetrofitActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_retrofit);
Retrofit retrofit = getRetrofit("http://wthrcdn.etouch.cn");
rxRetrofit(retrofit);
callRetrofit(retrofit);
rxRetroMap(retrofit);
rxRetrofitFlatMap(retrofit);
}
use of retrofit2.http in project CustomViews by AndroidStudy233.
the class RetrofitTest1 method main.
public static void main(String[] args) {
Retrofit retrofit = getRetrofit("http://wthrcdn.etouch.cn");
WeatherApi weatherApi = retrofit.create(WeatherApi.class);
weatherApi.getCityWeather("深圳").subscribeOn(Schedulers.io()).observeOn(Schedulers.computation()).subscribe(weatherBean -> {
System.out.print("onNext");
});
}
use of retrofit2.http in project AndroidStudy by tinggengyan.
the class RetrofitMainActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_retrofit);
this.retrofitTextView = (TextView) findViewById(R.id.retrofitTextView);
// 1. 创建Retrofit2的实例,并设置BaseUrl和Gson转换。
Retrofit retrofit = new Retrofit.Builder().baseUrl(API).addConverterFactory(GsonConverterFactory.create()).client(new OkHttpClient()).build();
// 2. 创建请求服务,并为网络请求方法设置参数
GitHubService gitHubService = retrofit.create(GitHubService.class);
// Call是Retrofit中重要的一个概念,代表被封装成单个请求/响应的交互行为
final Call<List<Contributor>> call = gitHubService.contributors("square", "retrofit");
// 3. 请求网络,并获取响应
Observable<String> observable = Observable.create(new Observable.OnSubscribe<String>() {
@Override
public void call(Subscriber<? super String> subscriber) {
try {
// execute(同步)或者enqueue(异步)方法,发送请求到网络服务器,并返回一个响应(Response)。
Response<List<Contributor>> response = call.execute();
subscriber.onNext(response.body().toString());
// 因为call只能被执行一次,可以clone一个,再重新执行
Call<List<Contributor>> cloneCall = call.clone();
cloneCall.enqueue(new Callback<List<Contributor>>() {
@Override
public void onResponse(Call<List<Contributor>> call, Response<List<Contributor>> response) {
Log.e(TAG, "onResponse:" + response.body().toString());
}
@Override
public void onFailure(Call<List<Contributor>> call, Throwable t) {
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
});
Action1<String> onNext = new Action1<String>() {
@Override
public void call(String o) {
retrofitTextView.setText(o);
}
};
observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(onNext);
}
use of retrofit2.http in project AndroidStudy by tinggengyan.
the class RetrofitRxJavaActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_retrofit);
this.retrofitTextView = (TextView) findViewById(R.id.retrofitTextView);
// 1. 创建Retrofit2的实例,并设置BaseUrl和Gson转换。
Retrofit retrofit = new Retrofit.Builder().baseUrl(API).addConverterFactory(GsonConverterFactory.create()).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).client(new OkHttpClient()).build();
// 2. 创建请求服务,并为网络请求方法设置参数
GitHubService gitHubService = retrofit.create(GitHubService.class);
// Call是Retrofit中重要的一个概念,代表被封装成单个请求/响应的交互行为
Observable<List<Contributor>> listObservable = gitHubService.contributorsObservable("square", "retrofit");
listObservable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<List<Contributor>>() {
@Override
public void call(List<Contributor> contributors) {
retrofitTextView.setText(contributors.get(0).toString());
}
});
}
use of retrofit2.http in project retrofit by square.
the class RequestBuilderTest method queryNameParamOptionalOmitsQuery.
@Test
public void queryNameParamOptionalOmitsQuery() {
class Example {
//
@GET("/foo/bar/")
Call<ResponseBody> method(@QueryName String ping) {
return null;
}
}
Request request = buildRequest(Example.class, new Object[] { null });
assertThat(request.url().toString()).isEqualTo("http://example.com/foo/bar/");
}
Aggregations