Is there anyone integratted ODOO with Android?

夙愿已清 提交于 2019-12-10 10:07:28

问题


I am currently developing an android application for a client who is insisting to use Odoo for API.I don't have any single idea about it I am not getting it any even after referring this link.They provide a URL, Database name, username, and password.If any one did Odoo with Android before, Can you give any suggestions?


回答1:


There are a lot of ways to connect Android to Odoo. Here they are:

  1. Json-RPC
  2. XML-RPC (especially aXMLRPC, this is what I am using)
  3. There is also a framework called Odoo Mobile Framework . I have tried it but found a lot of issues and I was not able to get it work properly. You can find the documentation here.

Odoo has a Web Service API which is available for Python, Ruby, PHP and Java. I strongly recommend to take a look.

For my case, I have cloned the aXMLRPC git repository, created a package in my project and adapted the original package name. But recently I have found this on Stack Overflow explaining how to add aXMLRPC to your Android project using Gradle (I didn't give it a try yet).

Odoo had made available three endpoints:

  1. xmlrpc/2/db to get the list of available databases on your server, it does not require to be authenticated;
  2. xmlrpc/2/common to log in to the server, it does not require to be authenticated;
  3. xmlrpc/2/object, is used to call methods of odoo models via the execute_kw RPC function.

     public class OdooConnect {
        String url;
        private XMLRPCClient client;
    
        public OdooConnect(String serverAddress, String path) {
            url = serverAddress + "/xmlrpc/2/" + path;
            client = new XMLRPCClient(url);
        }
    
        public Object login(String db, String username, String password) {
            Object object;
            try {
                object = client.call("login", db, username, password);
                return object;
            } catch (XMLRPCException e) {
                e.printStackTrace();
            }
            return null;
        }
    
        public Object checkServer() {
            Object object;
            try {
                object = client.call("list", new Object[]{});
                return object;
            } catch (XMLRPCException e) {
                e.printStackTrace();
            }
            return null;
        }
    
    }
    

In this class, the constructor as arguments the server address (it can be http(s)://your_ip_address:the_port_number) and the path ('db', 'common' or 'object').

The checkServer method returns an object which is actually an array containing the list of available databases.

The login mehtod returns an Integer which is the Id of the authenticated user.

For the Odoo CRUD mehtods (search_read, search_count, search, write, create, unlink) you can take a look to the Odoo Web Service API Java code matching the method you want.

Here is an example of the search_read method. I assume that you've an XMLRPCClient named client.

public Object search_read(String db, int user_id, String password, String object, List conditions, Map<String, List> fields) {
    Object result = null;
    try {
        result = client.call("execute_kw", db, user_id, password, object, "search_read", conditions, fields);
    } catch (XMLRPCException e) {
        e.printStackTrace();
    }
    return result;
}

Where

  1. object is an Odoo model for example "res.partner"
  2. conditions is the domain (filter) something like this: Collections.singletonList(Collections.singletonList(Arrays.asList("supplier", "=", true)));
  3. fields, the fields you want to get,

    fields = new HashMap() {{put("fields", Arrays.asList("id","name","is_company","street")); }};

You must cast the result of the method to Object[] which will give you an array containing a list of objects each representing a record.

Object[] objects = (Object[]) result;
if (objects.length > 0) {
    for (Object object : objects) {
        String name= OdooUtil.getString((Map<String, Object>) object, "name");
        boolean is_company= OdooUtil.getBoolean((Map<String, Object>) object, "is_company");
        String street = OdooUtil.getString((Map<String, Object>) object, "street");  
        int id= OdooUtil.getInteger((Map<String, Object>) object, "id");
    }
}

Here the OdooUtil class

public class OdooUtil {

    public static String getString(Map<String, Object> map, String fieldName) {
        String res = "";
        if (map.get(fieldName) instanceof String) {
            res = (String) map.get(fieldName);
        }
        return res;
    }

    public static Integer getInteger(Map<String, Object> map, String fieldName) {
        Integer res = 0;
        if (map.get(fieldName) instanceof Integer) {
            res = (Integer) map.get(fieldName);
        }
        return res;
    }

    public static Double getDouble(Map<String, Object> map, String fieldName) {
        Double res = 0.0;
        if (map.get(fieldName) instanceof Double) {
            res = (Double) map.get(fieldName);
        }
        return res;
    }

    public static Boolean getBoolean(Map<String, Object> map, String fieldName) {
        Boolean res = false;
        if (map.get(fieldName) instanceof Boolean) {
            res = (Boolean) map.get(fieldName);
        }
        return res;
    }


    public static Float getFloat(Map<String, Object> map, String fieldName) {
        Float res = 0f;
        if (map.get(fieldName) instanceof Float) {
            res = (Float) map.get(fieldName);
        }
        return res;
    }
}

If you have a many2one field you only have access to the id and the name of the related record. You can use the following class to get the id and the name of the many2one record.

public class Many2One {
    private int id;
    private String name;

    public Many2One() {
    }

    public static Many2One getMany2One(Map<String, Object> stringObjectMap, String fieldName) {
        Integer fieldId = 0;
        String fieldValue = "";

        Many2One res = new Many2One();
        if (stringObjectMap.get(fieldName) instanceof Object[]) {
            Object[] field = (Object[]) stringObjectMap.get(fieldName);

            if (field.length > 0) {
                fieldId = (Integer) field[0];
                fieldValue = (String) field[1];
            }
        }

        res.id = fieldId;
        res.name = fieldValue;

        return res;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }
}

Example of usage of Many2One class

    String partner_name= Many2One.getMany2One((Map<String, Object>) object, "partner_id").getName();
    int partner_id= Many2One.getMany2One((Map<String, Object>) object, "partner_id").getId();

For other remaining CRUD methods, you can easily find a way how they work by reading the Odoo Web Service API documentation.

I hope this gives you some insights.




回答2:


This is Just an Example did to access contacts/partners from odoo:

#!/usr/bin/env python

import csv
from xmlrpclib import ServerProxy

SERVER = 'http://localhost:8069'
DATABASE = 'testcompany'
USERNAME = 'admin'
PASSWORD = 'password'

FILE_PATH = 'ODOO_clientsMain2_test.csv'

server = ServerProxy('http://localhost:8069/xmlrpc/common')
user_id = server.login(DATABASE, USERNAME, PASSWORD)

server = ServerProxy('http://localhost:8069/xmlrpc/object')

def search(list, key):
    for item in list:
        return item[key]

reader = csv.reader(open(FILE_PATH,'rb'))

for row in reader:
    #print row
    partner_template = {
        'name': row[0],
        #'company_id': row[1],
    }
    if row[2] is not None and row[2]<>'':
        partner_template.update({'email': row[2]})
    if row[5] is not None and row[5]<>'':
        partner_template.update({'tin': row[5]})
    if row[6] is not None and row[6]<>'':
        partner_template.update({'ref': row[6]})
    if row[8] is not None and row[8]<>'':
        partner_template.update({'phone': row[8]})
    if row[9] is not None and row[9]<>'':
        partner_template.update({'mobile': row[9]})

    print partner_template

    partner_id = server.execute_kw(DATABASE, user_id, PASSWORD, 'res.partner', 'create', [partner_template])

    #create External ID

    external_ids = {
       'model': 'res.partner',
       'name': row[11],
       'res_id': partner_id,
    }
    external_id = server.execute_kw(DATABASE, user_id, PASSWORD, 'ir.model.data', 'create', [external_ids])

    # update related fields

    if row[7] is not None and row[7]<>'':
        #look up and update payment term

        payment_term_id = server.execute_kw(DATABASE, user_id, PASSWORD, 'account.payment.term', 'search_read', [[['name','=',row[7]],['active', '=', True]]],{'fields': ['id'], 'limit': 1})
        if payment_term_id is not None:
            id = server.execute_kw(DATABASE, user_id, PASSWORD, 'res.partner', 'write', [[partner_id],{'property_payment_term': search(payment_term_id,'id')}])

    if row[10] is not None and row[10]<>'':
        #look up and update pricelist

        pricelist_id = server.execute_kw(DATABASE, user_id, PASSWORD, 'product.pricelist', 'search_read', [[['name','=',row[10]],['active', '=', True]]],{'fields': ['id'], 'limit': 1})

        if pricelist_id is not None:
            id = server.execute_kw(DATABASE, user_id, PASSWORD, 'res.partner', 'write', [[partner_id],{'property_product_pricelist': search(pricelist_id,'id')}])



回答3:


If you are creating your application from stretch and only required Android API for Odoo, here is open-source API https://github.com/oogbox/odoo-mobile-api (Odoo android api)

To use in android, first add the following dependency to your app level build.gradle

compile 'com.oogbox.api:odoo:1.0.0'

Documentation: https://github.com/oogbox/odoo-mobile-api#getting-started

Thanks



来源:https://stackoverflow.com/questions/46067346/is-there-anyone-integratted-odoo-with-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!