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);
Related
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.
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);
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.
This is what I am doing to upload a video to a server:
email
gameId
source
uploadedfile
are the parameters that have to be sent. email, gameId and source are string values,
whereas uploadedfile is the filebody to be uploaded.
Running this code gives response error0.
HttpClient client = new DefaultHttpClient();
String postURL = GlobalConfig.url+"uploadBlogData.php";
HttpPost post = new HttpPost(postURL);
FileBody bin = new FileBody(file);
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("email", new StringBody(GlobalConfig.pref.getString(GlobalConfig.KEY_User_Emailid,""), "text/plain", Charset.forName( "UTF-8")));
reqEntity.addPart("gameId", new StringBody(GlobalConfig.pref.getString(
GlobalConfig.KEY_Game_Created_id,
""), "text/plain", Charset.forName( "UTF-8")));
reqEntity.addPart("source", new StringBody("phone", "text/plain", Charset.forName("UTF-8")));
reqEntity.addPart("uploadfile", bin);
post.setEntity(reqEntity);
HttpResponse response = client.execute(post);
Log.d("resP",GlobalConfig.getStringFromResponse(response));
HttpEntity resEntity = response.getEntity();
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();