Search in sources :

Example 1 with FlickrUploadClient

use of io.jawg.osmcontributor.flickr.rest.FlickrUploadClient in project osm-contributor by jawg.

the class PhotoActivity method uploadPhoto.

private void uploadPhoto() {
    OAuthRequest oAuthRequest = flickrOAuth.getOAuthRequest();
    if (oAuthRequest == null) {
        oAuthRequest = new OAuthRequest(application.getFlickr().getApiKey(), application.getFlickr().getSharedSecret());
        oAuthRequest.setOAuthToken(configManager.getFlickrToken());
        oAuthRequest.setOAuthTokenSecret(configManager.getFlickrTokenSecret());
        flickrOAuth.setOAuthRequest(oAuthRequest);
    }
    oAuthRequest.setRequestUrl(FLICKR_API_UPLOAD);
    oAuthRequest.initParam(OAuthParams.getOAuthParams().put(OAuthParams.OAUTH_TOKEN, oAuthRequest.getOAuthToken()).toMap());
    oAuthRequest.signRequest(Verb.POST);
    progressDialog.show();
    if (flickrUploadClient == null) {
        flickrUploadClient = FlickrUploadUtils.getRestAdapter(oAuthRequest.getParams()).create(FlickrUploadClient.class);
    }
    RequestBody requestFile = RequestBody.create(MediaType.parse(getContentResolver().getType(Uri.fromFile(photoFile))), photoFile);
    flickrUploadClient.upload(MultipartBody.Part.createFormData("photo", photoFile.getName(), requestFile)).enqueue(new Callback<String>() {

        @Override
        public void onResponse(Call<String> call, Response<String> response) {
            if (response.isSuccessful()) {
                onUploadFinishedEvent(ResponseConverter.convertImageId(response.body()));
            }
        }

        @Override
        public void onFailure(Call<String> call, Throwable t) {
            if (nbTry < MAX_RETRY_UPLOAD) {
                nbTry++;
                uploadPhoto();
            } else {
                nbTry = 0;
                Toast.makeText(PhotoActivity.this, R.string.flickr_communication_failure, Toast.LENGTH_LONG).show();
                progressDialog.dismiss();
            }
        }
    });
}
Also used : OAuthRequest(io.jawg.osmcontributor.flickr.oauth.OAuthRequest) FlickrUploadClient(io.jawg.osmcontributor.flickr.rest.FlickrUploadClient) RequestBody(okhttp3.RequestBody)

Aggregations

OAuthRequest (io.jawg.osmcontributor.flickr.oauth.OAuthRequest)1 FlickrUploadClient (io.jawg.osmcontributor.flickr.rest.FlickrUploadClient)1 RequestBody (okhttp3.RequestBody)1