-
Notifications
You must be signed in to change notification settings - Fork 4.1k
MultiPartPost #154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Add a File object to the RequestParams to upload: File myFile = new File("/path/to/file.png"); |
ee呃呃刚才 |
This works fine for small files. However, because the RequestParams class creates a non-buffered ByteArrayEntity to pass to the POST or PUT call, it is very easy to cause OutOfMemory errors with larger files, like when uploading a video or mp3 to a server, which are often larger than 4 or 5 MB. A better solution is to use the MultipartEntity in the apache mime library and pass that to the POST or PUT call. That entity is buffered and I have used it often to upload >> 100MB files. |
@bkhall I try to upload a large image and failed. At the end i ended up here. I try to use method you mentioned above with apache mime, but how do I pass MultipartEntity in the POST call? Do you have any example? So far this is what i've got : Thanks in advance. |
@bangandi try something similar to this:
|
@2fours cmiiw, your code doesn't use loopj at all? |
There are a number of ways to use the MultipartEntity for uploading a file. By far, the most common is something like this. File file = new File([path to your file]);
MimeTypeMap map = MimeTypeMap.getSingleton();
String ext = FilenameUtils.getExtension(file.getName());
String mime_type = map.getMimeTypeFromExtension(ext);
String userAgent = System.getProperty("http.agent", "Android device");
AndroidHttpClient httpClient = AndroidHttpClient.newInstance(userAgent);
HttpPost httpPost = new HttpPost([your url here]);
MultipartEntity form = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, this);
form.addPart("file-data", new FileBody(file, mime_type, "UTF-8"));
httpPost.setEntity(form);
HttpResponse httpResponse = httpClient.execute(httpPost);
httpClient.close(); That's a basic example. You should wrap it with appropriate try-catch blocks, etc. If you want to use the MultipartEntity with the android-async-http project just pass the object to an AsyncHttpClient like this: File file = new File([path to your file]);
MimeTypeMap map = MimeTypeMap.getSingleton();
String ext = FilenameUtils.getExtension(file.getName());
String mime_type = map.getMimeTypeFromExtension(ext);
MultipartEntity form = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, this);
form.addPart("file-data", new FileBody(file, mime_type, "UTF-8"));
AsyncHttpClient client = new AsyncHttpClient();
client.post(context, [your url], form, mime_type, responseHandler) ; Again, just a basic example. Follow the android-async-http project documentation and recommendations for creating the client and response handlers. |
Not in that code snippet but you should be able to call one of the post overloads in loopj client that accepts the entity. |
@bkhall this is what i do so far. Its not getting any response, it's starting, but onFinished & onFailure & onSuccess not getting any call. when i try to debug the mime, it give the correct result (image/jpeg). Am i missing something?
|
Sorry, |
@Kozlov-V There are several code snippets in here. To whose are you referring? If to mine, then something else is wrong, because I use this pattern in many published apps with the async library. No one is intentionally misleading...but the code snippets in here are also not exhaustive. What kind of error are you getting? Is the server configured to allow uploads larger than 1MB? There are at least 3 server side settings that can affect upload size limits. |
@bkhall Sorry! i non test you code. Tomorrow i will do it, but I think I understand what my mistake (: and tell the details |
我是这样解决的,上传大文件的问题。改写了RequestParams.java这个类
|
@bkhall Respected Baron!
and my http server close socket by timeout, when i post file over 1MB (my http server have keepalive timeout 5000) I need to to understand and the test it |
@2fours The last line of my example does exactly that. @wuyexiong Your sample works also, but requires extending the class and overriding the method. @Kozlov-V Server side settings are outside the scope of this thread. I may be able to help you though. Just send me a private message. |
@bkhall thank you In order to upload large files, so to do so |
Thank you! |
Ok, so this thread obviously has solution. Use proper timeout if needed. |
# By mareksebera (7) and Ankush Sachdeva (1) # Via Ankush Sachdeva (1) and Marek Sebera (1) * 'master' of https://github.com/loopj/android-async-http: Added Proxy Authentication ; fixes android-async-http#328 Fixed wrong static field, Closing android-async-http#229 Manual merge, Closing android-async-http#269, android-async-http#118, Closing android-async-http#10, Closing android-async-http#127, android-async-http#154 Closes android-async-http#137 Removing duplicate call Closing android-async-http#179 Updated Gradle Android tools version Cleaning up error message output
Can you give me an example on how to perform MultipartPost using this library? iam trying to post a photo + text, and dont know how to make the requestparams(what/how should i use inputstream)
The text was updated successfully, but these errors were encountered: