1

- 大家好

我对 sugarcrm 有一种奇怪的行为。这是我用来使用 REST 设置新条目的代码:

  public SugarApi(String sugarUrl){
    REST_ENDPOINT = sugarUrl + "/service/v4/rest.php";
    json = new GsonBuilder().create();
    codec = new URLCodec();
  }

    SetEntryRequest req = new SetEntryRequest(sessionId, nameValueListSetEntry, myKind.getModuleName());
    String response = null;
    try {
      response = postToSugar(REST_ENDPOINT+"?method=set_entry&response_type=JSON&input_type=JSON&rest_data="+codec.encode(json.toJson(req)));
    } catch (RemoteException e) {
        System.out.println("Set entry failed. Message: " + e.getMessage());
        e.printStackTrace();
    } catch (EncoderException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }

其中 postToSugar 是:

public String postToSugar(String urlStr) throws Exception {

URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setAllowUserInteraction(false);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

if (conn.getResponseCode() != 200) {
throw new IOException(conn.getResponseMessage());
}

// Buffer the result into a string
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = rd.readLine()) != null) {
  sb.append(line);
}
rd.close();

conn.disconnect();
if(System.getenv("sugardebug") != null){
  System.out.println(sb.toString());
}
return sb.toString();

}

因此,当帖子较小时,此代码可以正常工作。最大尺寸如下:

{"id":"8c8801c5-ce3b-093c-ee77-514985c19fe1","entry_list":{"account_id":{"name":"account_id","value":"9b37913b-994b-9bc9-4fbf-500e771d845b"},"status":{"name":"status","value":"New"},
"description":{"name":"description","value":"Ceci est un test \/ TICKET A SUPPRIMER"},"priority":{"name":"priority","value":"P1"},
"name":{"name":"name","value":"test longueur post --------------"},"caseorigin_c":{"name":"caseorigin_c","value":"OnLineForm"},"case_chechindate_c":{"name":"case_chechindate_c","value":"2013-01-12"},"type":{"name":"type","value":"ErrorOnCancel"}}}

但如果帖子较长,服务器会返回:

{"name":"Invalid Session ID","number":11,"description":"The session ID is invalid"}

任何帮助,将不胜感激

4

1 回答 1

0

我在描述字段有一个换行符的请求中遇到了这个问题。我什至对换行符进行了urlencoded,它仍然给了我错误。据推测,Sugar 服务器上的 apache 在换行符处中断了请求,这意味着未找到 sessionID。

我的解决方案是将所有出现的%0A%0D替换为%5Cn.

%0A并且%0D是不同的换行符并%5Cn成为\n糖中的换行符。

我没有针对其余 API 的大量无效字符列表。

于 2013-12-20T20:58:32.793 回答