From: Damyan Ivanov Date: Mon, 3 Dec 2018 21:20:34 +0000 (+0000) Subject: helper class for constructing urlencoded form data X-Git-Tag: v0.3~354 X-Git-Url: https://git.ktnx.net/?a=commitdiff_plain;h=5f8619dde51fa02c988eb63017530093a00fb35e;p=mobile-ledger.git helper class for constructing urlencoded form data --- diff --git a/app/src/main/java/net/ktnx/mobileledger/UrlEncodedFormData.java b/app/src/main/java/net/ktnx/mobileledger/UrlEncodedFormData.java new file mode 100644 index 00000000..1e09d240 --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/UrlEncodedFormData.java @@ -0,0 +1,46 @@ +package net.ktnx.mobileledger; + +import android.support.annotation.NonNull; + +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; +import java.util.AbstractMap; +import java.util.ArrayList; +import java.util.List; + +class UrlEncodedFormData { + private List> pairs; + + UrlEncodedFormData() { + pairs = new ArrayList>(); + } + + void add_pair(String name, String value) { + pairs.add(new AbstractMap.SimpleEntry(name, value)); + } + + @NonNull + public String toString() { + StringBuilder result = new StringBuilder(); + boolean first = true; + + for (AbstractMap.SimpleEntry pair : pairs) { + if (first) { + first = false; + } + else { + result.append('&'); + } + + try { + result.append(URLEncoder.encode(pair.getKey(), "UTF-8")) + .append('=') + .append(URLEncoder.encode(pair.getValue(), "UTF-8")); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + } + + return result.toString(); + } +}