Search in sources :

Example 81 with retrofit2.http

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);
}
Also used : Retrofit(retrofit2.Retrofit)

Example 82 with retrofit2.http

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");
    });
}
Also used : Retrofit(retrofit2.Retrofit)

Example 83 with retrofit2.http

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);
}
Also used : Call(retrofit2.Call) OkHttpClient(okhttp3.OkHttpClient) Action1(rx.functions.Action1) IOException(java.io.IOException) Observable(rx.Observable) Response(retrofit2.Response) Retrofit(retrofit2.Retrofit) Callback(retrofit2.Callback) List(java.util.List)

Example 84 with retrofit2.http

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());
        }
    });
}
Also used : Retrofit(retrofit2.Retrofit) OkHttpClient(okhttp3.OkHttpClient) List(java.util.List)

Example 85 with retrofit2.http

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/");
}
Also used : Request(okhttp3.Request) QueryName(retrofit2.http.QueryName) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)82 ResponseBody (okhttp3.ResponseBody)76 Request (okhttp3.Request)72 Retrofit (retrofit2.Retrofit)39 OkHttpClient (okhttp3.OkHttpClient)28 RequestBody (okhttp3.RequestBody)22 IOException (java.io.IOException)19 Query (retrofit2.http.Query)15 MultipartBody (okhttp3.MultipartBody)12 Buffer (okio.Buffer)12 Path (retrofit2.http.Path)12 Interceptor (okhttp3.Interceptor)11 HttpUrl (okhttp3.HttpUrl)10 Response (okhttp3.Response)10 HashMap (java.util.HashMap)9 LinkedHashMap (java.util.LinkedHashMap)9 Response (retrofit2.Response)9 Part (retrofit2.http.Part)9 List (java.util.List)8 QueryMap (retrofit2.http.QueryMap)8