Advertisement
Guest User

Untitled

a guest
Mar 1st, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. public class OpenERPConnection extends AsyncTask {
  2. static final String TAG = "ERP";
  3. static final String HOST = "http://test.openerp.verkest.fr";
  4. static final String REQUEST_AUTHENTICATE = "/web/session/authenticate";
  5. static final String REQUEST_SEARCH_READ = "/web/dataset/search_read";
  6. static final String DATABASE = "erp";
  7. static final String USER = "ME";
  8. static final String PASSWORD = "ME123";
  9. static final String method = "call";
  10.  
  11.  
  12. void doInBackground(Context... params) throws Exception{
  13. //The http client that allow to keep the session open over requests
  14. HttpClient httpClient = new DefaultHttpClient();
  15.  
  16. JSONRPCHttpClient jsonRPC_client = new JSONRPCHttpClient(httpClient, HOST + REQUEST_AUTHENTICATE);
  17. jsonRPC_client.setConnectionTimeout(2000);
  18. jsonRPC_client.setSoTimeout(2000);
  19. jsonRPC_client.setEncoding("UTF-8");
  20.  
  21. try {
  22. //set params to send to OpenERP to create the connection
  23. JSONObject jsonParams = new JSONObject();
  24. jsonParams.put("db", DATABASE);
  25. jsonParams.put("login", USER);
  26. jsonParams.put("password",USER);
  27.  
  28. //Do the connection with openERP
  29. JSONObject json_result = jsonRPC_client.callJSONObject(method, jsonParams);
  30. Log.d(TAG, "We are connect to OpenERP, session id: " + json_result.getString("session_id"));
  31.  
  32. //Now we can request partner using the same session (http client)
  33. jsonRPC_client = new JSONRPCHttpClient(httpClient, HOST + REQUEST_SEARCH_READ);
  34. jsonRPC_client.setConnectionTimeout(2000);
  35. jsonRPC_client.setSoTimeout(2000);
  36. jsonRPC_client.setEncoding("UTF-8");
  37.  
  38. //get the user_context from the connection result, to send to OpenERP in the next request
  39. JSONObject context = json_result.getJSONObject("user_context");
  40.  
  41. //set params to send to OpenERP service
  42. jsonParams = new JSONObject();
  43. jsonParams.put("session_id", json_result.getString("session_id"));
  44. jsonParams.put("context", context);
  45. jsonParams.put("model", "res.partner");
  46. jsonParams.put("limit",10);
  47.  
  48. //Domain is use in openerp to define the where sql
  49. JSONArray domain = new JSONArray();
  50. JSONArray customer = new JSONArray();
  51. JSONArray person = new JSONArray();
  52. //Get only customers
  53. customer.put("customer");
  54. customer.put("=");
  55. customer.put("1");
  56. domain.put(customer);
  57. //Get only person (no companies)
  58. person.put("is_company");
  59. person.put("=");
  60. person.put("0");
  61. domain.put(customer);
  62. //domain : [[customer, =, 1],[is_company, =, 0]]
  63. //SQL likes : WHERE customer = 1 AND is_company = 0
  64. jsonParams.put("domain",domain);
  65.  
  66. //choose fields you want to get
  67. JSONArray fields = new JSONArray();
  68. fields.put("name");
  69. fields.put("city");
  70. jsonParams.put("fields", fields);
  71.  
  72. //jsonParams.put("offset",0);
  73. jsonParams.put("sort","write_date desc");
  74.  
  75. //send the request to openerp
  76. json_result = jsonRPC_client.callJSONObject(method, jsonParams);
  77. JSONArray customers = json_result.getJSONArray("records");
  78. JSONObject oerp_customer;
  79. for(int i =0 ; i<10;i++){
  80. oerp_customer = customers.getJSONObject(i);
  81. Log.d(TAG, oerp_customer.getString("name") + " lives in " + oerp_customer.getString("city"));
  82. }
  83.  
  84.  
  85. } catch (JSONException e) {
  86. Log.e(TAG,"Json exception: " + e.getMessage(), e);
  87. } catch (JSONRPCException e) {
  88. Log.e(TAG,"Json-rpc exception: " + e.getMessage(), e);
  89. }
  90.  
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement