How to add NodeJs, S3, hero directly download in android?

I am trying to understand how to convert, how they provide an example from a website for android / retrofit ...

This is a sample code for a website:

function get_signed_request(file){ var xhr = new XMLHttpRequest(); xhr.open("GET", "/sign_s3?file_name="+file.name+"&file_type="+file.type); xhr.onreadystatechange = function(){ if(xhr.readyState === 4){ if(xhr.status === 200){ var response = JSON.parse(xhr.responseText); upload_file(file, response.signed_request, response.url); } else{ alert("Could not get signed URL."); } } }; xhr.send(); } function upload_file(file, signed_request, url){ var xhr = new XMLHttpRequest(); xhr.open("PUT", signed_request); xhr.setRequestHeader('x-amz-acl', 'public-read'); xhr.onload = function() { if (xhr.status === 200) { document.getElementById("preview").src = url; document.getElementById("avatar_url").value = url; } }; xhr.onerror = function() { alert("Could not upload file."); }; xhr.send(file); } 

This is my attempt at android modification:

 @GET("/sign_s3") public void getSign( @Query("name") String userId, @Query("type") String type, Callback<UserResponse> callback); @Multipart @PUT("/{url}") public void sendMedia( @Path("url") String signRequest, @Part("theNameToUse") String theNameToUse, @Part("isItAPicture") boolean isItAPicture, //if true it is a picture @Part("media") TypedFile media, Callback<UserResponse> callback); 

User Response:

 ApiManager.getAsyncApi().getSign(name, type, new Callback<UserResponse>() { @Override public void success(UserResponse userResponse, Response response) { sendMedia(response.signed_request); } @Override public void failure(RetrofitError error) { throw error; } }); private sendApi(path) { ApiManager.getAsyncApi().sendMedia(path, title, isPictureNotvideo, media, new Callback<UserResponse>() { @Override public void success(UserResponse userResponse, Response response) { } @Override public void failure(RetrofitError error) { throw error; } }); } 

link to heroku document: https://devcenter.heroku.com/articles/s3-upload-node

response.signed_request does not work and is not an option ... if I answer response.getBody (), this is the only thing I see or get headers ... I don’t know how to capture a signed request ...

+1
source share

Source: https://habr.com/ru/post/985396/


All Articles