private void loadResources(ResourcesMapView mapView) { ContentResolver cr = getContentResolver(); AndroidDBResourceManager resourceManager = new AndroidDBResourceManager(cr, this); Resource[] resources = resourceManager.loadResources(); if(resources.length == 0){//first timer or corrupted local data AndroidLoadFromServerResourceManager fromServerManager = new AndroidLoadFromServerResourceManager(); resources = fromServerManager.loadResources(); } ResourcesOverlays[] resourcesOverlays = new GoogleMapResource(resources, this).getResourcesOverlays(); for (int i = 0; i < resourcesOverlays.length; i++) { mapView.getOverlays().add(resourcesOverlays[i]); } }
package com.jleo.collectme.android; import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; import com.jleo.UserInfo; import com.jleo.collectme.model.Point; import com.jleo.collectme.model.Resource; import com.jleo.collectme.model.ResourceType; import com.jleo.collectme.model.ResourcesManager; import com.jleo.collectme.util.HttpClientUtil; public class AndroidLoadFromServerResourceManager implements ResourcesManager { public Resource[] loadResources() { final List<Resource> resourcesList = new ArrayList<Resource>(); Map<String, String> params = new HashMap<String,String>(); params.put("account", UserInfo.currentUserInfo.getAccount()); HttpClientUtil httpClient = new HttpClientUtil(); HttpClientUtil.Handler handler = new HttpClientUtil.Handler() { @Override public void onSucceed(InputStream is) { Document document; try { document = new SAXReader().read(is); } catch (DocumentException e) { return; } List<Element> resoucesElements = document.getRootElement().elements(); for (Element element : resoucesElements) { int id = Integer.valueOf(element.element("id").getText()); String title = element.element("title").getText(); String description = element.element("description").getText(); ResourceType resouceType = new ResourceType(id, description, title); List<Element> locationElements = element.element("locations").elements(); for (Element locationElement : locationElements) { int x = Integer.valueOf(locationElement.element("x").getText()); int y = Integer.valueOf(locationElement.element("y").getText()); Resource resouce = new Resource(resouceType,new Point(x, y)); resourcesList.add(resouce); } } } @Override public void onFailed() { //do nothing } }; httpClient.get(new Request("http://"+UserInfo.currentUserInfo.getServer() + "/pickitup/resource/load", params), handler); return resourcesList.toArray(new Resource[0]); } }
package com.jleo.collectme.android; import java.util.HashMap; import java.util.Map; public class Request { public Request(String url, Map<String, String> params) { this.url = url; this.params = params; } public String url; public Map<String, String> params = new HashMap<String, String>(); public String getUrl() { return url; } public Map<String, String> getParams() { return params; } }