Skip to content

Commit

Permalink
support for specifying HTTP method, headers and body
Browse files Browse the repository at this point in the history
  • Loading branch information
gfwilliams committed Aug 1, 2022
1 parent 62b575a commit 4cd53fa
Showing 1 changed file with 54 additions and 3 deletions.
Expand Up @@ -34,6 +34,7 @@

import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.RequestQueue;
Expand Down Expand Up @@ -65,6 +66,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.SimpleTimeZone;
import java.util.UUID;
import java.lang.reflect.Field;
Expand Down Expand Up @@ -543,19 +545,50 @@ private void handleUartRxJSON(JSONObject json) throws JSONException {
}
final String id = _id;


if (BuildConfig.INTERNET_ACCESS && devicePrefs.getBoolean(PREF_DEVICE_INTERNET_ACCESS, false)) {
RequestQueue queue = Volley.newRequestQueue(getContext());
String url = json.getString("url");

int method = Request.Method.GET;
if (json.has("method")) {
String m = json.getString("method").toLowerCase();
if (m.equals("get")) method = Request.Method.GET;
else if (m.equals("post")) method = Request.Method.POST;
else if (m.equals("head")) method = Request.Method.HEAD;
else if (m.equals("put")) method = Request.Method.PUT;
else if (m.equals("delete")) method = Request.Method.DELETE;
else uartTxJSONError("http", "Unknown HTTP method "+m,id);
}

byte[] _body = null;
if (json.has("body"))
_body = json.getString("body").getBytes();
final byte[] body = _body;

Map<String,String> _headers = null;
if (json.has("headers")) {
JSONObject h = json.getJSONObject("headers");
_headers = new HashMap<String,String>();
Iterator<String> iter = h.keys();
while (iter.hasNext()) {
String key = iter.next();
try {
String value = h.getString(key);
_headers.put(key, value);
} catch (JSONException e) {
}
}
}
final Map<String,String> headers = _headers;

String _xmlPath = "";
try {
_xmlPath = json.getString("xpath");
} catch (JSONException e) {
}
final String xmlPath = _xmlPath;
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
StringRequest stringRequest = new StringRequest(method, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Expand Down Expand Up @@ -586,7 +619,25 @@ public void onErrorResponse(VolleyError error) {
JSONObject o = new JSONObject();
uartTxJSONError("http", error.toString(),id);
}
});
}) {
@Override
public byte[] getBody() throws AuthFailureError {
if (body == null) return super.getBody();
return body;
}

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> h = super.getHeaders();
Iterator<String> iter = headers.keySet().iterator();
while (iter.hasNext()) {
String key = iter.next();
String value = headers.get(key);
h.put(key, value);
}
return h;
}
};
queue.add(stringRequest);
} else {
if (BuildConfig.INTERNET_ACCESS)
Expand Down

0 comments on commit 4cd53fa

Please sign in to comment.