Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 30th, 2012  |  syntax: None  |  size: 3.72 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. uploading files to http
  2. private class SessionTask extends AsyncTask<String, Integer, Integer> {
  3.  
  4.     ProgressDialog dialog;
  5.  
  6.     @Override
  7.     protected void onPreExecute() {
  8.         super.onPreExecute();
  9.         WifiManager wifiManager = (WifiManager)getBaseContext().getSystemService(Context.WIFI_SERVICE);
  10.         wifiManager.setWifiEnabled(true);
  11.         Vibrator v2 = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
  12.         v2.vibrate(1500);
  13.         dialog = new ProgressDialog(TestUI.this);
  14.         dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
  15.         dialog.setTitle("UploadFile");
  16.         dialog.setMessage("Uploading file...");
  17.         dialog.setCancelable(false);
  18.         dialog.setProgress(0);
  19.         dialog.show();
  20.     }
  21.  
  22.     @Override
  23.     protected Integer doInBackground(String... params) {
  24.         try {
  25.  
  26.             HttpClient httpclient = new DefaultHttpClient();
  27.             httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
  28.             MultipartEntity multipart = new MultipartEntity();
  29.             File file = new File("/mnt/sdcard/321.rar");  // File with some location (filepath)
  30.             Charset chars = Charset.forName("UTF-8"); // Setting up the encoding
  31.             FileBody fileB = new FileBody(file); // Create a new FileBody with the above mentioned file
  32.             multipart.addPart("data", fileB); // Add the part to my MultipartEntity. "data" is parameter name for the file
  33.  
  34.             StringBody stringB;  // Now lets add some extra information in a StringBody
  35.             try {
  36.                 stringB = new StringBody("I am the caption of the file",chars);  // Adding the content to the StringBody and setting up the encoding
  37.                 multipart.addPart("caption", stringB); // Add the part to my MultipartEntity
  38.             } catch (UnsupportedEncodingException e) {
  39.                 // TODO Auto-generated catch block
  40.                 e.printStackTrace();
  41.             }
  42.  
  43.             HttpPost post = new HttpPost("http://192.168.1.1:9999/folder/0"); // Setting up a HTTP Post method with the target url
  44.             post.setEntity(multipart); // Setting the multipart Entity to the post method
  45.             HttpResponse resp = httpclient.execute(post);  // Using some HttpClient (I'm using DefaultHttpClient) to execute the post method and receive the response
  46.         } catch(MalformedURLException e) {
  47.             Log.e(TestUI.TAG, "E: Malformed URL! " + e.getLocalizedMessage());
  48.  
  49.             return 1;
  50.         } catch(IOException e) {
  51.             Log.e(TestUI.TAG, "E: I/O error! " + e.getLocalizedMessage());
  52.         return 2;
  53.         }
  54.         return 0;
  55.     }
  56.  
  57.     @Override
  58.     protected void onProgressUpdate(Integer... values) {
  59.         super.onProgressUpdate(values);
  60.         dialog.setMax(values[1]);
  61.         dialog.setProgress(values[0]);
  62.  
  63.     }
  64.  
  65.     @Override
  66.     protected void onPostExecute(Integer result) {
  67.         super.onPostExecute(result);
  68.         dialog.dismiss();
  69.         switch (result) {
  70.         case 0:
  71.             Toast.makeText(TestUI.this, "Uploading finished", Toast.LENGTH_LONG).show();
  72.             new DownloadTask().execute(new String[] {TestUI.LINK_DOWN, TestUI.FILE_DOWN});
  73.             break;
  74.         case 1:
  75.             Toast.makeText(TestUI.this, "E: Malformed URL!", Toast.LENGTH_LONG).show();
  76.             break;
  77.         case 2:
  78.             Toast.makeText(TestUI.this, "E: I/O error! Connection was dismissed!!!", Toast.LENGTH_LONG).show();
  79.             try {
  80.                 Thread.sleep(timesleep);
  81.             } catch (InterruptedException e) {
  82.                 // TODO Auto-generated catch block
  83.                 e.printStackTrace();
  84.             }
  85.             new UploadTask().execute();
  86.             break;
  87.         default:
  88.             break;
  89.         }
  90.     }
  91. }