Here's my version
/**
* <strong>Uses:</strong><br/>
* <p>
* {@code
* List<Pair<String, String>> pairs = new ArrayList<>();}
* <br/>
* {@code pairs.add(new Pair<>("key1", "value1"));}<br/>
* {@code pairs.add(new Pair<>("key2", "value2"));}<br/>
* {@code pairs.add(new Pair<>("key3", "value3"));}<br/>
* <br/>
* {@code postToServer("http://www.example.com/", pairs);}<br/>
* </p>
*
* @param url
* @param pairs List of support.V4 Pair
* @return response from server in String format
* @throws Exception
*/
public String postToServer(String url, List<Pair<String, String>> pairs) throws Exception {
okhttp3.OkHttpClient client = new okhttp3.OkHttpClient();
okhttp3.Request.Builder builder = new okhttp3.Request.Builder().url(url);
if (pairs != null) {
okhttp3.FormBody.Builder postData = new okhttp3.FormBody.Builder();
for (Pair<String, String> pair : pairs) {
postData.add(pair.first, pair.second);
}
builder.post(postData.build());
}
okhttp3.Request request = builder.build();
okhttp3.Response response = client.newCall(request).execute();
if (!response.isSuccessful()) {
throw new IOException(response.message() + " " + response.toString());
}
return response.body().string();
}