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--");
}
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);
}
Aggregations