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);
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 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'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.
I am trying to make a simple GET request for a website, but I am getting unknown host exception.
Given below is my code--
DefaultHttpClient client = new DefaultHttpClient();
HttpHost targetHost=null;
targetHost= new HttpHost("google.com/", 80, "http");
HttpGet httpget = new HttpGet("about-us.html");
BasicHttpContext localcontext = new BasicHttpContext();
try {
HttpResponse response = client.execute(targetHost, httpget, localcontext);
It looks like you have a simple problem here.
The URL for your 'HttpHost' object is malformed. You need to drop the '/' from "google.com/".
It should work after that. I used your code with that single modification & it worked.
DefaultHttpClient client = new DefaultHttpClient();
HttpHost targetHost = new HttpHost("google.com", 80, "http");
HttpGet httpget = new HttpGet("about-us.html");
BasicHttpContext localContext = new BasicHttpContext();
HttpResponse response = null;
try { response = client.execute(targetHost, httpget, localContext);
System.out.println(response.getStatusLine()
}
catch(Exception e){
// Enter error-handling code here.
}