Skip to content

Commit c63bd5f

Browse files
committed
- Added new post method in volley client.
1 parent 6b9706a commit c63bd5f

File tree

1 file changed

+40
-13
lines changed
  • base/src/main/java/com/base/utils

1 file changed

+40
-13
lines changed

base/src/main/java/com/base/utils/Api.java

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
import android.content.Context;
55

66
import com.android.volley.Request;
7+
import com.android.volley.toolbox.JsonObjectRequest;
78
import com.android.volley.toolbox.StringRequest;
89

10+
import org.json.JSONObject;
11+
912
import java.util.HashMap;
1013
import java.util.Map;
1114

@@ -54,9 +57,9 @@ private Map<String, String> getHeaders() {
5457
/**
5558
* Create GET request
5659
*
57-
* @param url Desired URL
58-
* @param listener Success callback
59-
* @param errorListener Error callback
60+
* @param url String
61+
* @param listener Response<String>
62+
* @param errorListener Error
6063
*/
6164
public void get(String url, Response<String> listener, Error errorListener) {
6265
Logger.i(Constants.TAG, "url--> " + url);
@@ -73,10 +76,10 @@ public Map<String, String> getHeaders() {
7376
/**
7477
* Create POST request
7578
*
76-
* @param url Desired URL
77-
* @param body params
78-
* @param listener response listener
79-
* @param errorListener error listener
79+
* @param url String
80+
* @param body Map<String, String>
81+
* @param listener Response<String>
82+
* @param errorListener Error
8083
*/
8184
public void post(String url, final Map<String, String> body, Response<String> listener, Error errorListener) {
8285
Logger.i(Constants.TAG, "url--> " + url);
@@ -99,13 +102,37 @@ public String getBodyContentType() {
99102
Singleton.getInstance(context).addRequestQueue(request);
100103
}
101104

105+
/**
106+
* Create POST request
107+
*
108+
* @param url String
109+
* @param body JSONObject
110+
* @param listener Response<JSONObject>
111+
* @param errorListener Error
112+
*/
113+
public void post(String url, final JSONObject body, Response<JSONObject> listener, Error errorListener) {
114+
Logger.i(Constants.TAG, "url--> " + url);
115+
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, body, listener::response, error -> errorListener.error(Utils.with(context).getError(error))) {
116+
@Override
117+
public Map<String, String> getHeaders() {
118+
return Api.this.getHeaders();
119+
}
120+
121+
@Override
122+
public String getBodyContentType() {
123+
return "application/json";
124+
}
125+
};
126+
Singleton.getInstance(context).addRequestQueue(request);
127+
}
128+
102129
/**
103130
* Create PUT request
104131
*
105-
* @param url Desired URL
132+
* @param url String
106133
* @param body Map<String, String> body
107-
* @param listener Success callback
108-
* @param errorListener Error callback
134+
* @param listener Response<String>
135+
* @param errorListener Error
109136
*/
110137
public void put(String url, final Map<String, String> body, Response<String> listener, Error errorListener) {
111138
Logger.i(Constants.TAG, "url--> " + url);
@@ -131,9 +158,9 @@ public String getBodyContentType() {
131158
/**
132159
* Create DELETE request
133160
*
134-
* @param url Desired URL
135-
* @param listener Success callback
136-
* @param errorListener Error callback
161+
* @param url String
162+
* @param listener Response<String>
163+
* @param errorListener Error
137164
*/
138165
public void delete(String url, Response<String> listener, Error errorListener) {
139166
Logger.i(Constants.TAG, "url--> " + url);

0 commit comments

Comments
 (0)