Search in sources :

Example 66 with POST

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

the class RequestBuilderTest method multipartPartMapWithEncoding.

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

        // 
        @Multipart
        // 
        @POST("/foo/bar/")
        Call<ResponseBody> method(@PartMap(encoding = "8-bit") Map<String, RequestBody> parts) {
            return null;
        }
    }
    Map<String, RequestBody> params = new LinkedHashMap<>();
    params.put("ping", RequestBody.create(null, "pong"));
    params.put("kit", RequestBody.create(null, "kat"));
    Request request = buildRequest(Example.class, params);
    assertThat(request.method()).isEqualTo("POST");
    assertThat(request.headers().size()).isZero();
    assertThat(request.url().toString()).isEqualTo("http://example.com/foo/bar/");
    RequestBody body = request.body();
    Buffer buffer = new Buffer();
    body.writeTo(buffer);
    String bodyString = buffer.readUtf8();
    assertThat(bodyString).contains("Content-Disposition: form-data;").contains("name=\"ping\"\r\n").contains("Content-Transfer-Encoding: 8-bit").contains("\r\npong\r\n--");
    assertThat(bodyString).contains("Content-Disposition: form-data;").contains("name=\"kit\"").contains("Content-Transfer-Encoding: 8-bit").contains("\r\nkat\r\n--");
}
Also used : Buffer(okio.Buffer) Request(okhttp3.Request) PartMap(retrofit2.http.PartMap) PartMap(retrofit2.http.PartMap) HashMap(java.util.HashMap) HeaderMap(retrofit2.http.HeaderMap) FieldMap(retrofit2.http.FieldMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) QueryMap(retrofit2.http.QueryMap) ResponseBody(okhttp3.ResponseBody) RequestBody(okhttp3.RequestBody) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 67 with POST

use of retrofit2.http.POST in project TumCampusApp by TCA-Team.

the class ChatRoomsActivity method createOrJoinChatRoom.

/**
 * Creates a given chat room if it does not exist and joins it
 * Works asynchronously.
 */
private void createOrJoinChatRoom(String name) {
    if (this.currentChatMember == null) {
        Utils.showToast(this, getString(R.string.chat_not_setup));
        return;
    }
    Utils.logv("create or join chat room " + name);
    currentChatRoom = new ChatRoom(name);
    ChatVerification verification;
    try {
        verification = ChatVerification.Companion.getChatVerification(this, this.currentChatMember);
    } catch (NoPrivateKey noPrivateKey) {
        this.finish();
        return;
    }
    Callback callback = new Callback<ChatRoom>() {

        @Override
        public void onResponse(@NonNull Call<ChatRoom> call, @NonNull Response<ChatRoom> response) {
            if (!response.isSuccessful()) {
                Utils.logv("Error creating&joining chat room: " + response.message());
                return;
            }
            // The POST request is successful: go to room. API should have auto joined it
            Utils.logv("Success creating&joining chat room: " + response.body());
            currentChatRoom = response.body();
            manager.join(currentChatRoom);
            // When we show joined chat rooms open chat room directly
            if (mCurrentMode == 1) {
                moveToChatActivity();
            } else {
                // Otherwise show a nice information, that we added the room
                final List<ChatRoomAndLastMessage> rooms = manager.getAllByStatus(mCurrentMode);
                runOnUiThread(() -> {
                    chatRoomAdapter.updateRooms(rooms);
                    Utils.showToast(ChatRoomsActivity.this, R.string.joined_chat_room);
                });
            }
        }

        @Override
        public void onFailure(@NonNull Call<ChatRoom> call, @NonNull Throwable t) {
            Utils.log(t, "Failure creating/joining chat room - trying to GET it from the server");
            Utils.showToastOnUIThread(ChatRoomsActivity.this, R.string.activate_key);
        }
    };
    TUMCabeClient.getInstance(this).createRoom(currentChatRoom, verification, callback);
}
Also used : Response(retrofit2.Response) Call(retrofit2.Call) Callback(retrofit2.Callback) NoPrivateKey(de.tum.in.tumcampusapp.api.app.exception.NoPrivateKey) ChatRoom(de.tum.in.tumcampusapp.component.ui.chat.model.ChatRoom) ChatRoomAndLastMessage(de.tum.in.tumcampusapp.component.ui.chat.model.ChatRoomAndLastMessage) NonNull(android.support.annotation.NonNull) ChatVerification(de.tum.in.tumcampusapp.component.ui.chat.model.ChatVerification)

Aggregations

ResponseBody (okhttp3.ResponseBody)38 Test (org.junit.Test)33 Request (okhttp3.Request)31 Response (retrofit2.Response)27 ServiceResponse (com.microsoft.rest.ServiceResponse)22 RequestBody (okhttp3.RequestBody)19 TypeToken (com.google.common.reflect.TypeToken)18 Product (fixtures.lro.models.Product)18 Buffer (okio.Buffer)14 MultipartBody (okhttp3.MultipartBody)13 Part (retrofit2.http.Part)10 Body (retrofit2.http.Body)8 HashMap (java.util.HashMap)7 LinkedHashMap (java.util.LinkedHashMap)7 OkHttpClient (okhttp3.OkHttpClient)6 Field (retrofit2.http.Field)6 FieldMap (retrofit2.http.FieldMap)6 PartMap (retrofit2.http.PartMap)6 List (java.util.List)5 Map (java.util.Map)5