- uploading files to http
- private class SessionTask extends AsyncTask<String, Integer, Integer> {
- ProgressDialog dialog;
- @Override
- protected void onPreExecute() {
- super.onPreExecute();
- WifiManager wifiManager = (WifiManager)getBaseContext().getSystemService(Context.WIFI_SERVICE);
- wifiManager.setWifiEnabled(true);
- Vibrator v2 = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
- v2.vibrate(1500);
- dialog = new ProgressDialog(TestUI.this);
- dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
- dialog.setTitle("UploadFile");
- dialog.setMessage("Uploading file...");
- dialog.setCancelable(false);
- dialog.setProgress(0);
- dialog.show();
- }
- @Override
- protected Integer doInBackground(String... params) {
- try {
- HttpClient httpclient = new DefaultHttpClient();
- httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
- MultipartEntity multipart = new MultipartEntity();
- File file = new File("/mnt/sdcard/321.rar"); // File with some location (filepath)
- Charset chars = Charset.forName("UTF-8"); // Setting up the encoding
- FileBody fileB = new FileBody(file); // Create a new FileBody with the above mentioned file
- multipart.addPart("data", fileB); // Add the part to my MultipartEntity. "data" is parameter name for the file
- StringBody stringB; // Now lets add some extra information in a StringBody
- try {
- stringB = new StringBody("I am the caption of the file",chars); // Adding the content to the StringBody and setting up the encoding
- multipart.addPart("caption", stringB); // Add the part to my MultipartEntity
- } catch (UnsupportedEncodingException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- HttpPost post = new HttpPost("http://192.168.1.1:9999/folder/0"); // Setting up a HTTP Post method with the target url
- post.setEntity(multipart); // Setting the multipart Entity to the post method
- HttpResponse resp = httpclient.execute(post); // Using some HttpClient (I'm using DefaultHttpClient) to execute the post method and receive the response
- } catch(MalformedURLException e) {
- Log.e(TestUI.TAG, "E: Malformed URL! " + e.getLocalizedMessage());
- return 1;
- } catch(IOException e) {
- Log.e(TestUI.TAG, "E: I/O error! " + e.getLocalizedMessage());
- return 2;
- }
- return 0;
- }
- @Override
- protected void onProgressUpdate(Integer... values) {
- super.onProgressUpdate(values);
- dialog.setMax(values[1]);
- dialog.setProgress(values[0]);
- }
- @Override
- protected void onPostExecute(Integer result) {
- super.onPostExecute(result);
- dialog.dismiss();
- switch (result) {
- case 0:
- Toast.makeText(TestUI.this, "Uploading finished", Toast.LENGTH_LONG).show();
- new DownloadTask().execute(new String[] {TestUI.LINK_DOWN, TestUI.FILE_DOWN});
- break;
- case 1:
- Toast.makeText(TestUI.this, "E: Malformed URL!", Toast.LENGTH_LONG).show();
- break;
- case 2:
- Toast.makeText(TestUI.this, "E: I/O error! Connection was dismissed!!!", Toast.LENGTH_LONG).show();
- try {
- Thread.sleep(timesleep);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- new UploadTask().execute();
- break;
- default:
- break;
- }
- }
- }