Search in sources :

Example 66 with retrofit2.http

use of retrofit2.http in project retrofit by square.

the class RetrofitTest method responseConverterFactoryNoMatchThrows.

@Test
public void responseConverterFactoryNoMatchThrows() {
    Type type = String.class;
    Annotation[] annotations = new Annotation[0];
    NonMatchingConverterFactory nonMatchingFactory = new NonMatchingConverterFactory();
    Retrofit retrofit = new Retrofit.Builder().baseUrl("http://example.com/").addConverterFactory(nonMatchingFactory).build();
    try {
        retrofit.responseBodyConverter(type, annotations);
        fail();
    } catch (IllegalArgumentException e) {
        assertThat(e).hasMessage("" + "Could not locate ResponseBody converter for class java.lang.String.\n" + "  Tried:\n" + "   * retrofit2.BuiltInConverters\n" + "   * retrofit2.helpers.NonMatchingConverterFactory");
    }
    assertThat(nonMatchingFactory.called).isTrue();
}
Also used : MediaType(okhttp3.MediaType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Annotation(java.lang.annotation.Annotation) NonMatchingConverterFactory(retrofit2.helpers.NonMatchingConverterFactory) Test(org.junit.Test)

Example 67 with retrofit2.http

use of retrofit2.http in project retrofit by square.

the class RetrofitTest method callFactoryThrowingPropagates.

@Test
public void callFactoryThrowingPropagates() {
    final RuntimeException cause = new RuntimeException("Broken!");
    okhttp3.Call.Factory callFactory = new okhttp3.Call.Factory() {

        @Override
        public okhttp3.Call newCall(Request request) {
            throw cause;
        }
    };
    Retrofit retrofit = new Retrofit.Builder().baseUrl("http://example.com/").callFactory(callFactory).build();
    server.enqueue(new MockResponse());
    CallMethod service = retrofit.create(CallMethod.class);
    Call<ResponseBody> call = service.getResponseBody();
    try {
        call.execute();
        fail();
    } catch (Exception e) {
        assertThat(e).isSameAs(cause);
    }
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Request(okhttp3.Request) ToStringConverterFactory(retrofit2.helpers.ToStringConverterFactory) NonMatchingCallAdapterFactory(retrofit2.helpers.NonMatchingCallAdapterFactory) DelegatingCallAdapterFactory(retrofit2.helpers.DelegatingCallAdapterFactory) NonMatchingConverterFactory(retrofit2.helpers.NonMatchingConverterFactory) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Example 68 with retrofit2.http

use of retrofit2.http in project retrofit by square.

the class RetrofitTest method callFactoryReturningNullThrows.

@Test
public void callFactoryReturningNullThrows() throws IOException {
    okhttp3.Call.Factory callFactory = new okhttp3.Call.Factory() {

        @Override
        public okhttp3.Call newCall(Request request) {
            return null;
        }
    };
    Retrofit retrofit = new Retrofit.Builder().baseUrl("http://example.com/").callFactory(callFactory).build();
    server.enqueue(new MockResponse());
    CallMethod service = retrofit.create(CallMethod.class);
    Call<ResponseBody> call = service.getResponseBody();
    try {
        call.execute();
        fail();
    } catch (NullPointerException e) {
        assertThat(e).hasMessage("Call.Factory returned null.");
    }
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Request(okhttp3.Request) ToStringConverterFactory(retrofit2.helpers.ToStringConverterFactory) NonMatchingCallAdapterFactory(retrofit2.helpers.NonMatchingCallAdapterFactory) DelegatingCallAdapterFactory(retrofit2.helpers.DelegatingCallAdapterFactory) NonMatchingConverterFactory(retrofit2.helpers.NonMatchingConverterFactory) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Example 69 with retrofit2.http

use of retrofit2.http in project retrofit by square.

the class RetrofitTest method responseConverterFactorySkippedNoMatchThrows.

@Test
public void responseConverterFactorySkippedNoMatchThrows() {
    Type type = String.class;
    Annotation[] annotations = new Annotation[0];
    NonMatchingConverterFactory nonMatchingFactory1 = new NonMatchingConverterFactory();
    NonMatchingConverterFactory nonMatchingFactory2 = new NonMatchingConverterFactory();
    Retrofit retrofit = new Retrofit.Builder().baseUrl("http://example.com/").addConverterFactory(nonMatchingFactory1).addConverterFactory(nonMatchingFactory2).build();
    try {
        retrofit.nextResponseBodyConverter(nonMatchingFactory1, type, annotations);
        fail();
    } catch (IllegalArgumentException e) {
        assertThat(e).hasMessage("" + "Could not locate ResponseBody converter for class java.lang.String.\n" + "  Skipped:\n" + "   * retrofit2.BuiltInConverters\n" + "   * retrofit2.helpers.NonMatchingConverterFactory\n" + "  Tried:\n" + "   * retrofit2.helpers.NonMatchingConverterFactory");
    }
    assertThat(nonMatchingFactory1.called).isFalse();
    assertThat(nonMatchingFactory2.called).isTrue();
}
Also used : MediaType(okhttp3.MediaType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Annotation(java.lang.annotation.Annotation) NonMatchingConverterFactory(retrofit2.helpers.NonMatchingConverterFactory) Test(org.junit.Test)

Example 70 with retrofit2.http

use of retrofit2.http in project GitTest by xiaoxige.

the class MainActivity method publicImg.

private void publicImg(final String path) throws Exception {
    new Thread(new Runnable() {

        @Override
        public void run() {
            final Retrofit retrofit = new Retrofit.Builder().baseUrl("http://116.196.92.172:4869/").addConverterFactory(new StringConverterFactory()).addConverterFactory(GsonConverterFactory.create()).build();
            final File file = new File(path);
            final UpImageApi upImageApi = retrofit.create(UpImageApi.class);
            RequestBody body = RequestBody.create(MediaType.parse("image/" + "png"), file);
            try {
                Call<ResponseBody> responseBodyCall = upImageApi.uploadImage(body);
                Response<ResponseBody> response = responseBodyCall.execute();
                String string = response.body().string();
                Log.e("TAG", "string = " + string);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }).start();
}
Also used : ResponseBody(okhttp3.ResponseBody) Retrofit(retrofit2.Retrofit) File(java.io.File) RequestBody(okhttp3.RequestBody)

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