diff --git a/.idea/misc.xml b/.idea/misc.xml
index 125ad42..70d16fa 100644
--- a/.idea/misc.xml
+++ b/.idea/misc.xml
@@ -3,6 +3,24 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/DemoApp/src/main/AndroidManifest.xml b/DemoApp/src/main/AndroidManifest.xml
index ddc91aa..53a991b 100644
--- a/DemoApp/src/main/AndroidManifest.xml
+++ b/DemoApp/src/main/AndroidManifest.xml
@@ -8,7 +8,6 @@
android:allowBackup = "true"
android:icon = "@mipmap/ic_launcher"
android:label = "@string/app_name"
- android:roundIcon = "@mipmap/ic_launcher_round"
android:supportsRtl = "true"
android:theme = "@style/AppTheme">
@@ -20,4 +19,4 @@
-
\ No newline at end of file
+
diff --git a/DemoApp/src/main/java/boxresin/library/javahttp/MainActivity.java b/DemoApp/src/main/java/boxresin/library/javahttp/MainActivity.java
index 5484a46..91d761a 100644
--- a/DemoApp/src/main/java/boxresin/library/javahttp/MainActivity.java
+++ b/DemoApp/src/main/java/boxresin/library/javahttp/MainActivity.java
@@ -25,21 +25,6 @@ protected void onCreate(Bundle savedInstanceState)
if (response != null)
binding.txtResponse.setText(response);
}
-
- try
- {
- HttpResponse response = new HttpRequester()
- .setUrl("https://www.google.com/")
- .setMethod("GET")
- .addHeader("key", "value")
- .addHeader("key", "value")
- .addHeader("key", "value")
- .addHeader("key", "value")
- .request();
- }
- catch (IOException e)
- {
- }
}
public void onClick(View view)
diff --git a/Library/src/main/java/boxresin/library/javahttp/HttpRequester.java b/Library/src/main/java/boxresin/library/javahttp/HttpRequester.java
index 97b41b5..4a6461d 100644
--- a/Library/src/main/java/boxresin/library/javahttp/HttpRequester.java
+++ b/Library/src/main/java/boxresin/library/javahttp/HttpRequester.java
@@ -5,9 +5,11 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URL;
+import java.net.URLEncoder;
import java.util.Map;
import java.util.TreeMap;
@@ -55,6 +57,32 @@ public HttpRequester setUrl(String url)
return this;
}
+ /**
+ * Sets the URL to request.
+ * @param url The URL before a part of query parameter
+ * @param params Query parameters to URL. It will be URL encoded.
+ * @since v1.1.0
+ */
+ public HttpRequester setUrl(String url, Map params)
+ {
+ this.url = url;
+ if (!params.isEmpty())
+ {
+ this.url += "?";
+ for (Map.Entry param : params.entrySet())
+ {
+ try
+ {
+ this.url += (param.getKey() + "=" + URLEncoder.encode(param.getValue(), "UTF-8"));
+ this.url += '&';
+ }
+ catch (UnsupportedEncodingException ignored) {}
+ }
+ this.url = this.url.substring(0, this.url.length() - 1);
+ }
+ return this;
+ }
+
/**
* Returns HTTP method to request.
* @return HTTP method as String type (ex. "POST", "GET" etc)
diff --git a/Library/src/test/java/boxresin/library/javahttp/LibraryTest.java b/Library/src/test/java/boxresin/library/javahttp/LibraryTest.java
index 02bf31d..557b8a8 100644
--- a/Library/src/test/java/boxresin/library/javahttp/LibraryTest.java
+++ b/Library/src/test/java/boxresin/library/javahttp/LibraryTest.java
@@ -4,7 +4,10 @@
import org.junit.Test;
import java.io.IOException;
+import java.net.URLEncoder;
import java.nio.charset.Charset;
+import java.util.HashMap;
+import java.util.Map;
/**
* Created by Minsuk Eom on 2017-04-27.
@@ -73,4 +76,42 @@ public void testHttpResponse() throws IOException
Assert.assertEquals(new String(response.getBodyAsByteArray(), Charset.forName(response.getBodyEncoding())), response.getBody());
}
+
+ @Test
+ public void testSetURL()
+ {
+ Map params = new HashMap<>();
+ params.put("hello", "world");
+ params.put("test", "it");
+
+ HttpRequester requester = new HttpRequester();
+ requester.setUrl("http://www.google.com", params);
+ Assert.assertEquals("http://www.google.com?test=it&hello=world", requester.getUrl());
+
+ params = new HashMap<>();
+ params.put("hangul", "한글 테스트");
+ params.put("second", "두 번째");
+
+ requester.setUrl("http://www.google.com", params);
+ Assert.assertEquals("http://www.google.com?hangul=%ED%95%9C%EA%B8%80+%ED%85%8C%EC%8A%A4%ED%8A%B8&" +
+ "second=%EB%91%90+%EB%B2%88%EC%A7%B8", requester.getUrl());
+ }
+
+ @Test
+ public void testURLCodec()
+ {
+ System.out.println(URLEncoder.encode("한글"));
+ for (Map.Entry charset : Charset.availableCharsets().entrySet())
+ {
+ try
+ {
+ if ("%ED%95%9C%EA%B8%80".equals(URLEncoder.encode("한글", charset.getKey())))
+ return;
+ }
+ catch (Exception e)
+ {
+ }
+ }
+ Assert.assertTrue(false);
+ }
}
diff --git a/build.gradle b/build.gradle
index d7c138c..1b0d6f1 100644
--- a/build.gradle
+++ b/build.gradle
@@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
- classpath 'com.android.tools.build:gradle:2.3.2'
+ classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'com.novoda:bintray-release:0.4.0'
// NOTE: Do not place your application dependencies here; they belong