2016-07-12 3 views
0

私は多くを検索しましたが、解決策は見つかりませんでした。私は、クライアントのResttemplateを使って、base64でエンコードされた画像をAndroidサービス経由でWebサービスに投稿しようとしています。AndroidでrestTemplateからBase64で画像をJSONに投稿するときにエラーが発生する

RestTemplate restTemplate = new RestTemplate(); 
    restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter()); 
    restTemplate.getMessageConverters().add(new StringHttpMessageConverter()); 
    ResponseEntity responseEntity; 
    try { 
     HttpHeaders header = createHeadersAthententicated(accessToken); 
     header.setContentType(new MediaType("application", "json")); 
     JsonObject jsonObject = new JsonObject(); 

     jsonObject.addProperty("base64File", ImageUtil.bitmapToString(user.getProfileImageInBitmap())); 
     jsonObject.addProperty("filename", "profileImage".concat("_").concat(user.getEmail())); 
     HttpEntity<JsonObject> requestEntity = new HttpEntity<>(jsonObject, header); 
     responseEntity = restTemplate.exchange(userUrl, HttpMethod.POST, requestEntity, String.class); 
    } catch (HttpClientErrorException e) { 
     Log.e(TAG, e.getMessage(), e); 
     responseEntity = new ResponseEntity(e.getResponseBodyAsString(), HttpStatus.BAD_REQUEST); 
    } catch (RestClientException e1) { 
     Log.e(TAG, e1.getMessage(), e1); 
     responseEntity = new ResponseEntity(e1.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); 
    } 


public static String bitmapToString(Bitmap bitmap) { 
    try { 
     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
     bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); 
     byte[] b = baos.toByteArray(); 
     String temp = Base64.encodeToString(b, Base64.DEFAULT); 
     return temp; 
    } catch (NullPointerException e) { 
     return null; 
    } catch (OutOfMemoryError e) { 
     return null; 
    } 
} 

私が得たエラー:

org.springframework.http.converter.HttpMessageNotWritableException: はJSON書き込めませんでした:JsonObjectを(参照チェーンを通じて: com.google.gson.JsonObject [ "asBigDecimal"]);ネストされた例外は com.fasterxml.jackson.databind.JsonMappingExceptionある:(参照チェーンを介し:com.google.gson.JsonObject [ "asBigDecimal"])JsonObject

答えて

0

Iがそのように、Resttemplateと進展していません私はOkHttp3に変更しました

final okhttp3.MediaType JSON = okhttp3.MediaType.parse("application/json"); 
    OkHttpClient client = new OkHttpClient(); 

    RequestBody body = RequestBody.create(JSON, jsonObject.toString()); 
    Request request = new Request.Builder() 
      .addHeader("Authorization", "Bearer" + accessToken) 
      .url(userUrl) 
      .post(body) 
      .build(); 

完璧に動作します。

Base64に変換するともう1つのことがBase64.NO_WRAPを使用する必要がありますが、これはブレークラインを挿入しません。

関連する問題