如何使用API​​在ZOHO INVOICE中添加新客户?我使用以下POST方法来创建新的Customer..But我不会工作

时间:2016-08-05 05:03:36

标签: java zoho

POST

如何使用API​​在ZOHO INVOICE中添加新客户?我使用以下{{1}}方法创建新客户,但它不起作用。

3 个答案:

答案 0 :(得分:1)

您能否确保您提供的JSONString格式正确?

有效的JSON -

{
"contact_name":"Sabari",
"company_name": "Bowman and Co",
"contact_persons": [
        {
          "salutation": "Mr.",
          "first_name": "Will",
          "last_name": "Smith",
          "email": "willsmith@bowmanfurniture.com"
         }
 ]
}

如果它不是格式良好的json格式,您将面临错误 示例 -

{customer_name=sabari,company_name=Bowman and Co}

希望这有帮助。

如果您遇到任何问题,请告诉我。

Sabari

答案 1 :(得分:1)

我希望以下代码可以满足您的需求。

HttpClient httpClient = HttpClientBuilder.create().build();
    String url = "https://invoice.zoho.com/api/v3/contacts?authtoken=AUTHTOKEN&organization_id=ORGID";
    HttpPost request = new HttpPost(url);

    JSONObject json = new JSONObject();
    json.put("contact_name", "Test");
    //You can add other JSONString params here.

    List<NameValuePair> params = new ArrayList<>();
    params.add(new BasicNameValuePair("JSONString", json.toString()));
    //You can add other params like JSONString here.

    request.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
    request.addHeader("Accept", "application/json");
    HttpResponse response = httpClient.execute(request);

    ResponseHandler<String> handler = new BasicResponseHandler();

    System.out.println(response.getStatusLine().getStatusCode());
    String body = handler.handleResponse(response);

    System.out.println(body);

答案 2 :(得分:0)

显然提到您提供的JSON无效。您可以使用JSONSimple库来创建一个简单的JSON或GSON,如下面的链接所示,并尝试一下。 https://www.zoho.com/invoice/api/v3/contacts/#create-a-contact

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.net.URLEncoder;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

public class ZOHOTestClient {

    public static void main(String[] args) throws Exception {
        String url = "https://invoice.zoho.com/api/v3/contacts?authtoken=xxxxxxxxxxx6&organization_id=xxxxxxxx";

        HttpClient client = HttpClientBuilder.create().build();

        JSONParser parser = new JSONParser();
//being lazy pick the JSON from a file :)
        Object obj = parser.parse(new FileReader("C:\\DevelopmentTools\\3.CODE\\invoice.json"));
/*
//You can create the JSON like this

        JSONObject jsonObject = (JSONObject) obj;

        JSONObject zohoInvoiceRequest = new JSONObject();

        zohoInvoiceRequest.put("contact_name", "Bowman and Co");
        zohoInvoiceRequest.put("company_name", "Bowman and Co");
        zohoInvoiceRequest.put("payment_terms", new Integer(15));
        zohoInvoiceRequest.put("payment_terms", "Net 15");

        JSONObject billing_address = new JSONObject();
        billing_address.put("address", "4900 Hopyard Rd, Suite 310");
        billing_address.put("city", "Pleasanton");
        billing_address.put("state", "94588");
        billing_address.put("country", "USA");
        zohoInvoiceRequest.put("billing_address", billing_address);

        System.out.println(jsonObject.toJSONString());
*/
        url += "&JSONString=";
        url += URLEncoder.encode(jsonObject.toJSONString(),"UTF-8");

        System.out.println(url);
        HttpPost post = new HttpPost(url);

        post.addHeader("Accept", "application/json");
        post.addHeader("charset", "UTF-8");
        post.addHeader("Content-Type", "application/json");
        // post.setEntity(new StringEn);
        // post.setEntity(new StringEntity("&JSONString=" +
        // jsonObject.toJSONString()));

        HttpResponse response = client.execute(post);
        System.out.println(response.getStatusLine());
        System.out.println("Response Code : " + response);

        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

        StringBuffer result = new StringBuffer();
        String line = "";
        while ((line = rd.readLine()) != null) {
            result.append(line);
        }

    }
}

响应代码:HttpResponseProxy {HTTP / 1.1 201 Created [Server:ZGS,Date:Fri,05 Aug 2016 16:32:17 GMT,Content-Type:application / json; charset = UTF-8,Transfer-Encoding: chunked,连接:keep-alive,Set-Cookie:edfb7d8656 = ce40e27c37c608027f2a04de26189092;路径= /;安全; HttpOnly,X-XSS-Protection:1,X-Content-Type-Options:nosniff,Set-Cookie:zbcscook = 1c083c08-9c4e-4210-9a70-b248751cf5a2;路径= /; Secure,Pragma:no-cache,Cache-Control:no-store,no-cache,must-revalidate,max-age = 0,post-check = 0,pre-check = 0,Expires:Thu,1970年1月1日00 :00:00 GMT,Set-Cookie:JSESSIONID = 0A8CC1842C061BC3CA8FD333FEBB5823;路径= /;安全; HttpOnly,X-Rate-Limit-Limit:1000,X-Rate-Limit-Reset:7062,X-Rate-Limit-Remaining:991,Content-Disposition:attachment;,Allow:OPTIONS,POST,GET,DELETE,Set -Cookie:BuildCookie_632436810 = 2; Expires = Fri,05-Aug-2016 16:37:17 GMT;安全,BUILD_VERSION:Jul_28_2016_2_5435,CLIENT_BUILD_VERSION:Jul_28_2016_2_5435,SERVER_BUILD_VERSION:Aug_02_2016_5_4512 /,位置:/ api / v3 / contacts / xxxxxxxxxxxxxx001,Vary:Accept-Encoding,Cache-Control:no-cache,严格传输 - 安全:max-age = 15768000] org.apache.http.client.entity.DecompressingEntity@d6da883}

文件invoice.json是URL中的JSON请求的复制粘贴(https://www.zoho.com/invoice/api/v3/contacts/#create-a-contact