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

説明フィールドに改行文字が含まれているリクエストで、この問題が発生しました。改行文字を urlencode してもエラーが発生しました。おそらく、Sugar のサーバーの apache が、セッション ID が見つからないことを意味する改行でリクエストを中断していたのでしょう。

%0A私の解決策は、すべてのand%0Dをに置き換えることでした%5Cn

%0A%0Dは異なる改行文字であり、どれが砂糖の改行であるかに%5Cnなります。\n

残りの API の無効な文字の広範なリストはありません。

于 2013-12-20T20:58:32.793 に答える