Voici deux petites classes permettant de faire des requêtes sur une application symfony depuis android tout en respectant l'architecture REST des modules.
Le client (singleton pour conserver la 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; } }
Les requêtes :
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 String url; protected String method; protected List<NameValuePair> params; protected HttpGet getRequest; protected HttpPost postRequest; protected HttpResponse response; protected String result; protected DefaultHttpClient httpClient; /** * constructor */ public sfRequest(DefaultHttpClient client){ this.httpClient = client; this.params = new ArrayList<NameValuePair>(2); } public void setUrl(String url){ this.url = url; } public String getUrl(){ return this.url; } public void setMethod(String method){ this.method = method; } public String getMethod(){ return this.method; } /** * Adding parameter * @param key * @param value */ public void addParam(String key, String value){ this.params.add(new BasicNameValuePair(key, value)); } /** * getting response text * @return String response */ public String getResult(){ 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){ InputStream inputStream = entity.getContent(); this.result = convertStreamToString(inputStream); } } catch (ClientProtocolException e) { //e.printStackTrace(); this.result = e.getMessage(); } catch (IOException e) { this.result = e.getMessage(); } } /** * converting stream reader to string * @return String */ private static String convertStreamToString(InputStream is) { BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder stringBuilder = new StringBuilder(); String line = null; try { while ((line = reader.readLine()) != null) { stringBuilder.append(line + "\n"); } } catch (IOException e) { e.printStackTrace(); } finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } return stringBuilder.toString(); } }
L'utilisation est très simple :
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(); String message = request.getResult();
Merci à Cansin