Browse Source

Now uses the QueryString generator.

Thomas Flucke 9 years ago
parent
commit
ba114271a0

+ 16 - 5
app/src/main/java/com/example/yiupang/freefoodfinder/HttpCall.java

@@ -3,6 +3,8 @@ package com.example.yiupang.freefoodfinder;
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
 
+import java.util.Map;
+
 /**
  * Created by yiupang on 5/27/2017.
  *
@@ -41,24 +43,25 @@ class HttpCall
     private String url;
     private int methodType;
     private JsonNode body;
+    private Map<String, String> query;
 
-    String getUrl() {
+    public String getUrl() {
         return url;
     }
 
-    void setUrl(String url) {
+    public void setUrl(String url) {
         this.url = url;
     }
 
-    int getMethodType() {
+    public int getMethodType() {
         return methodType;
     }
 
-    void setMethodType(int methodType) {
+    public void setMethodType(int methodType) {
         this.methodType = methodType;
     }
 
-    JsonNode getBody() {
+    public JsonNode getBody() {
         return body;
     }
 
@@ -69,4 +72,12 @@ class HttpCall
     public void setBody(Object params) {
         this.body = mapper.valueToTree(params);
     }
+
+    public Map<String, String> getQueryParams() {
+        return query;
+    }
+
+    public void setQueryParams(Map<String, String> query) {
+        this.query = query;
+    }
 }

+ 12 - 16
app/src/main/java/com/example/yiupang/freefoodfinder/HttpRequest.java

@@ -2,7 +2,6 @@ package com.example.yiupang.freefoodfinder;
 
 import android.os.AsyncTask;
 
-import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
 
 import java.io.BufferedWriter;
@@ -12,7 +11,6 @@ import java.io.UnsupportedEncodingException;
 import java.net.HttpURLConnection;
 import java.net.URL;
 import java.net.URLEncoder;
-import java.util.HashMap;
 import java.util.Map;
 
 /**
@@ -43,13 +41,13 @@ class HttpRequest extends AsyncTask<HttpCall, String, String>
             OutputStream os;
             BufferedWriter writer;
 
-            url = new URL(httpCall.getUrl());
+            url = new URL(httpCall.getUrl() + getDataString(httpCall.getQueryParams()));
             urlConnection = (HttpURLConnection) url.openConnection();
             urlConnection.setRequestMethod(HttpCall.methodToStr(httpCall.getMethodType()));
             urlConnection.setReadTimeout(10000 /* milliseconds */);
             urlConnection.setConnectTimeout(15000 /* milliseconds */);
 
-            if(httpCall.getBody() != null && httpCall.getMethodType() != HttpCall.GET)
+            if (httpCall.getBody() != null && httpCall.getMethodType() != HttpCall.GET)
             {
                 urlConnection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
                 os = urlConnection.getOutputStream();
@@ -109,23 +107,21 @@ class HttpRequest extends AsyncTask<HttpCall, String, String>
     /**
      * Create the string for parameters in the URL based on the attributes and data passed by the caller
      * */
-    private String getDataString(HashMap<String, String> params, int methodType) throws UnsupportedEncodingException
+    private String getDataString(Map<String, String> params) throws UnsupportedEncodingException
     {
+        if (params == null)
+        {
+            return "";
+        }
         StringBuilder result = new StringBuilder();
         boolean isFirst = true;
         for(Map.Entry<String, String> entry : params.entrySet())
         {
-            if(isFirst)
-            {
-                isFirst = false;
-                if(methodType == HttpCall.GET)
-                    result.append("?");
-                else
-                    result.append("&");
-                result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
-                result.append("=");
-                result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
-            }
+            result.append(isFirst? "?":"&");
+            isFirst = false;
+            result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
+            result.append("=");
+            result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
         }
         return result.toString();
     }