Skip to content
This repository has been archived by the owner on Nov 26, 2022. It is now read-only.

Commit

Permalink
Support Array and Non-Encoding params and queries
Browse files Browse the repository at this point in the history
Change storage from map to arraylist to support same key for multiple params and queries
Update KeyValuePair instance method to support non-encoding params and queries
  • Loading branch information
Decky Fiyemonda committed Nov 13, 2018
1 parent c2b2eef commit 0e751b0
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 37 deletions.
25 changes: 20 additions & 5 deletions app/src/main/java/com/github/deckyfx/httprequest/KeyValuePair.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,32 @@
public class KeyValuePair {
private String key;
private Object value;
private boolean encode;

public KeyValuePair(String key, Object value){
this.key = key;
this.value = value;
this(key, value, true);
}

public KeyValuePair(String key, Object value, boolean encode){
this.key = key;
this.value = value;
this.encode = encode;
}

public String getKey() {
return key;
return this.key;
}

public Object getValue() {
return value;
return this.value;
}

public boolean shouldEncode() {
return this.encode;
}

public String getValueAsString() {
return String.valueOf(value);
return String.valueOf(this.value);
}

public KeyValuePair key(String key) {
Expand All @@ -34,4 +44,9 @@ public KeyValuePair value(Object value) {
this.value = value;
return this;
}

public KeyValuePair encode(boolean value) {
this.encode = value;
return this;
}
}
87 changes: 55 additions & 32 deletions app/src/main/java/com/github/deckyfx/httprequest/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,14 @@
import android.content.ContentResolver;
import android.content.Context;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.webkit.MimeTypeMap;

import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -59,8 +57,8 @@ public class Request implements Callback {
private @Nullable RequestBody body = null;
private Object tag = null;
private DBHelper db = null;
private Map<String, Object> params = null;
private Map<String, Object> queries = null;
private ArrayList<KeyValuePair> params = null;
private ArrayList<KeyValuePair> queries = null;
private volatile CacheControl cacheControl = null; // Lazily initialized.
private RequestListener requestHandler = null;
private Call call = null;
Expand Down Expand Up @@ -128,7 +126,8 @@ public boolean isHttps() {
return url.isHttps();
}

@Override public String toString() {
@Override
public String toString() {
return "Request{method="
+ method
+ ", url="
Expand Down Expand Up @@ -158,6 +157,18 @@ public Call call() {
return this.call;
}

private String generateSimpleParam(ArrayList<KeyValuePair> params) {
StringBuilder sb = new StringBuilder();
for (KeyValuePair param : this.params) {
String param_value = "";
if (param.getValue() != null) {
param_value = param.getValueAsString();
}
sb.append(param.getKey()).append("=").append(param_value);
}
return sb.toString();
}

@Override
public void onFailure(Call call, IOException e) {
this.call = call;
Expand All @@ -175,7 +186,7 @@ public void onFailure(Call call, IOException e) {

}
}
String param_str = (new JSONObject(this.params)).toString();
String param_str = this.generateSimpleParam(this.params);
String url = call.request().url().toString();
String responseMessage = "";

Expand All @@ -201,7 +212,7 @@ public void onResponse(Call call, Response response) throws IOException {
int request_code = response.code();
String url = call.request().url().toString();
String message = response.message();
String param_str = (new JSONObject(this.params)).toString();
String param_str = this.generateSimpleParam(this.params);
String errorMessage = "";
String responMessage = "";
if (response_bytes != null) {
Expand Down Expand Up @@ -365,8 +376,8 @@ public static class Builder {
private @Nullable RequestBody body = null;
private Object tag = null;
private DBHelper db = null;
private Map<String, Object> params = new HashMap<String, Object>();
private Map<String,Object> queries = new HashMap<String, Object>();
private ArrayList<KeyValuePair> params = new ArrayList<KeyValuePair>();
private ArrayList<KeyValuePair> queries = new ArrayList<KeyValuePair>();
private volatile CacheControl cacheControl = null; // Lazily initialized.
private RequestListener requestHandler = null;
private Call call = null;
Expand Down Expand Up @@ -582,40 +593,40 @@ public Builder method(String method){
}

public Builder params(Map<String, Object> params){
this.params = new HashMap<>();
for (Map.Entry<String, Object> param : params.entrySet()) {
this.addParam(param.getKey(), param.getValue());
this.addParam(new KeyValuePair(param.getKey(), param.getValue()));
}
return this;
}

public Builder addParam(String key, Object value) {
this.params.put(key, value);
if (value instanceof File) {
this.containFile = true;
}
this.addParam(new KeyValuePair(key, value));
return this;
}

public Builder addParam(KeyValuePair pair) {
return this.addParam(pair.getKey(), pair.getValueAsString());
if (pair.getValue() instanceof File) {
this.containFile = true;
}
this.params.add(pair);
return this;
}

public Builder queries(Map<String, Object> params){
this.queries = new HashMap<>();
for (Map.Entry<String, Object> param : params.entrySet()) {
this.addQuery(param.getKey(), param.getValue());
this.addQuery(new KeyValuePair(param.getKey(), param.getValue()));
}
return this;
}

public Builder addQuery(String key, Object value) {
this.queries.put(key, value);
this.addQuery(new KeyValuePair(key, value));
return this;
}

public Builder addQuery(KeyValuePair pair) {
return this.addQuery(pair.getKey(), pair.getValueAsString());
this.queries.add(pair);
return this;
}

public Builder authBasicHeader(String login, String password) {
Expand Down Expand Up @@ -686,21 +697,29 @@ private HttpUrl buildURL(boolean strict) {
}
}
if (this.method.equals(HttpMethod.GET)) {
for (Map.Entry<String, Object> param : this.params.entrySet()) {
for (KeyValuePair param : this.params) {
String param_value = "";
if (param.getValue() != null) {
param_value = param.getValue().toString();
param_value = param.getValueAsString();
}
if (param.shouldEncode()) {
builder.addQueryParameter(param.getKey(), param_value);
} else {
builder.addEncodedQueryParameter(param.getKey(), param_value);
}
builder.addQueryParameter(param.getKey(), param_value);
}
}
if (!this.queries.isEmpty()) {
for (Map.Entry<String, Object> param : this.queries.entrySet()) {
for (KeyValuePair param : this.queries) {
String param_value = "";
if (param.getValue() != null) {
param_value = param.getValue().toString();
param_value = param.getValueAsString();
}
if (param.shouldEncode()) {
builder.addQueryParameter(param.getKey(), param_value);
} else {
builder.addEncodedQueryParameter(param.getKey(), param_value);
}
builder.addQueryParameter(param.getKey(), param_value);
}
}
return builder.build();
Expand All @@ -716,7 +735,7 @@ private RequestBody buildBody(boolean strict) {
if (this.containFile) {
MultipartBody.Builder bodyBuilder = new MultipartBody.Builder();
bodyBuilder.setType(MultipartBody.FORM);
for (Map.Entry<String, Object> param : this.params.entrySet()) {
for (KeyValuePair param : this.params) {
if (param.getValue() instanceof File) {
File f = (File) param.getValue();
String mimeType = null;
Expand All @@ -740,12 +759,16 @@ private RequestBody buildBody(boolean strict) {
}
} else {
FormBody.Builder bodyBuilder = new FormBody.Builder();
for (Map.Entry<String, Object> param : this.params.entrySet()) {
for (KeyValuePair param : this.params) {
String param_value = "";
if (param.getValue() != null) {
param_value = param.getValue().toString();
param_value = param.getValueAsString();
}
if (param.shouldEncode()) {
bodyBuilder.add(param.getKey(), param_value);
} else {
bodyBuilder.addEncoded(param.getKey(), param_value);
}
bodyBuilder.add(param.getKey(), param_value);
}
this.body = bodyBuilder.build();
}
Expand Down

0 comments on commit 0e751b0

Please sign in to comment.