Back to project page foodroid.
The source code is released under:
GNU General Public License
If you think the Android project foodroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.wmc.ReservationClient; //from ww w. j a va2s . co m import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.StringReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.List; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.ResponseHandler; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPut; import org.apache.http.impl.client.BasicResponseHandler; import org.apache.http.impl.client.DefaultHttpClient; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; import android.content.Context; public class Update { private List<Branch> branchs=new ArrayList<Branch>(); private List<Comment> comments=new ArrayList<Comment>(); private List<Food> foods=new ArrayList<Food>(); String Server; String[] items; public Update(Context context) { Server = context.getString(R.string.Server); } public void updatePictures(Context context) { try { String urlImage = "http://" + Server + "/RestaurantReservation/" + "picture/1.jpg"; URL ulrn = new URL(urlImage); HttpURLConnection con = (HttpURLConnection)ulrn.openConnection(); InputStream in = con.getInputStream(); String fullPath = "/data/data/com.wmc.ReservationClient/"; OutputStream out = new FileOutputStream(fullPath + "/" + "picture/1.jpg"); byte[] buffer = new byte[1024]; int length; while ((length = in.read(buffer)) != -1) { out.write(buffer, 0, length); } in.close(); in = null; out.flush(); out.close(); out = null; } catch(Exception ex) { } } public void UpdateBranche() throws Exception { HttpClient client=new DefaultHttpClient(); HttpGet getMethod=new HttpGet( "http://" + Server + "/RestaurantReservation/resources/branchlist/*"); ResponseHandler<String> responseHandler=new BasicResponseHandler(); try { String responseBody=client.execute(getMethod, responseHandler); responseBody = responseBody.replace("<", "<").replace(">", ">").replace("&", "&"); getWebServiceBranchs(responseBody); items = new String[branchs.size()]; for(int i = 0; i< branchs.size(); i++) items[i] = branchs.get(i).name; } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } void getWebServiceBranchs(String raw) throws Exception { branchs.clear(); String value = ""; DocumentBuilder builder=DocumentBuilderFactory .newInstance() .newDocumentBuilder(); Document doc=builder.parse(new InputSource(new StringReader(raw))); NodeList IDs=doc.getElementsByTagName("ID"); for (int i=0;i<IDs.getLength();i++) { Element id=(Element)IDs.item(i); Branch branch=new Branch(); branchs.add(branch); value = ""; for(int j=0;j<id.getChildNodes().getLength();j++) value += id.getChildNodes().item(j).getNodeValue(); branch.id = value; } NodeList names=doc.getElementsByTagName("Name"); for (int i=0;i<names.getLength();i++) { Element name=(Element)names.item(i); Branch branch=branchs.get(i); value = ""; for(int j=0;j<name.getChildNodes().getLength();j++) value += name.getChildNodes().item(j).getNodeValue(); branch.name = value; } NodeList pictures=doc.getElementsByTagName("Picture"); for (int i=0;i<pictures.getLength();i++) { Element picture=(Element)pictures.item(i); Branch branch=branchs.get(i); value = ""; for(int j=0;j<picture.getChildNodes().getLength();j++) value += picture.getChildNodes().item(j).getNodeValue(); branch.picture = value; } NodeList tels=doc.getElementsByTagName("Tel"); for (int i=0;i<tels.getLength();i++) { Element tel=(Element)tels.item(i); Branch branch=branchs.get(i); value = ""; for(int j=0;j<tel.getChildNodes().getLength();j++) value += tel.getChildNodes().item(j).getNodeValue(); branch.tel = value; } NodeList addresses=doc.getElementsByTagName("Address"); for (int i=0;i<addresses.getLength();i++) { Element address=(Element)addresses.item(i); Branch branch=branchs.get(i); value = ""; for(int j=0;j<address.getChildNodes().getLength();j++) value += address.getChildNodes().item(j).getNodeValue(); branch.address = value; } } void UpdateComment() throws Exception { HttpClient client=new DefaultHttpClient(); HttpGet getMethod=new HttpGet( "http://" + Server + "/RestaurantReservation/resources/commentlist/" + "1"/*branchID*/); ResponseHandler<String> responseHandler=new BasicResponseHandler(); try { String responseBody=client.execute(getMethod, responseHandler); responseBody = responseBody.replace("<", "<").replace(">", ">").replace("&", "&"); getWebServiceComment(responseBody); String[] commentItems = new String[comments.size()]; for(int i = 0; i< comments.size(); i++) commentItems[i] = comments.get(i).message; } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } void getWebServiceComment(String raw) throws Exception { comments.clear(); String value = ""; DocumentBuilder builder=DocumentBuilderFactory .newInstance() .newDocumentBuilder(); Document doc=builder.parse(new InputSource(new StringReader(raw))); NodeList IDs=doc.getElementsByTagName("BranchID"); for (int i=0;i<IDs.getLength();i++) { Element id=(Element)IDs.item(i); Comment comment =new Comment(); comments.add(comment); value = ""; for(int j=0;j<id.getChildNodes().getLength();j++) value += id.getChildNodes().item(j).getNodeValue(); comment.id = value; } NodeList messages=doc.getElementsByTagName("message"); for (int i=0;i<messages.getLength();i++) { Element message=(Element)messages.item(i); Comment comment =comments.get(i); value = ""; for(int j=0;j<message.getChildNodes().getLength();j++) value += message.getChildNodes().item(j).getNodeValue(); comment.message = value; } NodeList rates=doc.getElementsByTagName("rate"); for (int i=0;i<rates.getLength();i++) { Element rate=(Element)rates.item(i); Comment comment=comments.get(i); value = ""; for(int j=0;j<rate.getChildNodes().getLength();j++) value += rate.getChildNodes().item(j).getNodeValue(); comment.rate = value; } } void UpdateFood() throws Exception { HttpClient client=new DefaultHttpClient(); HttpGet getMethod=new HttpGet( "http://" + Server + "/RestaurantReservation/resources/foodlist/" + "1"/*branchID*/); ResponseHandler<String> responseHandler=new BasicResponseHandler(); try { String responseBody=client.execute(getMethod, responseHandler); responseBody = responseBody.replace("<", "<").replace(">", ">").replace("&", "&"); getWebServiceFood(responseBody); String[] foodItems = new String[foods.size()]; for(int i = 0; i< foods.size(); i++) foodItems[i] = foods.get(i).Name; } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } void getWebServiceFood(String raw) throws Exception { foods.clear(); String value = ""; DocumentBuilder builder=DocumentBuilderFactory .newInstance() .newDocumentBuilder(); Document doc=builder.parse(new InputSource(new StringReader(raw))); NodeList IDs=doc.getElementsByTagName("BranchID"); for (int i=0;i<IDs.getLength();i++) { Element id=(Element)IDs.item(i); Food food=new Food(); foods.add(food); value = ""; for(int j=0;j<id.getChildNodes().getLength();j++) value += id.getChildNodes().item(j).getNodeValue(); food.BranchID = value; } NodeList names=doc.getElementsByTagName("Name"); for (int i=0;i<names.getLength();i++) { Element name=(Element)names.item(i); Food food=foods.get(i); value = ""; for(int j=0;j<name.getChildNodes().getLength();j++) value += name.getChildNodes().item(j).getNodeValue(); food.Name = value; } NodeList prices=doc.getElementsByTagName("Price"); for (int i=0;i<prices.getLength();i++) { Element price=(Element)prices.item(i); Food food=foods.get(i); value = ""; for(int j=0;j<price.getChildNodes().getLength();j++) value += price.getChildNodes().item(j).getNodeValue(); food.Price = value; } NodeList pictures=doc.getElementsByTagName("Picture"); for (int i=0;i<pictures.getLength();i++) { Element picture=(Element)pictures.item(i); Food food=foods.get(i); value = ""; for(int j=0;j<picture.getChildNodes().getLength();j++) value += picture.getChildNodes().item(j).getNodeValue(); food.Picture = value; } } }