I am new in Android. I want to upload a file through wifi and gprs but problem is how can I send multipart data in Android.
Asked
Active
Viewed 947 times
2 Answers
1
// Enable HTTP parameters
HttpParams sslparams = new BasicHttpParams();
HttpProtocolParams.setVersion(sslparams, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(sslparams, HTTP.UTF_8);
// Register the HTTP and HTTPS Protocols. For HTTPS, register our custom SSL Factory object.
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
registry.register(new Scheme("https",sslFactory, 443));
// Create a new connection manager using the newly created registry and then create a new HTTP client
// using this connection manager
ClientConnectionManager ccm = new ThreadSafeClientConnManager(sslparams, registry);
httpclient = new DefaultHttpClient(ccm,sslparams);
HttpPost httppost = new HttpPost(url);
/*httppost.setEntity(new UrlEncodedFormEntity(nameValue));
httppost.setEntity(new InputStreamEntity(new FileInputStream(file), file.length()));
httppost.setHeader("Content-Type", "application/x-www-form-urlencoded");*/
MultipartEntity entity = new MultipartEntity();
//entity.addPart("fileName",);
entity.addPart("file", new InputStreamBody(new FileInputStream(file), ContentType.APPLICATION_OCTET_STREAM));
entity.addPart("UserID", new StringBody(parameter.getUsreId()));
entity.addPart("UserName",new StringBody(parameter.getUserName()));
entity.addPart("HostName", new StringBody(parameter.getHostName()));
entity.addPart("ProtectionType", new StringBody(parameter.getProtectionType()));
entity.addPart("FolderPath", new StringBody(parameter.getFolderPath()));
entity.addPart("osid", new StringBody(parameter.getOsId()));
//entity.addPart("file", new FileBody(file,ContentType.MULTIPART_FORM_DATA));
httppost.setEntity(entity);
//httppost.setEntity(new StringEntity(file));
//httppost.setHeader("Content-Type", "text/xml");
HttpParams httpparams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpparams, 15000);
HttpConnectionParams.setSoTimeout(httpparams, 30000);
httppost.setParams(httpparams);
HttpResponse response = httpclient.execute(httppost);

Vikrant
- 1,102
- 8
- 5
0
Create HttpConnection and pass stream on server . follow this link

Shailendra Singh Rajawat
- 8,172
- 3
- 35
- 40