如何使用 API 在 ZOHO INVOICE 添加新客户?我使用以下 POST 方法创建新客户..但我不会工作

How to add new Customer in ZOHO INVOICE using API? I use the Following POST methode to create the new Customer..But i wont Work

HttpClient httpClient = new DefaultHttpClient();

try {
    HttpPost request = new HttpPost("https://invoice.zoho.com/api/v3/contacts?authtoken="ur_token"&organization_id="ur_org_id");
    StringEntity params =new StringEntity("JSONString={\"contact_name\":\"company_name\",\"age\":\"20\"} ");
    request.addHeader("content-type", "application/json");
    request.addHeader("Accept","application/json");
    request.setEntity(params);
    HttpResponse response = httpClient.execute(request);
    System.out.println(response);

    // handle response here...
}catch (Exception ex) {
    // handle exception here
} finally {
    httpClient.getConnectionManager().shutdown();
}

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

明确提到您提供的JSON无效。您可以使用 JSONSimple 库来创建一个简单的 JSON 或 GS​​ON,如下文 link 中所述,并试一试。 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 已创建[服务器:ZGS,日期:2016 年 8 月 5 日星期五16:32:17 GMT,内容类型:application/json;字符集= UTF-8,传输编码:分块,连接:保持活动,设置 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, 过期: Thu, 1970 年 1 月 1 日 00:00:00 GMT,设置 Cookie:JSESSIONID=0A8CC1842C061BC3CA8FD333FEBB5823;路径=/;安全的; HttpOnly,X-Rate-Limit-Limit:1000,X-Rate-Limit-Reset:7062,X-Rate-Limit-Remaining:991,Content-Disposition:附件;,允许:OPTIONS,POST,GET , 删除, 设置 Cookie: BuildCookie_632436810=2; Expires=2016 年 8 月 5 日星期五 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,变化:接受编码,缓存控制:无缓存,严格传输安全:max-age=15768000] org.apache.http.client.entity.DecompressingEntity@d6da883}

文件 invoice.json 是 URL (https://www.zoho.com/invoice/api/v3/contacts/#create-a-contact)

中 JSON 请求的复制粘贴

能否确保您提供的JSON字符串格式正确?

有效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}

希望对您有所帮助。

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

萨巴里

希望以下这段代码能满足您的需要。

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);