Skip to content

Commit ef73fe7

Browse files
committed
post with Retrofit 2
1 parent b0de196 commit ef73fe7

5 files changed

Lines changed: 296 additions & 0 deletions

File tree

retrofit2-post-examples/pom.xml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.howtoprogram</groupId>
5+
<artifactId>retrofit2-post-examples</artifactId>
6+
<version>0.0.1-SNAPSHOT</version>
7+
<packaging>jar</packaging>
8+
9+
<name>retrofit2-post-examples</name>
10+
<url>http://howtoprogram.xyz/2017/02/16/how-to-post-with-retrofit-2/</url>
11+
12+
<properties>
13+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
14+
<maven.compiler.source>1.8</maven.compiler.source>
15+
<maven.compiler.target>1.8</maven.compiler.target>
16+
17+
</properties>
18+
19+
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>com.squareup.retrofit2</groupId>
24+
<artifactId>retrofit</artifactId>
25+
<version>2.1.0</version>
26+
</dependency>
27+
<dependency>
28+
<groupId>com.squareup.retrofit2</groupId>
29+
<artifactId>converter-gson</artifactId>
30+
<version>2.1.0</version>
31+
</dependency>
32+
<dependency>
33+
<groupId>junit</groupId>
34+
<artifactId>junit</artifactId>
35+
<version>4.12</version>
36+
</dependency>
37+
</dependencies>
38+
</project>
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.howtoprogram.retrofit2;
2+
3+
import javax.xml.bind.annotation.XmlRootElement;
4+
5+
public class Book {
6+
7+
private Long id;
8+
private String name;
9+
private String author;
10+
11+
public Book() {
12+
13+
}
14+
// getter and sett
15+
16+
17+
public Book(Long id, String name, String author) {
18+
super();
19+
this.id = id;
20+
this.name = name;
21+
this.author = author;
22+
}
23+
24+
/**
25+
* @return the id
26+
*/
27+
public Long getId() {
28+
return id;
29+
}
30+
31+
/**
32+
* @param id the id to set
33+
*/
34+
public void setId(Long id) {
35+
this.id = id;
36+
}
37+
38+
/**
39+
* @return the name
40+
*/
41+
public String getName() {
42+
return name;
43+
}
44+
45+
/**
46+
* @param name the name to set
47+
*/
48+
public void setName(String name) {
49+
this.name = name;
50+
}
51+
52+
/**
53+
* @return the author
54+
*/
55+
public String getAuthor() {
56+
return author;
57+
}
58+
59+
/**
60+
* @param author the author to set
61+
*/
62+
public void setAuthor(String author) {
63+
this.author = author;
64+
}
65+
66+
/*
67+
* (non-Javadoc)
68+
*
69+
* @see java.lang.Object#toString()
70+
*/
71+
@Override
72+
public String toString() {
73+
return "Book [id=" + id + ", name=" + name + ", author=" + author + "]";
74+
}
75+
76+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.howtoprogram.retrofit2;
2+
3+
4+
import okhttp3.MultipartBody;
5+
import okhttp3.RequestBody;
6+
import okhttp3.ResponseBody;
7+
import retrofit2.Call;
8+
import retrofit2.Callback;
9+
import retrofit2.http.*;
10+
11+
import java.util.Map;
12+
13+
public interface BookResourceRetrofit2 {
14+
15+
@FormUrlEncoded
16+
@POST("post")
17+
Call<Book> updateBook(@FieldMap Map<String, String> fieldsMap);
18+
19+
@Multipart
20+
@POST("post")
21+
Call<Book> addBookCover(@Part("id") RequestBody id, @Part MultipartBody.Part photo);
22+
23+
24+
@POST("post")
25+
Call<Book> addBook(@Body Book book);
26+
27+
28+
@FormUrlEncoded
29+
@POST("post")
30+
Call<Book> updateBook(@Field("id") Long id, @Field("author") String first,
31+
@Field("name") String name);
32+
33+
34+
@Multipart
35+
@POST("post")
36+
Call<Book> addBookCover2(@Part("id") RequestBody id, @Part("photo") RequestBody photo);
37+
}
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
package com.howtoprogram.retrofit2;
2+
3+
import okhttp3.MediaType;
4+
import okhttp3.MultipartBody;
5+
import okhttp3.RequestBody;
6+
import org.junit.Test;
7+
import retrofit2.Call;
8+
import retrofit2.Response;
9+
import retrofit2.Retrofit;
10+
import retrofit2.converter.gson.GsonConverterFactory;
11+
12+
import java.io.File;
13+
import java.io.IOException;
14+
import java.util.HashMap;
15+
import java.util.Map;
16+
17+
import static org.junit.Assert.assertTrue;
18+
19+
public class Retrofit2Test {
20+
21+
@Test
22+
public void testPostObject() throws IOException {
23+
24+
Retrofit retrofit = new Retrofit.Builder()
25+
.baseUrl("http://httpbin.org/")
26+
.addConverterFactory(GsonConverterFactory.create())
27+
.build();
28+
29+
BookResourceRetrofit2 bookResource = retrofit.create(BookResourceRetrofit2.class);
30+
Book book = new Book(1l, "Java How To Program", "Paul Deitel");
31+
32+
Call<Book> call = bookResource.addBook(book);
33+
Response<Book> response = call.execute();
34+
35+
assertTrue(response.isSuccessful());
36+
37+
}
38+
39+
@Test
40+
public void testPostJson() throws IOException {
41+
42+
Retrofit retrofit = new Retrofit.Builder()
43+
.baseUrl("http://httpbin.org/")
44+
.addConverterFactory(GsonConverterFactory.create())
45+
.build();
46+
47+
BookResourceRetrofit2 bookResource = retrofit.create(BookResourceRetrofit2.class);
48+
Book book = new Book(1l, "Java How To Program", "Paul Deitel");
49+
50+
Call<Book> call = bookResource.addBook(book);
51+
Response<Book> response = call.execute();
52+
53+
assertTrue(response.isSuccessful());
54+
55+
}
56+
57+
@Test
58+
public void testPostForm() throws IOException {
59+
Retrofit retrofit = new Retrofit.Builder()
60+
.baseUrl("http://httpbin.org/")
61+
.addConverterFactory(GsonConverterFactory.create())
62+
.build();
63+
64+
BookResourceRetrofit2 service = retrofit.create(BookResourceRetrofit2.class);
65+
66+
Call<Book> call = service.updateBook(1l, "Tom1", "Riddle1");
67+
68+
Response<Book> response = call.execute();
69+
assertTrue(response.isSuccessful());
70+
71+
72+
}
73+
@Test
74+
public void testPostFormWithFieldMap() throws IOException {
75+
Retrofit retrofit = new Retrofit.Builder()
76+
.baseUrl("http://httpbin.org/")
77+
.addConverterFactory(GsonConverterFactory.create())
78+
.build();
79+
80+
BookResourceRetrofit2 service = retrofit.create(BookResourceRetrofit2.class);
81+
Map<String, String> fieldsMap = new HashMap<>();
82+
fieldsMap.put("author","Joshua Bloch");
83+
fieldsMap.put("name","Java puzzlers");
84+
fieldsMap.put("id","1");
85+
Call<Book> call = service.updateBook(fieldsMap);
86+
87+
Response<Book> response = call.execute();
88+
assertTrue(response.isSuccessful());
89+
90+
}
91+
92+
@Test
93+
public void testPostMultipart() throws IOException {
94+
Retrofit retrofit = new Retrofit.Builder()
95+
.baseUrl("http://httpbin.org/")
96+
.addConverterFactory(GsonConverterFactory.create())
97+
.build();
98+
99+
BookResourceRetrofit2 service = retrofit.create(BookResourceRetrofit2.class);
100+
101+
// create RequestBody instance from file
102+
ClassLoader classLoader = getClass().getClassLoader();
103+
File file = new File(classLoader.getResource("RestImage.png").getFile());
104+
105+
RequestBody reqFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
106+
107+
// MultipartBody.Part is used to send also the actual file name
108+
MultipartBody.Part body =
109+
MultipartBody.Part.createFormData("file", file.getName(), reqFile);
110+
111+
// add book id part within the multipart request
112+
RequestBody id = RequestBody.create(MediaType.parse("text/plain"), String.valueOf(1l));
113+
114+
Call<Book> call = service.addBookCover(id, body);
115+
116+
Response<Book> response = call.execute();
117+
assertTrue(response.isSuccessful());
118+
119+
}
120+
121+
@Test
122+
public void testPostMultipart2() throws IOException {
123+
Retrofit retrofit = new Retrofit.Builder()
124+
.baseUrl("http://httpbin.org/")
125+
.addConverterFactory(GsonConverterFactory.create())
126+
.build();
127+
128+
BookResourceRetrofit2 service = retrofit.create(BookResourceRetrofit2.class);
129+
130+
// create RequestBody instance from file
131+
ClassLoader classLoader = getClass().getClassLoader();
132+
File file = new File(classLoader.getResource("RestImage.png").getFile());
133+
134+
RequestBody reqFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
135+
136+
RequestBody id = RequestBody.create(MediaType.parse("text/plain"), String.valueOf(1l));
137+
138+
Call<Book> call = service.addBookCover2(id, reqFile);
139+
140+
Response<Book> response = call.execute();
141+
assertTrue(response.isSuccessful());
142+
143+
}
144+
145+
}
1.75 KB
Loading

0 commit comments

Comments
 (0)