浏览代码

I modified the HTTP interface to support PUT and DELETE, and also switched to using JSON as the method body to be compatable with server expected format.

Thomas Flucke 9 年之前
父节点
当前提交
62f78a3b77

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

@@ -1,5 +1,7 @@
 package com.example.yiupang.freefoodfinder;
 
+import com.fasterxml.jackson.databind.JsonNode;
+
 import java.util.HashMap;
 
 /**
@@ -11,10 +13,29 @@ class HttpCall
 {
     static final int GET = 1;
     static final int POST = 2;
+    static final int PUT = 3;
+    static final int DELETE = 4;
+
+    public static String methodToStr(int method)
+    {
+        switch (method)
+        {
+            case GET:
+                return "GET";
+            case POST:
+                return "POST";
+            case PUT:
+                return "PUT";
+            case DELETE:
+                return "DELETE";
+            default:
+                throw new IllegalArgumentException("Invalid method code");
+        }
+    }
 
     private String url;
     private int methodType;
-    private HashMap<String, String> params;
+    private JsonNode body;
 
     String getUrl() {
         return url;
@@ -32,11 +53,11 @@ class HttpCall
         this.methodType = methodType;
     }
 
-    HashMap<String, String> getParams() {
-        return params;
+    JsonNode getBody() {
+        return body;
     }
 
-    public void setParams(HashMap<String, String> params) {
-        this.params = params;
+    public void setBody(JsonNode params) {
+        this.body = params;
     }
 }

+ 5 - 11
app/src/main/java/com/example/yiupang/freefoodfinder/HttpRequest.java

@@ -42,29 +42,23 @@ class HttpRequest extends AsyncTask<HttpCall, String, String>
         try
         {
             HttpCall httpCall = params[0];
-            String dataParams = "";
-            boolean hasDataFromCaller = httpCall.getParams() != null;
             URL url;
             OutputStream os;
             BufferedWriter writer;
             int responseCode;
 
-            if(hasDataFromCaller)/*if no data is passed from the caller*/
-            {
-                dataParams = getDataString(httpCall.getParams(), httpCall.getMethodType());
-            }
-
-            url = new URL(httpCall.getMethodType() == HttpCall.GET ? httpCall.getUrl() + dataParams : httpCall.getUrl());
+            url = new URL(httpCall.getUrl());
             urlConnection = (HttpURLConnection) url.openConnection();
-            urlConnection.setRequestMethod(httpCall.getMethodType() == HttpCall.GET ? "GET":"POST");
+            urlConnection.setRequestMethod(HttpCall.methodToStr(httpCall.getMethodType()));
             urlConnection.setReadTimeout(10000 /* milliseconds */);
             urlConnection.setConnectTimeout(15000 /* milliseconds */);
 
-            if(hasDataFromCaller && httpCall.getMethodType() == HttpCall.POST)
+            if(httpCall.getBody() != null && httpCall.getMethodType() != HttpCall.GET)
             {
+                urlConnection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
                 os = urlConnection.getOutputStream();
                 writer = new BufferedWriter(new OutputStreamWriter(os, UTF_8));
-                writer.append(dataParams);
+                writer.append(httpCall.getBody().toString());
                 writer.flush();
                 writer.close();
                 os.close();