I am trying to post around 10 images via volley by converting it to base64 but Volley post the images multiple time due to its retry policy. I am already compressing the files and i also tried changing its timeout but problem still exist. Retrofit also doing same.
Why do you need/use base64?
Are you using get rather than post?
Use multipart file upload for large files, for instance (with retrofit):
public interface FileUploadService {
#Multipart
#POST("/upload")
void upload(#Part("myfile") TypedFile file,
#Part("description") String description,
Callback<String> cb);
}
You can use Android Asynchronous Http Client for multipart request.
Refer this link
HttpClient support Base64.
Related
I'm sending an http request to a website using Volley (POST and StringRequest). The call is correctly executed. However, I can see that the result is a string codified. When seeing the headers of the answer I can see it is encoded in br, which means brotli. How can I decode the answer to later read it as a JSON?
Should I change to OkHttp or another alternative?
I have some http request related problem which I am not able to solve.
I have aws url on which I have to make PUT request with mp4 video file to upload with header "Content-Type=video/mp4"
I wrote the request in postman and successfully uploaded the mo4 video file. Here is my postman screenshot.
I tried implementing it in Android with retrofit and successfully did it as well with 200 response from server. But the problem is, video file is not being uploaded in correct format and getting error in aws console.
Get this error: 4000 3f0fbcf1-1f39-4dc8-9d1e-332966662dd0: Amazon
Elastic Transcoder could not interpret the media file.
Here is my retrofit implementation
#PUT
#Multipart
fun postVideo(#Header("Content-Type") contentType: String
, #Url url: String
, #Part video: MultipartBody.Part): Call<ResponseBody>
And posting body as following
val requestBody = RequestBody.create(MediaType.parse("video/mp4"),videoFile)
val body = MultipartBody.Builder().addPart(requestBody).build()
As far as I know, I think my problem lies in the way I am attaching body to my request and header and url I am giving properly (as I am getting 200ok from server)
My lack of HTTP knowledge is giving me nightmares, any help is appreciated.
as MultipartEntity is removed from sdk 23 what is best way to upload images with other string params to server .
i did refer How to upload file using Volley library in android? but here only file are present
how i can upload image using volley
Use this below link. You can know more about volley
http://www.androidhive.info/2014/05/android-working-with-volley-library-1/
Retrofit is another rest client to know more about retrofit see the below link
http://square.github.io/retrofit/
I want to use Retrofit for Uploading Image to server with some Strings. As in Rails the tags are like
user[username],user[email],user[password]
So after searching a lot i found some where on Github issues of Retrofit to use
Call<UploadImageResponse> uploadImage(#Query("user[name]") String username,#Query("user[password]") String password,#Query("user[phoneno]") String phoneno,#Query("user[email]") String email,#Query("user[avatar]\"; filename=\"profilepic.png\" ") RequestBody file);
The #Query worked for me. But for image upload using #Query did't worked. I also Tried #Part but not got any success. Can anybody please Help me. I m a Fresher and using this techniques to save time and all the Boiler plate code.
I also Tried the replace in #Query and using #Multipart and #Part with String upload but it give Internal Server Error. Error 500.
when the Keys were simple like email and password in login then i use Map and pass them in Body
#POST("login.json")
Call<Model> model(#Body Map<String,String> user);
I hardcodedly pass the Keys and Value in Map and it also worked.
So can any Body please tell me how to Upload Image using Retrofit to server with Key look like
user[avatar]
in Body.
I can upload image using Postman to server so Server is working Properly.
Hy guys!
I am working on an android project(java) with another guy working on the server-side(php). In my application I need to call POST and GET methods in order to upload files to server, download files, send Strings, byte[] array etc.
My question is: What is the best library to use in my case?(I think my files will not exceed 3mb)
I am new in android so I tried so far:
1.Android Asynchronous Http Client(com.loopj.android:android-async-http:x.x.x)
-we gave up to this because it is not from a "trusted" source
2.AsyncTask+HttpClient+HttpPost
-we gave up to this too
3.Volley library
-best so far(for strings, image request), but it needs additional libraries to send images to server(org.apache.httpcomponents:httpmime:4.5)
-I followed so examples from here but I got exceptions, error, libraries error(duplicates) and never managed to solve one without other showing up.
-so I gave up on this too
My question posted for volley library here
4. Now I am thinking about using Retrofit, but dont know it fits my needs:
-send strings and all types of primitive data
-send image/images to server(together with an Api key)
-download image/images from server
Tell me if I am wrong somewhere or if I missed something working with the libraries specified above. I managed to send simple data with all of these, but I didnt managed to send Files(excepting loopj library).
Do you think should I go back to Volley, or starting reading about Retrofit? Volley seems to be the most flexible one, but not for uploading files.
Any reference or advice is welcome! Thanks in advance!
Update:
I found a possible solution for my problem:
-I convert my file/image to a byte array and encode it to a base64 string
-I send the string to server as basic StringRequest with HashMap<String,String>(Using Volley library from Google developers)
-The server decode the string a save the file
I think a very good fit for you would be AndroidAsync.
You can find more about it on their GitHub repository here: https://github.com/koush/AndroidAsync
As an example for you on how to upload files to server:
AsyncHttpPost post = new AsyncHttpPost("http://myservercom/postform.html");
MultipartFormDataBody body = new MultipartFormDataBody();
body.addFilePart("my-file", new File("/path/to/file.txt");
body.addStringPart("foo", "bar");
post.setBody(body);
AsyncHttpClient.getDefaultInstance().execute(post, new StringCallback() {
#Override
public void onCompleted(Exception e, AsyncHttpResponse source, String result) {
if (e != null) {
ex.printStackTrace();
return;
}
System.out.println("Server says: " + result);
}
});
There is also NanoHTTPD which you can find here: https://github.com/NanoHttpd/nanohttpd
I hope this will help you.
You should try HttpURLConnection its really easy to send data to a server.
https://developer.android.com/training/basics/network-ops/connecting.html