Ver código fonte

Modified HTTP handler to accept JSON or strings in the case of error messages.

Thomas Flucke 9 anos atrás
pai
commit
9ef9f2697d

+ 2 - 2
app/src/main/java/com/example/yiupang/freefoodfinder/EventsScreen.java

@@ -39,7 +39,7 @@ public class EventsScreen extends Fragment
         httpCall.setUrl("http://free-food-finder.herokuapp.com/events");
         httpCall.setUrl("http://free-food-finder.herokuapp.com/events");
         new HttpRequest(){
         new HttpRequest(){
             @Override
             @Override
-            public void onResponse(JsonNode response, int code)
+            public void onResponse(Object response, int code)
             {
             {
                 super.onResponse(response, code);
                 super.onResponse(response, code);
                 if (code != HttpURLConnection.HTTP_OK)
                 if (code != HttpURLConnection.HTTP_OK)
@@ -53,7 +53,7 @@ public class EventsScreen extends Fragment
                     try {
                     try {
                         events = mapper.reader(
                         events = mapper.reader(
                                 typeFactory.constructCollectionType(List.class, Event.class)
                                 typeFactory.constructCollectionType(List.class, Event.class)
-                        ).readValue(response);
+                        ).readValue((JsonNode) response);
                     } catch (IOException e) {
                     } catch (IOException e) {
                         /*handle error*/
                         /*handle error*/
                     }
                     }

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

@@ -23,7 +23,7 @@ class HttpRequest extends AsyncTask<HttpCall, String, String>
 {
 {
     private static final String UTF_8 = "UTF-8";
     private static final String UTF_8 = "UTF-8";
 
 
-    private JsonNode response;
+    private Object response;
     private int responseCode;
     private int responseCode;
 
 
     /**
     /**
@@ -62,8 +62,15 @@ class HttpRequest extends AsyncTask<HttpCall, String, String>
 
 
             /*Handle the response*/
             /*Handle the response*/
             responseCode = urlConnection.getResponseCode();
             responseCode = urlConnection.getResponseCode();
-            ObjectMapper mapper = new ObjectMapper();
-            response = mapper.readTree(urlConnection.getInputStream());
+            if (responseCode >= 300)
+            {
+                ObjectMapper mapper = new ObjectMapper();
+                response = mapper.readTree(urlConnection.getInputStream());
+            }
+            else
+            {
+                response = urlConnection.getResponseMessage();
+            }
         }
         }
         catch (Exception e)
         catch (Exception e)
         {
         {
@@ -94,7 +101,7 @@ class HttpRequest extends AsyncTask<HttpCall, String, String>
     /**
     /**
      * It needs to be overwritten by the caller to handle the response
      * It needs to be overwritten by the caller to handle the response
     * */
     * */
-    public void onResponse(JsonNode response, int code)
+    public void onResponse(Object response, int code)
     {
     {
 
 
     }
     }