我正在尝试使用calendar 365 rest api创建一个事件。我使用主题上的希腊字母正确构造JSON。我得到201响应,事件被创建但主题显示为????领域。有没有办法回复作为回应希腊字母而不是问号?这是我到目前为止所做的。
@Path("/createevent")
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.TEXT_HTML)
public Response createEvent(@FormParam("startTime") String startTime, @FormParam("endTime") String endTime, @FormParam("title") String title, @FormParam("eventBody") String eventBody, @FormParam("uniqueApiID") String uniqueApiID)
//public Response createEvent(@PathParam("startTime") String startTime, @PathParam("endTime") String endTime, @PathParam("title") String title, @PathParam("eventBody") String eventBody, @PathParam("uniqueApiID") String uniqueApiID)
{
Response resp = null;
String accessToken ="";
Token token =null;
String microsoftResponse = "";
CloseableHttpResponse response=null;
StringEntity stringEntity =null;
try
{
byte titleByte[] = title.getBytes("UTF-8");
String titleUTF= new String(titleByte,"UTF-8");
token = DatabaseUtils.getaccessTokensAPI(uniqueApiID);
accessToken=token.getAccessToken();
if(accessToken=="")
{
microsoftResponse="Your unique id is not correct";
resp = Response.status(404).entity(microsoftResponse).build();
}
else
{
eventBody ="{\n"+
" \"Subject\": \""+titleUTF+"\",\n"+
" \"Body\": {\n"+
" \"ContentType\": \"HTML\",\n"+
" \"Content\": \"I think it will meet our requirements!\"\n"+
" },\n"+
" \"Start\": \""+startTime+"\",\n"+
" \"End\": \""+endTime+"\",\n"+
" \"Attendees\": [\n"+
" {"+
" \"EmailAddress\": {\n"+
" \"Address\": \"my@mail.com\",\n"+
" \"Name\": \"Janet Schorr\"\n"+
" },\n"+
" \"Type\": \"Required\"\n"+
" }\n"+
" ]\n"+
"}";
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("https://outlook.office365.com/api/v1.0/me/events");
httpPost.setHeader("authorization" ,"Bearer "+accessToken);
httpPost.setHeader("charset" ,"UTF-8");
httpPost.addHeader("Accept-Language", "el");
stringEntity = new StringEntity(eventBody,ContentType.create("application/json"));
httpPost.setEntity(stringEntity);
response = httpclient.execute(httpPost);
response.setHeader("charset" ,"UTF-8");
InputStream in=null;
BufferedReader buffer=null;
in= response.getEntity().getContent();
int statusCode = response.getStatusLine().getStatusCode();
buffer = new BufferedReader(new InputStreamReader(in, "UTF-8"));
String s = "";
while ((s = buffer.readLine()) != null) {
microsoftResponse += s;
}
String mystring = new String(microsoftResponse.getBytes("UTF-8"),"UTF-8");
// System.out.println(microsoftResponse);
resp = Response.status(statusCode).entity(mystring).build();
}
}
catch(Exception e)
{
e.printStackTrace();
resp = Response.status(500).entity("error creating event").build();
}
return resp;
}
答案 0 :(得分:0)
我以某种方式将其编辑回来。这是希腊的活动
Categories":[],"DateTimeCreated":"2015-03-26T13:38:37.2175553Z","DateTimeLastModified":"2015-03-26T13:38:37.5769451Z","Subject":"\u0388\u03bd\u03b1 \u03b1\u03c0\u03bb\u03cc \u03b3\u03b5\u03b3\u03bf\u03bd\u03cc\u03c2","BodyPreview":"\u0394\u03b5\u03b4\u03bf\u03bc\u03ad\u03bd\u03b1 \u03c4\u03bf\u03c5 \u03b3\u03b5\u03b3\u03bf\u03bd\u03cc\u03c4\u03bf\u03c2","Body":{"ContentType":"HTML","Content":"\u0394\u03b5\u03b4\u03bf\u03bc\u03ad\u03bd\u03b1 \u03c4\u03bf\u03c5 \u03b3\u03b5\u03b3\u03bf\u03bd\u03cc\u03c4\u03bf\u03c2"},"Importance":"Normal","HasAttachments":false,"Start":"2015-03-26T00:00:00Z","End":"2015-03-28T00:00:00Z","Location":{"DisplayName":"\u03b1\u03af\u03b8\u03bf\u03c5\u03c3\u03b1 \u03c3\u03c5\u03bd\u03b5\u03b4\u03c1\u03b9\u03ac\u03c3\u03b5\u03c9\u03bd"},
答案 1 :(得分:0)
我设法解决了这个问题。问题在于我发送的编码。如果有人发现这有用,则必须将String实体更改为此行:
stringEntity = new StringEntity(eventBody ,ContentType.create("application/json",Consts.UTF_8));