Skip to content

Commit d56bdf0

Browse files
committed
Posting JSON with Retrofit
1 parent a197ca3 commit d56bdf0

4 files changed

Lines changed: 196 additions & 0 deletions

File tree

retrofit-post-json/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>retrofit-post-json</artifactId>
6+
<version>0.0.1-SNAPSHOT</version>
7+
<packaging>jar</packaging>
8+
9+
<name>retrofit-post-json</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.3.0</version>
26+
</dependency>
27+
<dependency>
28+
<groupId>com.squareup.retrofit2</groupId>
29+
<artifactId>converter-gson</artifactId>
30+
<version>2.3.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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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.HashMap;
12+
import java.util.Map;
13+
14+
public interface BookResource {
15+
16+
17+
@POST("books")
18+
Call<Book> createBook(@Body Book book);
19+
20+
@POST("books")
21+
Call<Book> updateBook(@Body Map<String, Object> body);
22+
23+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.howtoprogram.retrofit2;
2+
3+
import org.junit.Test;
4+
import retrofit2.Call;
5+
import retrofit2.Response;
6+
import retrofit2.Retrofit;
7+
import retrofit2.converter.gson.GsonConverterFactory;
8+
9+
import java.io.IOException;
10+
import java.util.HashMap;
11+
import java.util.Map;
12+
13+
import static org.junit.Assert.assertTrue;
14+
15+
public class BookResourceTest {
16+
17+
@Test
18+
public void testPostJsonByPojo() throws IOException {
19+
20+
//create an instance of Retrofit
21+
Retrofit retrofit = new Retrofit.Builder()
22+
.baseUrl("http://localhost:8080/v1/")
23+
.addConverterFactory(GsonConverterFactory.create())
24+
.build();
25+
26+
//create an instance of the BookResource interface
27+
BookResource bookResource = retrofit.create(BookResource.class);
28+
//create a Book object
29+
Book book = new Book(1l, "Java How To Program", "Paul Deitel");
30+
31+
// send the Book to the Rest API
32+
Call<Book> call = bookResource.createBook(book);
33+
Response<Book> response = call.execute();
34+
35+
assertTrue(response.isSuccessful());
36+
37+
}
38+
39+
@Test
40+
public void testPostJsonByMap() throws IOException {
41+
42+
Retrofit retrofit = new Retrofit.Builder()
43+
.baseUrl("http://localhost:8080/v1/")
44+
.addConverterFactory(GsonConverterFactory.create())
45+
.build();
46+
47+
BookResource bookResource = retrofit.create(BookResource.class);
48+
Map<String, Object> bookMap = new HashMap<>();
49+
bookMap.put("name", "To Kill a Mockingbird");
50+
bookMap.put("author", "Harper Lee");
51+
52+
Call<Book> call = bookResource.updateBook(bookMap);
53+
Response<Book> response = call.execute();
54+
55+
assertTrue(response.isSuccessful());
56+
57+
}
58+
59+
}

0 commit comments

Comments
 (0)