i am upload video file and data using multipartentity .How to show the progressbar and upload the Array values in one key value?
I am using the following code:
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(Call_Server.REPLAY_VIDEO);
MultipartEntity mpEntity = new MultipartEntity(
HttpMultipartMode.STRICT);
mpEntity.addPart("FILE_UPLOAD", new FileBody(videofile));
mpEntity.addPart("message_id", new StringBody(recipient_id));
mpEntity.addPart("recipient_email", new StringBody(recipient_email2));
totalSize = (int) mpEntity.getContentLength();
System.out.println(totalSize + ":::::totalSize");
httppost.setEntity(mpEntity);
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
Use Thread to upload data, and use runOnUiThread to show progress bar. Also you can use asyn task.
Well, you can use Async Task threading function.
Related
I'm trying to publish image from my automation suite to slack channel.
URL to hit: https://slack.com/api/files.upload
Body is form-data type and it has,
file - image file upload
initial_comment - some string
channels - the slack channel to be published.
i tried using MultipartEntity class inside HttpPost
MultipartEntity multiPartEntity = new MultipartEntity();
FileBody fileBody = new FileBody(file);
//Prepare payload
multiPartEntity.addPart("file", fileBody);
multiPartEntity.addPart("file_type", new StringBody("JPG"));
multiPartEntity.addPart("initial_comment", new StringBody("cat shakes"));
multiPartEntity.addPart("channels", new StringBody("bot-e2e-report"));
//Set to request body
postRequest.setEntity(multiPartEntity);
Im getting the success response from http post. but the image is not posted in slack channel.any help!
The problem was with header. Actually, this slack api gives 200 response even for the wrong header. Working code:
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
//Set various attributes
MultipartEntityBuilder entitybuilder = MultipartEntityBuilder.create();
entitybuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
entitybuilder.addBinaryBody("file", file);
entitybuilder.addTextBody("initial_comment", "cat");
entitybuilder.addTextBody("channels","bot-e2e-report");
HttpEntity mutiPartHttpEntity = entitybuilder.build();
RequestBuilder reqbuilder = RequestBuilder.post("https://slack.com/api/files.upload");
reqbuilder.setEntity(mutiPartHttpEntity);
//set Header
reqbuilder.addHeader("Authorization", "Bearer xoxb-16316687382-1220823299362-fdkBklPrY7rc72bQk3WSOSjD");
HttpUriRequest multipartRequest = reqbuilder.build();
// call post
HttpResponse response = httpclient.execute(multipartRequest);
// Parse the response
HttpEntity entity = response.getEntity();
String json = EntityUtils.toString(entity, StandardCharsets.UTF_8);
System.out.println(json);
I am using HttpClient to send a request with JSON and MP3 audio content. Can anybody tell me where the below code goes wrong? I am using HttpClient version 4.5.2.
HttpClient httpClient = HttpClientBuilder.create().setUserAgent("MyAgent").setMaxConnPerRoute(4).build();
HttpPost httpPostRequest = new HttpPost("url");
httpPostRequest.addHeader("content-type","multipart/form-data");
File audioFile = new File("filename.mp3");
FileBody fileBody = new FileBody(audioFile);
StringBody jsonStringBody = new StringBody(gson.toJson(pojoObject).toString(), ContentType.MULTIPART_FORM_DATA);
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
multipartEntityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntityBuilder.setContentType(ContentType.MULTIPART_FORM_DATA);
multipartEntityBuilder.addPart("file", fileBody);
multipartEntityBuilder.addPart("jsonMetaData", jsonStringBody);
HttpEntity entity = multipartEntityBuilder.build();
httpPostRequest.setEntity(entity);
HttpResponse response = httpClient.execute(httpPostRequest);
I am trying to write a program in android that uploads a file to server. I want to use apache httpclient for that.
Here is my code:
String url = "http://192.168.1.103:8080";
File file = new File("/sdcard/alireza/ali.txt");
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
InputStreamEntity reqEntity = new InputStreamEntity(new FileInputStream(file), -1);
reqEntity.setContentType("binary/octet-stream");
reqEntity.setChunked(true); // Send in multiple parts if needed
httppost.setEntity(reqEntity);
Toast.makeText(getApplicationContext(),"executing...",Toast.LENGTH_LONG).show();
HttpResponse response = httpclient.execute(httppost);
Toast.makeText(getApplicationContext(),"done",Toast.LENGTH_LONG).show();
//Do something with response...
}catch (Exception e){
}
The done toast doesn't appear.
I have developed application where I am sending audio file and receiver information to server, Its working but many times uploading takes very long time and cause ConnectionTimeout exception.
Code I am using now:
BasicHttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 120000);
HttpConnectionParams.setSoTimeout(httpParameters, 120000);
HttpClient httpClient = new DefaultHttpClient(httpParameters);
postStr = new Gson().toJson(audioBean);
File file = new File(audioBean.getFilePath());
ContentBody contentBody = new FileBody(file, "audio/wav");
MultipartEntity mpEntity = new MultipartEntity();
mpEntity.addPart("audio", new StringBody(postStr));
mpEntity.addPart("file", contentBody);
httpPost.setEntity(mpEntity);
HttpResponse httpResponse = httpClient.execute(httpPost);
inputStream = httpResponse.getEntity().getContent();
above code runs in thread, please tell me how to improve performance
I use httpclient to post img to website.
But I have tucked it into httpclient.execute(httppost).
In my local environment , I run it perfect. But when i publish project to SinaAppEngine(Java Local IO
restricted)->sae ,execute method doesn't work because code like this:
reqEntity.addPart("image", byteArrayBody);
(Stringbody run normally )
My sample code:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://r.catchoom.com/v1/search");
ByteArrayBody byteArrayBody = new ByteArrayBody(imgByte, "test.jpg");
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("image", byteArrayBody);
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);