예제 #1
0
  /**
   * Uploads the image to the server
   *
   * @param b
   * @param listener
   */
  public void uploadImage(final Bitmap b, final OnResponseListener listener) {
    OkHttpClient httpClient = new OkHttpClient();
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    // encode image as PNG, re-encoding as JPG may cause compression artefacts to be visible
    b.compress(Bitmap.CompressFormat.PNG, 0, bos);

    RequestBody requestBody =
        new MultipartBuilder()
            .type(MultipartBuilder.FORM)
            .addPart(
                // the filename parameter is needed to specify that this is indeed a file and not an
                // inline
                // attachment
                Headers.of(
                    "Content-Disposition", "form-data; name=\"image\"; filename=\"image.png\""),
                RequestBody.create(MediaType.parse("image/png"), bos.toByteArray()))
            .build();
    Request request = new Request.Builder().url(UPLOAD_ENDPOINT).post(requestBody).build();

    Call call = httpClient.newCall(request);
    call.enqueue(
        new Callback() {
          @Override
          public void onFailure(Request request, IOException e) {}

          @Override
          public void onResponse(Response response) throws IOException {
            listener.onResponse(new ServerResponse(response.code(), response.body().string()));
          }
        });
  }
예제 #2
0
  @Override
  public Request withHeaders(Map<String, String> additionalHeaders) {
    LinkedHashMap<String, String> newHeaders = new LinkedHashMap<>(headers().asMap());

    newHeaders.putAll(additionalHeaders);

    return create(
        method(), uri(), parameters(), Headers.of(newHeaders), service(), payload(), ttl());
  }