فهرست منبع

attempt to fix a smell

Yiupang 9 سال پیش
والد
کامیت
e364099b69

+ 9 - 0
app/src/main/java/com/example/yiupang/freefoodfinder/Event.java

@@ -1,6 +1,14 @@
 package com.example.yiupang.freefoodfinder;
 
+import android.util.Log;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.type.TypeFactory;
+
+import java.io.IOException;
 import java.util.Date;
+import java.util.List;
 
 class Event
 {
@@ -93,6 +101,7 @@ class Event
     public String getPlace(){return this.place;}
     public void setPlace(String place){this.place = place;}
 
+
     @Override
     public String toString(){
         return "name: " + this.name;

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

@@ -13,10 +13,7 @@ import android.widget.AdapterView;
 import android.widget.ListView;
 
 import com.fasterxml.jackson.databind.JsonNode;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.databind.type.TypeFactory;
 
-import java.io.IOException;
 import java.net.HttpURLConnection;
 import java.util.List;
 
@@ -72,7 +69,7 @@ public class EventsScreen extends Fragment
     private class HttpRequestSpecial extends HttpRequest {
         View view;
 
-        public HttpRequestSpecial(View view) {
+        private HttpRequestSpecial(View view) {
             this.view = view;
         }
 
@@ -81,19 +78,11 @@ public class EventsScreen extends Fragment
             if (code != HttpURLConnection.HTTP_OK)
                 Log.d("ON RESPONSE ERROR", "HTTP ERR: NOT OK");
             else {
-                ObjectMapper mapper = new ObjectMapper();
-                TypeFactory typeFactory = mapper.getTypeFactory();
-                List<Event> events = null; /*Parse to Event Objs*/
-                try {
-                    events = mapper.reader(
-                            typeFactory.constructCollectionType(List.class, Event.class)
-                    ).readValue((JsonNode) response);
-                    ListView listView = (ListView) view.findViewById(R.id.events_screen);
-                    listView.setAdapter(new EventArrayAdapter(view.getContext(), R.layout.events_list_item, events));
-                    setItemListener(listView);
-                } catch (IOException e) {
-                    Log.d("size:  ", e + "");
-                }
+
+                List<Event> events = Utility.parseFromJSONToEventObjs((JsonNode) response);
+                ListView listView = (ListView) view.findViewById(R.id.events_screen);
+                listView.setAdapter(new EventArrayAdapter(view.getContext(), R.layout.events_list_item, events));
+                setItemListener(listView);
             }
         }
     }

+ 9 - 27
app/src/main/java/com/example/yiupang/freefoodfinder/MapScreen.java

@@ -8,8 +8,6 @@ import android.view.View;
 import android.view.ViewGroup;
 
 import com.fasterxml.jackson.databind.JsonNode;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.databind.type.TypeFactory;
 import com.google.android.gms.maps.CameraUpdateFactory;
 import com.google.android.gms.maps.GoogleMap;
 import com.google.android.gms.maps.OnMapReadyCallback;
@@ -17,7 +15,6 @@ import com.google.android.gms.maps.SupportMapFragment;
 import com.google.android.gms.maps.model.LatLng;
 import com.google.android.gms.maps.model.MarkerOptions;
 
-import java.io.IOException;
 import java.net.HttpURLConnection;
 import java.util.List;
 import java.util.ArrayList;
@@ -43,7 +40,6 @@ public class MapScreen extends android.support.v4.app.Fragment implements OnMapR
         SupportMapFragment mapFragment = (SupportMapFragment) fragmentManager.findFragmentById(R.id.map);
         mapFragment.getMapAsync(this);
 
-
         HttpCall httpCall = new HttpCall();
         httpCall.setMethodType(HttpCall.GET);
         httpCall.setUrl("http://free-food-finder.herokuapp.com/events");
@@ -53,30 +49,16 @@ public class MapScreen extends android.support.v4.app.Fragment implements OnMapR
             @Override
             public void onResponse(Object response, int code) {
                 super.onResponse(response, code);
-                if (code != HttpURLConnection.HTTP_OK) {
+                if (code != HttpURLConnection.HTTP_OK)
+                {
                     Log.d("ON RESPONSE ERROR", "HTTP ERR: NOT OK");
-                } else {
-                    ObjectMapper mapper = new ObjectMapper();
-                    TypeFactory typeFactory = mapper.getTypeFactory();
-                    List<Event> events = null;/*Parse to Event Objs*/
-                    try {
-
-                        events = mapper.reader(
-                                typeFactory.constructCollectionType(List.class, Event.class)
-                        ).readValue((JsonNode) response);
-
-                        Log.d("EVENTS NUM", "Events #: " + events.size());
-
-                        for(int i = 0; i < events.size(); i++)
-                        {
-                            dropPin(events, i);
-                        }
-
-                    } catch (IOException e) {
-                        /*handle error*/
-                        Log.d("size:  ", e + "");
-                    }
-
+                }
+                else
+                {
+                    List<Event> events = Utility.parseFromJSONToEventObjs((JsonNode) response);
+                    Log.d("EVENTS NUM", "Events #: " + events.size());
+                    for(int i = 0; i < events.size(); i++)
+                        dropPin(events, i);
                 }
             }
 

+ 36 - 0
app/src/main/java/com/example/yiupang/freefoodfinder/Utility.java

@@ -0,0 +1,36 @@
+package com.example.yiupang.freefoodfinder;
+
+import android.util.Log;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.type.TypeFactory;
+
+import java.io.IOException;
+import java.util.List;
+
+/**
+ * Created by yiupang on 6/6/2017.
+ *
+ */
+
+public class Utility
+{
+    public static List<Event> parseFromJSONToEventObjs(JsonNode node)
+    {
+        ObjectMapper mapper = new ObjectMapper();
+        TypeFactory typeFactory = mapper.getTypeFactory();
+        try
+        {
+            return mapper.reader(
+                    typeFactory.constructCollectionType(List.class, Event.class)
+            ).readValue((JsonNode) node);
+        }
+        catch (IOException e)
+        {
+            Log.d("size:  ", e + "");
+        }
+        return null;
+    }
+
+}