In android, how do I send a file(data) from a mobile device to server using http.
To use the File Transfer Using HTTP feature, you may need to specify a username and password for the HTTP connections for those servers that require a username and password to connect. Commands are also available to specify custom connection characteristics, although default settings can be used.
GitHub - piotrpolak/android-http-server: A complete zero-dependency implementation of a web server and a servlet container in Java with a sample Android application. Product.
To upload you file along with parameters. NOTE : put this code below in non-ui-thread to get response. String charset = "UTF-8"; String requestURL = "YOUR_URL"; MultipartUtility multipart = new MultipartUtility(requestURL, charset); multipart. addFormField("param_name_1", "param_value"); multipart.
Easy, you can use a Post request and submit your file as binary (byte array).
String url = "http://yourserver"; File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "yourfile"); 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); HttpResponse response = httpclient.execute(httppost); //Do something with response... } catch (Exception e) { // show error }
This can be done with a HTTP Post request to the server:
HttpClient http = AndroidHttpClient.newInstance("MyApp"); HttpPost method = new HttpPost("http://url-to-server"); method.setEntity(new FileEntity(new File("path-to-file"), "application/octet-stream")); HttpResponse response = http.execute(method);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With