创建新报价时无法添加产品详细信息

时间:2014-07-25 11:47:06

标签: java android post crm vtiger

我正在开发一个Android应用程序,以便在vTiger(版本5.4)CRM服务器中创建一个新的引用。

我能够生成新报价,但我在报价详细信息中添加的product_id和数量未添加到其中。除了产品清单,数量和价格之外,其他详细信息将在新报价中显示。

我也研究了vTiger webservices tutorial,但在这种情况下没有用。

我发现accepted answer类似question,但它位于php而不是Android/JAVA

这就是我发送在vTiger服务器中创建新报价所需的详细信息。: -

            try {
                objectJson.put("subject", subject);
                objectJson.put("account_id", accountId);
                objectJson.put("bill_street", address); 
                objectJson.put("assigned_user_id", "19x1");
                objectJson.put("conversion_rate", "1.000"); 
                objectJson.put("currency_id", "21x1");  
                objectJson.put("hdnTaxType", "group");          
                objectJson.put("productid", productId); 
                objectJson.put("quantity", quantity);
            } 
            catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            String data = null;

            try {               
                data = URLEncoder.encode("sessionName", "UTF-8")
                        + "=" + URLEncoder.encode(sessionId, "UTF-8");
                data += "&" + URLEncoder.encode("element", "UTF-8") + "="
                        + URLEncoder.encode(objectJson.toString(), "ISO-8859-1");
                data += "&" + URLEncoder.encode("elementType", "UTF-8") + "="
                        + URLEncoder.encode(moduleName, "UTF-8");  //moduleName='Quotes'
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 

             String text = "";
             BufferedReader reader=null;

             // Send data
                try
                {                 
                    // Defined URL  where to send data
                    URL url = new URL("http://vtiger_url/webservice.php?operation=create");

                    // Send POST data request
                    URLConnection conn = url.openConnection();
                    conn.setDoOutput(true);
                    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
                    wr.write( data );
                    wr.flush();   
                 } 
                    catch(Exception ex)
                        {

                        }

以上代码可帮助我生成没有产品详情的报价。

在研究了上述php answer之后,我将我在代码中使用的网址更改为: - http://vtiger_url/webservice.php?total=23000&operation=create 。这有助于我将总金额添加到新创建的报价中,但我没有成功使用此方法添加其余详细信息。

4 个答案:

答案 0 :(得分:1)

你发现的answer似乎是合适的,即使它是一个php解决方案。你要解决的问题是:

  

我也试过这个,但不幸的是我无法访问Inventoryproductrel表。

可能已经表明缺少正确的身份验证或缺少权限。所以我的建议是

  1. 找出您无法访问表Inventoryproductrel的原因。尝试使用您的身份验证权限(sessionID)访问该表,检查任何提示的响应并尝试解决table isn't accessible问题。
  2. 最后请遵循此answer的建议。

  3. 只是另一个提示。如果您可以通过Web浏览器成功访问该表,那么我会嗅探请求并查看http参数及其值。根据这些调查结果,您可以修改您的请求。 一定要考虑,网络浏览器只能和你的Android应用程序一样。

答案 1 :(得分:1)

这些行已添加到现有代码中: -

JSONArray pdoInformation = new JSONArray();

try{

// Added these lines in try block to send product details

  for(int i=0; i<productIds.size(); i++) {
     JSONObject obj = new JSONObject();
     obj.put("productid", productIds.get(i) );
     obj.put("qty", quantities.get(i));
     obj.put("listprice", listprices.get(i));
     pdoInformation.put(obj);
       }                
            objectJson.put("pdoInformation", pdoInformation);     
}

此处的产品详细信息需要以名称为JSONArray的{​​{1}}发送。

"pdoInformation"循环用于发送多个产品详细信息。

此处forproductIdsquantities是存储为listprices的三个强制性产品详细信息。

答案 2 :(得分:0)

为什么不使用网络服务来创建产品?应根据文档支持这一点。 创建产品并获取其ID后,您可以创建包含这些ID的报价对象。对于其余的API,对象及其字段没有很好地记录,因此您可以使用查询/描述API来尽可能多地获取有关为创建不同对象而需要提供哪些数据的信息。 根据{{​​3}}的说明,您需要包含item_details,其中包含产品和数量信息。确切的字段名称和格式可以通过描述API获得,如Web服务文档中所述

获取vTiger对象的描述

String modeleName = "Quotes"; //Use Products if you want a description of the Products module
String data = null;

try {               
    data = URLEncoder.encode("sessionName", "UTF-8")
                + "=" + URLEncoder.encode(sessionId, "UTF-8");
    data += "&" + URLEncoder.encode("elementType", "UTF-8") + "="
                + URLEncoder.encode(moduleName, "UTF-8");
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
} 

String text = "";
BufferedReader reader=null;
System.out.println(data);
// Send data
try
{                 
    // Defined URL  where to send data
    URL url = new URL("http://vtiger_url/webservice.php?operation=describeobject");

    // Send GET data request
    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);
    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
    wr.write( data );
    wr.flush();   
} catch(Exception ex) {
}

请注意,它与您正在做的事情几乎相同,只有网址和广告素材相同。参数不同,请求类型是GET而不是POST 要创建产品,您将按照与报价相同的步骤进行操作,只有网址和参数不同

答案 3 :(得分:0)

@help据我所知,你的问题是如何将JsonObject与数据一起发送到指定的服务器是正确的吗?如果它是正确的,那么我建议您使用Volley库进行联网,这里有许多可能对您有用的示例。http://arnab.ch/blog/2013/08/asynchronous-http-requests-in-android-using-volley/http://www.androidhive.info/2014/05/android-working-with-volley-library-1/

完成它。它提供了执行网络操作的最简单方法,此库也可以取消请求。

示例代码:

final String URL = "SERVER_URL";
// Post params to be sent to the server
HashMap<String, String> params = new HashMap<String, String>();
params.put("token", "AbCdEfGh123456");

JsonObjectRequest req = new JsonObjectRequest(URL, new JSONObject(params),
   new Response.Listener<JSONObject>() {
       @Override
       public void onResponse(JSONObject response) {
           try {
               VolleyLog.v("Response:%n %s", response.toString(4));
           } catch (JSONException e) {
               e.printStackTrace();
           }
       }
   }, new Response.ErrorListener() {
       @Override
       public void onErrorResponse(VolleyError error) {
           VolleyLog.e("Error: ", error.getMessage());
       }
   });

// add the request object to the queue to be executed
ApplicationController.getInstance().addToRequestQueue(req);

使用此代码,您可以将数据发送到服务器,在服务器端,您可以使用php代码接收此jsondata并解析它。

感谢。