android httpclient - android

How to transfer a 50mb file to web server using httppost.
When trying to transfer the file it shows out of memory error.
can we use chuncked encoding? how? give some code snippets.
Thank you.
Edit:
Here's the code:
InputStreamEntity reqEntity = new InputStreamEntity(new FileInputStream(f), -1);
reqEntity.setContentType("binary/octect-stream");
reqEntity.setChunked(true);
httppost.setEntity(reqEntity);

Use InputStreamEntity as it does not load the whole file in memory. Do something like this:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://localhost/upload");
File file = new File("/path/to/myfile");
FileInputStream fileInputStream = new FileInputStream(file);
InputStreamEntity reqEntity = new InputStreamEntity(fileInputStream, file.length());
httppost.setEntity(reqEntity);
reqEntity.setContentType("binary/octet-stream");
HttpResponse response = httpclient.execute(httppost);
HttpEntity responseEntity = response.getEntity();
if (responseEntity != null) {
responseEntity.consumeContent();
}
httpclient.getConnectionManager().shutdown();

Related

How to replace MultipartEntity with MultipartEntityBuilder on Android?

HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpPost httppost = new HttpPost("http://192.168.150.101:8080/TDIDP/ServletImagen");
File file = new File("C:\\pw\\proyectos\\TDIDP\\a.png");
MultipartEntity mpEntity = new MultipartEntity();
ContentBody cbFile = new FileBody(file, "image/png");
mpEntity.addPart("userfile", cbFile);
httppost.setEntity(mpEntity);
System.out.println("executing request " + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
System.out.println(response.getStatusLine());
if (resEntity != null) {
System.out.println(EntityUtils.toString(resEntity));
}
if (resEntity != null) {
resEntity.consumeContent();
}
httpclient.getConnectionManager().shutdown();
I have this code that sends images to a Servlet, but the problem is that now MultipartEntitiy is not available for Android since it is obsolete, how can I use MultiPartEntityBuilder to do a multipar?
how can I use MultiPartEntityBuilder to do a multipart.
HttpClient is not supported any more in sdk 23, if you use it, you need to work around much. You have to use URLConnection
You can see MultipartUtility in this link https://github.com/RecastAI/SDK-Android/blob/master/src/main/java/ai/recast/sdk_android/MultipartUtility.java.

Cannot set header to multipart/form-data for HttpPost

I'm trying to post some compressed data plus text to a perl program on a web server but cannot change the header from octet-stream to multipart/form-data.
My code is:-
HttpClient httpclient = new DefaultHttpClient();
String url = "http://webaddress/perl.pl";
HttpPost httppost = new HttpPost(url);
httppost.setHeader("Content-Type","multipart/form-data");
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
try {
entity.addPart("mtdata",new ByteArrayBody(data, file.getLastPathSegment()));
entity.addPart("email", new StringBody(strUserName));
entity.addPart("mtfilename", new StringBody(file.getLastPathSegment()));
httppost.setEntity(entity);
HttpResponse httpresponse = httpclient.execute(httppost);
HttpEntity resEntity = httpresponse.getEntity();
response = EntityUtils.toString(resEntity);
}
The raw data being received is :-
Buffer = --FfT4ZNRUCPw6yONkpSsXNkA3WA2l6fvy53
Content-Disposition: form-data; name="mtdata"; filename="filename"
Content-Type: application/octet-stream <<=== cannot change this
... Some binary data ...
What am I doing wrong?
In the end I found that the best way was to save it as a file and then post the file using MultipartEntity with a FileBody explicitly stating the Content-Type.
The code I used was:-
pairs.addPart("File", new FileBody(fout,"multipart/form-data"));
As an aside, I also found that I needed to use ZipOutputStream rather than GZIPOutputStream so that I could add the uncompressed file name.
Code for that was:-
data = bos.toByteArray();
OutputStream fos = new FileOutputStream(extStorageDirectory + newfilename);
ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(fos));
try {
ZipEntry entry = new ZipEntry(file.getLastPathSegment());
zos.putNextEntry(entry);
zos.write(data);
zos.closeEntry();
}
finally {
zos.close();
}
fos.close();
Try insert
httppost.addHeader("Content-Type", "multipart/form-data");
and remove
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

Uploading pictures with additional data to php

I want to upload a photo from the phone with some data like name and email.
From an Android device I know how to upload a photo and I know how to send data between the phone and the server but how do you do both at the same time?
Should I do them separately?
In your case you should use the MultipartEntity class,
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("name", new StringBody(name));
reqEntity.addPart("email", new StringBody(email));
if(imagePath.trim().length() != 0) {
reqEntity.addPart("profilePic", new FileBody(new File(imagePath)));
}
HttpClient hc = new DefaultHttpClient();
HttpPost postMethod = new HttpPost(urlString);
HttpEntity resEntity;
HttpResponse response = null;
postMethod.setEntity(reqEntity);
response = hc.execute(postMethod);
resEntity = response.getEntity();
response_str = EntityUtils.toString(resEntity);

Sending Video using HTTP Post Multipart entity

What I want I want to send a video from my SDcard to a server. I also want to send some parameters/value with it.
I have tried I have tried the following code:
public String SendToServer(String aUrl,File aFile)
{
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(aUrl);
try
{
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("file", new FileBody(aFile));
entity.addPart("video[title]", new StringBody("testVideo"));
entity.addPart("video[type]", new StringBody("1"));
httpPost.setEntity(entity);
HttpContext localContext = new BasicHttpContext();
// Bind custom cookie store to the local context
localContext.setAttribute(ClientContext.COOKIE_STORE, Globals.sessionCookie);
HttpResponse response = httpClient.execute(httpPost, localContext);
HttpEntity resEntity = response.getEntity();
String Response = "";
if (response != null)
{
Response = EntityUtils.toString(resEntity);
}
return Response;
}
catch (IOException e)
{
e.printStackTrace();
}
return "Exception";
}
What is the problem When I run this code, I get stuck at this line
HttpResponse response = httpClient.execute(httpPost, localContext);
I get no exception, no response nothing at all. Can anyone please guide me, what is the problem in here?
The above code in my question was perfect, but I had the network problem. My device was connected to a hotspot(Connectify Software). When I connected to the original network, this code worked perfect.
I recommend you people to never trust a hotspot for this kind of functionality.
try using this way if want to send as content or esle I will upload the project by tonight
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
InputStreamEntity reqEntity = new InputStreamEntity(new FileInputStream(filePath), -1);
reqEntity.setContentType("binary/octet-stream");
reqEntity.setChunked(true); // Send in multiple parts if needed
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);

Set image type in android

I have some code to post an image to my php script that uploads to a database, when it adds to the database the file type is application/oct???? (what is this)
is there anyway of changing this to a jpg file at the android stage?
Below is my code
HttpClient client = new DefaultHttpClient();
String postURL = "http://10.0.2.2:90/mobileupload3.php";
HttpPost post = new HttpPost(postURL);
FileBody bin = new FileBody(file);
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("image", bin);
reqEntity.addPart("name", new StringBody(enteredName));
reqEntity.addPart("gender", new StringBody(radio));
reqEntity.addPart("cat", new StringBody(radio2));
reqEntity.addPart("lat", new StringBody(lat));
reqEntity.addPart("lon", new StringBody(lon));
post.setEntity(reqEntity);
HttpResponse response = client.execute(post);
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
Log.i("RESPONSE",EntityUtils.toString(resEntity));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
application/oct (I assume you mean application/octet-stream) is a MIME type for a general binary file.
Without more information on your method of upload, I believe that the other part of your question has already been answered on SO here.

Categories

Resources