" A l'écoute des technologies... "

symfony android client

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

sfCmsSolution overview

Our new symfony powered CMS : sfCmsSolution

  • login : guest
  • password : guest

sfGridSolution beta 3 | grid state persistance

Another GridSolution beta introducing datagrid state persistance in cookie : samples.

  1. svn co http://svn-gridsolution.dev-solutions.fr/tags/beta-3

GridSolution beta

We've just tagged a beta release of sfGridSolutionPlugin our mootools datagrid.

You can checkout the symfony plugin :

  1. svn co http://svn-gridsolution.dev-solutions.fr/tags/beta-1

or just javascript files for use outside symfony

  1. svn co http://svn-gridsolution.dev-solutions.fr/tags/beta-1/sfGridSolutionPlugin/web/js/

we're now working on optimisation, documentation and examples.

GridSolutionCookie will be released ASAP for view state persistance.

sfGridSolutionPlugin alpha

Ca y est, voici la première version téléchargeable de sfGridSolutionPlugin. Attention, il s'agit d'une version alpha pas encore vraiment débuggée sur IE, des fonctionnalités risquent fortement de changer.

Vous pouvez jeter un oeil sur la démo.

Exemple d'implémentation :

Dans votre contrôleur symfony :

  1. /**
  2.   * getting main data
  3.   * @param sfWebRequest $request A request object
  4.   */
  5. public function executeGetData(sfWebRequest $request)
  6. {
  7. //initialise
  8. $gridAction = new sfGridSolutionQuery($request);
  9. //defining get datas query
  10. $gridAction->setQuery(
  11. Doctrine_Query::create()
  12. ->select('*')
  13. ->from('Exemple e')
  14. );
  15. die($gridAction->execute());
  16. }
  17. /**
  18.   * Showing demo grid
  19.   * @param sfRequest $request A request object
  20.   */
  21. public function executeIndex(sfWebRequest $request)
  22. {
  23. //init grid
  24. $this->grid = new sfGridSolution('grid', 'mygrid', array(
  25. 'showHeader' => true,
  26. 'resizeColumns' => true,
  27. 'selectRow' => true,
  28. 'sortOn' => 'id',
  29. 'sortBy' => 'DESC',
  30. 'getDataUrl' => 'Exemple/getData', // => route to find datas
  31. 'saveDataUrl' => 'Exemple/saveData', // => route to save data
  32. 'getDataMethod' => 'post',
  33. 'page' => 1,
  34. 'perPageOptions' => array(10, 15, 20, 50),
  35. 'perPage' => 20,
  36. 'height' => 300,
  37. 'imagesPath' => 'sfGridSolutionPlugin/images'
  38. ));
  39. //adding id
  40. $this->grid->addGroup(
  41. new sfGridSolutionGroup(
  42. 'label' => '&nbsp;',
  43. 'isResizable' => true,
  44. 'cssClass' => 'group',
  45. 'columns' => array(
  46. new sfGridSolutionColumn(
  47. 'label' => 'id',
  48. 'model' => 'id',
  49. 'align' => 'right',
  50. 'filterable' => true,
  51. 'sortable' => true,
  52. 'width' => '90',
  53. 'actions' => array(
  54. new sfGridSolutionAction(
  55. 'title' => 'Show details',
  56. 'method' => 'showDetails', // => will call javascript showDetails() method
  57. 'icon' => 'viewmag.png'
  58. )
  59. )
  60. )
  61. )
  62. )
  63. )
  64. )
  65. )
  66. );
  67. //group1
  68. $this->grid->addGroup(
  69. new sfGridSolutionGroup(
  70. 'label' => 'group1',
  71. 'isResizable' => true,
  72. 'cssClass' => 'group',
  73. 'collapsable' => 'true',
  74.  
  75. 'collapse' => false,
  76. 'columns' => array(
  77. new sfGridSolutionColumn(
  78. 'label' => 'int_field1_1',
  79. 'model' => 'int_field1_1',
  80. 'align' => 'right',
  81. 'filterable' => false,
  82. 'sortable' => true,
  83. 'width' => '90'
  84. )
  85. ),
  86. new sfGridSolutionColumn(
  87. 'label' => 'char_field1_2',
  88. 'model' => 'char_field1_2',
  89. 'align' => 'right',
  90. 'filterable' => true,
  91. 'sortable' => true,
  92. 'editable' => true,
  93. 'width' => '90'
  94. )
  95. ),
  96. new sfGridSolutionColumn(
  97. 'label' => 'date_field1_3',
  98. 'model' => 'date_field1_3',
  99. 'editable' => true,
  100. 'align' => 'left',
  101. 'filterClass' => 'DateFilter',
  102. 'filterable' => true,
  103. 'sortable' => true,
  104. 'width' => '130'
  105. )
  106. )
  107. )
  108. )
  109. )
  110. );

Dans la vue :

  1. <h1>Demo Data Grid</h1>
  2.  
  3. <div id="mygrid" style=""></div>
  4.  
  5. <?php echo $grid->render();?>
  6.  
  7. <script type="text/javascript">
  8. function showDetails(event, action, cell){
  9. cell.row.buildExpandTip('Exemple/getDetails?id=' + cell.row.datas['id'].value);
  10. }
  11. </script>

Un peu plus de doc verra le jour au fur et à mesure du développement.

- page 1 de 6