A small client to send requests from android to your symfony apps (symfony's REST pattern compliant).
sfClient (singleton to store session) :
package org.me.sfBackendClient; import org.apache.http.impl.client.DefaultHttpClient; /** * symfony application client singleton * thanks to Cansin http://senior.ceng.metu.edu.tr/2009/praeda/2009/01/11/a-simple-restful-client-at-android/ * @author jean-philippe serafin <jean-philippe.serafin@dev-solutions.fr> */ public class sfClient{ private static sfClient instance; protected DefaultHttpClient httpClient; /** * constructor */ private sfClient(){ this.httpClient = new DefaultHttpClient(); } /** * instance accessor */ public static sfClient getInstance(){ if(null == instance){ instance = new sfClient(); } return instance; } /** * creating new request */ public sfRequest createRequest(){ sfRequest request = new sfRequest(this.httpClient); return request; } }
sfRequest :
package org.me.sfBackendClient; //IO import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; //utils import java.util.List; import java.util.ArrayList; //apache import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.*; import org.apache.http.NameValuePair; import org.apache.http.message.BasicNameValuePair; import org.apache.http.impl.client.DefaultHttpClient; /** * symfony request * thanks to Cansin http://senior.ceng.metu.edu.tr/2009/praeda/2009/01/11/a-simple-restful-client-at-android/ * @author jean-philippe serafin <jean-philippe.serafin@dev-solutions.fr> */ public class sfRequest{ protected List<NameValuePair> params; protected HttpGet getRequest; protected HttpPost postRequest; protected HttpResponse response; protected DefaultHttpClient httpClient; /** * constructor */ public sfRequest(DefaultHttpClient client){ this.httpClient = client; this.params = new ArrayList<NameValuePair>(2); } this.url = url; } return this.url; } this.method = method; } return this.method; } /** * Adding parameter * @param key * @param value */ this.params.add(new BasicNameValuePair(key, value)); } /** * getting response text * @return String response */ return this.result; } /** * executing request */ public void execute(){ try { if(this.method.compareToIgnoreCase("GET") == 0){ this.getRequest = new HttpGet(this.getUrl()); this.response = this.httpClient.execute(this.getRequest); }else if(this.method.compareToIgnoreCase("POST") == 0 || this.method.compareToIgnoreCase("PUT") == 0 || this.method.compareToIgnoreCase("DELETE") == 0){ this.addParam("sf_method", this.getMethod()); this.postRequest = new HttpPost(this.getUrl()); this.postRequest.setEntity(new UrlEncodedFormEntity(this.params)); this.response = this.httpClient.execute(this.postRequest); } HttpEntity entity = response.getEntity(); if(entity != null){ this.result = convertStreamToString(inputStream); } } catch (ClientProtocolException e) { //e.printStackTrace(); this.result = e.getMessage(); this.result = e.getMessage(); } } /** * converting stream reader to string * @return String */ StringBuilder stringBuilder = new StringBuilder(); try { while ((line = reader.readLine()) != null) { stringBuilder.append(line + "\n"); } e.printStackTrace(); } finally { try { is.close(); e.printStackTrace(); } } return stringBuilder.toString(); } }
Usage :
sfRequest request = sfClient.getInstance().createRequest(); request.setUrl("http://xxxxxxxxx/androidConnect/connect"); request.setMethod("POST"); request.addParam("login", loginField.getText().toString()); request.addParam("password", passwordField.getText().toString()); request.execute();
thanks to Cansin



