Search in sources :

Example 1 with Body

use of retrofit2.http.Body in project cw-omnibus by commonsguy.

the class DownloadCheckService method getUpdateUrl.

private String getUpdateUrl() throws IOException {
    Retrofit retrofit = new Retrofit.Builder().baseUrl("https://commonsware.com").addConverterFactory(GsonConverterFactory.create()).build();
    BookUpdateInterface updateInterface = retrofit.create(BookUpdateInterface.class);
    BookUpdateInfo info = updateInterface.update().execute().body();
    if (info.updatedOn.compareTo(OUR_BOOK_DATE) > 0) {
        return (info.updateUrl);
    }
    return (null);
}
Also used : Retrofit(retrofit2.Retrofit)

Example 2 with Body

use of retrofit2.http.Body in project muzei by romannurik.

the class FiveHundredPxExampleArtSource method onTryUpdate.

@Override
protected void onTryUpdate(@UpdateReason int reason) throws RetryException {
    String currentToken = (getCurrentArtwork() != null) ? getCurrentArtwork().getToken() : null;
    OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(new Interceptor() {

        @Override
        public Response intercept(final Chain chain) throws IOException {
            Request request = chain.request();
            HttpUrl url = request.url().newBuilder().addQueryParameter("consumer_key", Config.CONSUMER_KEY).build();
            request = request.newBuilder().url(url).build();
            return chain.proceed(request);
        }
    }).build();
    Retrofit retrofit = new Retrofit.Builder().baseUrl("https://api.500px.com/").client(okHttpClient).addConverterFactory(GsonConverterFactory.create()).build();
    FiveHundredPxService service = retrofit.create(FiveHundredPxService.class);
    PhotosResponse response;
    try {
        response = service.getPopularPhotos().execute().body();
    } catch (IOException e) {
        Log.w(TAG, "Error reading 500px response", e);
        throw new RetryException();
    }
    if (response == null || response.photos == null) {
        throw new RetryException();
    }
    if (response.photos.size() == 0) {
        Log.w(TAG, "No photos returned from API.");
        scheduleUpdate(System.currentTimeMillis() + ROTATE_TIME_MILLIS);
        return;
    }
    Random random = new Random();
    Photo photo;
    String token;
    while (true) {
        photo = response.photos.get(random.nextInt(response.photos.size()));
        token = Integer.toString(photo.id);
        if (response.photos.size() <= 1 || !TextUtils.equals(token, currentToken)) {
            break;
        }
    }
    publishArtwork(new Artwork.Builder().title(photo.name).byline(photo.user.fullname).imageUri(Uri.parse(photo.image_url)).token(token).viewIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("http://500px.com/photo/" + photo.id))).build());
    scheduleUpdate(System.currentTimeMillis() + ROTATE_TIME_MILLIS);
}
Also used : OkHttpClient(okhttp3.OkHttpClient) Artwork(com.google.android.apps.muzei.api.Artwork) Request(okhttp3.Request) Photo(com.example.muzei.examplesource500px.FiveHundredPxService.Photo) Intent(android.content.Intent) IOException(java.io.IOException) HttpUrl(okhttp3.HttpUrl) Retrofit(retrofit2.Retrofit) Random(java.util.Random) PhotosResponse(com.example.muzei.examplesource500px.FiveHundredPxService.PhotosResponse) Interceptor(okhttp3.Interceptor)

Example 3 with Body

use of retrofit2.http.Body in project retrofit by square.

the class ProtoConverterFactoryTest method serializeAndDeserialize.

@Test
public void serializeAndDeserialize() throws IOException, InterruptedException {
    ByteString encoded = ByteString.decodeBase64("Cg4oNTE5KSA4NjctNTMwOQ==");
    server.enqueue(new MockResponse().setBody(new Buffer().write(encoded)));
    Call<Phone> call = service.post(Phone.newBuilder().setNumber("(519) 867-5309").build());
    Response<Phone> response = call.execute();
    Phone body = response.body();
    assertThat(body.getNumber()).isEqualTo("(519) 867-5309");
    RecordedRequest request = server.takeRequest();
    assertThat(request.getBody().readByteString()).isEqualTo(encoded);
    assertThat(request.getHeader("Content-Type")).isEqualTo("application/x-protobuf");
}
Also used : Buffer(okio.Buffer) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) ByteString(okio.ByteString) Phone(retrofit2.converter.protobuf.PhoneProtos.Phone) Test(org.junit.Test)

Example 4 with Body

use of retrofit2.http.Body in project retrofit by square.

the class ProtoConverterFactoryTest method deserializeEmpty.

@Test
public void deserializeEmpty() throws IOException {
    server.enqueue(new MockResponse());
    Call<Phone> call = service.get();
    Response<Phone> response = call.execute();
    Phone body = response.body();
    assertThat(body.hasNumber()).isFalse();
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Phone(retrofit2.converter.protobuf.PhoneProtos.Phone) Test(org.junit.Test)

Example 5 with Body

use of retrofit2.http.Body in project retrofit by square.

the class RequestBuilderTest method multipartPartsShouldBeInOrder.

@Test
public void multipartPartsShouldBeInOrder() throws IOException {
    class Example {

        @Multipart
        @POST("/foo")
        Call<ResponseBody> get(@Part("first") String data, @Part("second") String dataTwo, @Part("third") String dataThree) {
            return null;
        }
    }
    Request request = buildRequest(Example.class, "firstParam", "secondParam", "thirdParam");
    MultipartBody body = (MultipartBody) request.body();
    Buffer buffer = new Buffer();
    body.writeTo(buffer);
    String readBody = buffer.readUtf8();
    assertThat(readBody.indexOf("firstParam")).isLessThan(readBody.indexOf("secondParam"));
    assertThat(readBody.indexOf("secondParam")).isLessThan(readBody.indexOf("thirdParam"));
}
Also used : Buffer(okio.Buffer) Part(retrofit2.http.Part) MultipartBody(okhttp3.MultipartBody) Request(okhttp3.Request) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Aggregations

ResponseBody (okhttp3.ResponseBody)40 Test (org.junit.Test)32 Request (okhttp3.Request)27 RequestBody (okhttp3.RequestBody)27 Retrofit (retrofit2.Retrofit)22 Response (retrofit2.Response)19 MultipartBody (okhttp3.MultipartBody)17 Buffer (okio.Buffer)17 OkHttpClient (okhttp3.OkHttpClient)13 IOException (java.io.IOException)12 MockResponse (okhttp3.mockwebserver.MockResponse)11 Body (retrofit2.http.Body)11 Part (retrofit2.http.Part)10 List (java.util.List)7 ServiceResponse (com.microsoft.rest.ServiceResponse)6 Interceptor (okhttp3.Interceptor)6 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)6 TypeToken (com.google.common.reflect.TypeToken)5 Product (fixtures.lro.models.Product)5 HttpUrl (okhttp3.HttpUrl)5