2014-11-28 4 views
0

OkHttp를 사용하여 Google에 일괄 요청을 보내려고했지만 응답에서 항상 오류 500 및 알 수없는 오류가 발생했습니다.Google에 일괄 요청을 보내지 않았다.

이것은 내 코드이며 토큰은 GoogleAuthUtil에서 가져옵니다.

MultipartBuilder multipartBuilder = new MultipartBuilder(); 
    MediaType http = MediaType.parse("application/http"); 
    MediaType mixed = MediaType.parse("multipart/mixed"); 
    multipartBuilder.addPart(RequestBody.create(http, "GET https://www.googleapis.com/gmail/v1/users/me/messages")); 
    multipartBuilder.type(mixed); 
    Request request = new Request.Builder() 
      .url("https://www.googleapis.com/batch") 
      .header("Authorization", " Bearer " + token) 
      .post(multipartBuilder.build()) 
      .build(); 

    try{ 
     Response response = client.newCall(request).execute(); 
     String res = response.body().string(); 
    } 
    catch (IOException e){ 
     e.printStackTrace(); 
    } 

답변

1

마지막으로이 문제를 해결했습니다. 나중에 유사한 방법을 첫 번째 [] 바이트 및 통화로 변환되어야하며, \ n을 잊지 마세요

multipartBuilder.addPart(RequestBody.create(http, "GET https://www.googleapis.com/gmail/v1/users/me/messages")); 

: 모든 코드는 한 줄 외에 아무 문제가 없습니다.

byte[] request = (https://www.googleapis.com/gmail/v1/users/me/messages).getBytes(); 
RequestBody partRequest = RequestBody.create(HTTP, request); 

나는 OkHttp 소스 코드에서 문제를 발견했다.

public static RequestBody create(MediaType contentType, String content) { 
    Charset charset = Util.UTF_8; 
    if (contentType != null) { 
    charset = contentType.charset(); 
    if (charset == null) { 
     charset = Util.UTF_8; 
     contentType = MediaType.parse(contentType + "; charset=utf-8"); 
    } 
    } 
    byte[] bytes = content.getBytes(charset); 
    return create(contentType, bytes); 
} 

문제는 Content-Type에 추가되는 charset입니다. 그것은 500 오류를 만듭니다.

관련 문제