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
Related
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 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);
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.
I'am trying to convert many doc files via an online website, using HttpClient to emulate an Http request
URI uri = new URI("http://www.docx2doc.com/convert/convert-file");
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(uri);
MultipartEntity reqEntity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("email",new StringBody(""));
reqEntity.addPart("input_type",new StringBody("doc"));
reqEntity.addPart("output_type",new StringBody("html"));
reqEntity.addPart("script",new StringBody("converters"));
FileBody bin = new FileBody(
new File("mydoc.docx"));
reqEntity.addPart("input_file", bin );
httppost.setEntity(reqEntity);`
all the parameters (email,output_type...) in the post are accepted by the server but not the file ...
how can I handle this ?
Thank You.