A small client to send requests from android to your symfony apps (symfony's REST pattern compliant).

sfClient (singleton to store session) :

  1. package org.me.sfBackendClient;
  2.  
  3. import org.apache.http.impl.client.DefaultHttpClient;
  4.  
  5. /**
  6.  * symfony application client singleton
  7.  * thanks to Cansin http://senior.ceng.metu.edu.tr/2009/praeda/2009/01/11/a-simple-restful-client-at-android/
  8.  * @author jean-philippe serafin <jean-philippe.serafin@dev-solutions.fr>
  9.  */
  10. public class sfClient{
  11. private static sfClient instance;
  12. protected DefaultHttpClient httpClient;
  13. /**
  14.   * constructor
  15.   */
  16. private sfClient(){
  17. this.httpClient = new DefaultHttpClient();
  18. }
  19. /**
  20.   * instance accessor
  21.   */
  22. public static sfClient getInstance(){
  23. if(null == instance){
  24. instance = new sfClient();
  25. }
  26. return instance;
  27. }
  28. /**
  29.   * creating new request
  30.   */
  31. public sfRequest createRequest(){
  32. sfRequest request = new sfRequest(this.httpClient);
  33. return request;
  34. }
  35. }

sfRequest :

  1. package org.me.sfBackendClient;
  2. //IO
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. //utils
  8. import java.util.List;
  9. import java.util.ArrayList;
  10. //apache
  11. import org.apache.http.HttpEntity;
  12. import org.apache.http.HttpResponse;
  13. import org.apache.http.client.ClientProtocolException;
  14. import org.apache.http.client.entity.UrlEncodedFormEntity;
  15. import org.apache.http.client.methods.*;
  16. import org.apache.http.NameValuePair;
  17. import org.apache.http.message.BasicNameValuePair;
  18. import org.apache.http.impl.client.DefaultHttpClient;
  19.  
  20. /**
  21.  * symfony request
  22.  * thanks to Cansin http://senior.ceng.metu.edu.tr/2009/praeda/2009/01/11/a-simple-restful-client-at-android/
  23.  * @author jean-philippe serafin <jean-philippe.serafin@dev-solutions.fr>
  24.  */
  25. public class sfRequest{
  26. protected String url;
  27. protected String method;
  28. protected List<NameValuePair> params;
  29. protected HttpGet getRequest;
  30. protected HttpPost postRequest;
  31. protected HttpResponse response;
  32. protected String result;
  33. protected DefaultHttpClient httpClient;
  34.  
  35. /**
  36.   * constructor
  37.   */
  38. public sfRequest(DefaultHttpClient client){
  39. this.httpClient = client;
  40. this.params = new ArrayList<NameValuePair>(2);
  41. }
  42. public void setUrl(String url){
  43. this.url = url;
  44. }
  45. public String getUrl(){
  46. return this.url;
  47. }
  48. public void setMethod(String method){
  49. this.method = method;
  50. }
  51. public String getMethod(){
  52. return this.method;
  53. }
  54. /**
  55.   * Adding parameter
  56.   * @param key
  57.   * @param value
  58.   */
  59. public void addParam(String key, String value){
  60. this.params.add(new BasicNameValuePair(key, value));
  61. }
  62. /**
  63.   * getting response text
  64.   * @return String response
  65.   */
  66. public String getResult(){
  67. return this.result;
  68. }
  69. /**
  70.   * executing request
  71.   */
  72. public void execute(){
  73. try {
  74. if(this.method.compareToIgnoreCase("GET") == 0){
  75. this.getRequest = new HttpGet(this.getUrl());
  76. this.response = this.httpClient.execute(this.getRequest);
  77. }else if(this.method.compareToIgnoreCase("POST") == 0
  78. || this.method.compareToIgnoreCase("PUT") == 0
  79. || this.method.compareToIgnoreCase("DELETE") == 0){
  80. this.addParam("sf_method", this.getMethod());
  81. this.postRequest = new HttpPost(this.getUrl());
  82. this.postRequest.setEntity(new UrlEncodedFormEntity(this.params));
  83. this.response = this.httpClient.execute(this.postRequest);
  84. }
  85. HttpEntity entity = response.getEntity();
  86. if(entity != null){
  87. InputStream inputStream = entity.getContent();
  88. this.result = convertStreamToString(inputStream);
  89. }
  90. } catch (ClientProtocolException e) {
  91. //e.printStackTrace();
  92. this.result = e.getMessage();
  93. } catch (IOException e) {
  94. this.result = e.getMessage();
  95. }
  96. }
  97. /**
  98.   * converting stream reader to string
  99.   * @return String
  100.   */
  101. private static String convertStreamToString(InputStream is) {
  102. StringBuilder stringBuilder = new StringBuilder();
  103.  
  104. String line = null;
  105. try {
  106. while ((line = reader.readLine()) != null) {
  107. stringBuilder.append(line + "\n");
  108. }
  109. } catch (IOException e) {
  110. e.printStackTrace();
  111. } finally {
  112. try {
  113. is.close();
  114. } catch (IOException e) {
  115. e.printStackTrace();
  116. }
  117. }
  118. return stringBuilder.toString();
  119. }
  120. }

Usage :

  1. sfRequest request = sfClient.getInstance().createRequest();
  2. request.setUrl("http://xxxxxxxxx/androidConnect/connect");
  3. request.setMethod("POST");
  4. request.addParam("login", loginField.getText().toString());
  5. request.addParam("password", passwordField.getText().toString());
  6. request.execute();
  7. String message = request.getResult();

thanks to Cansin