public void GetMethod(String url) { Task.Run(async() => { LinearLayout linearLayout = FindViewById <LinearLayout>(Resource.Id.linearLayout); HttpClient client = new HttpClient(); String ip = GetString(Resource.String.ip); String contentfolder = "Pliki pobrane"; String path = this.GetExternalFilesDir(null).AbsolutePath; Android.Util.Log.Error("path", path); File file = new File(); var result = await client.GetAsync(url); var json = await result.Content.ReadAsStringAsync(); if (result.IsSuccessStatusCode) { List <File> files = Newtonsoft.Json.JsonConvert.DeserializeObject <List <File> >(json); int i = 0; int b = 0; var layouty = new List <LinearLayout>(); for (var j = 0; j < files.Count; j++) { int k = j; System.Console.WriteLine("to jest j: " + j); System.Console.WriteLine("wtf " + files.Count); LinearLayout linear = (LinearLayout)LayoutInflater.Inflate(Resource.Layout.materialy, linearLayout, false); System.Diagnostics.Debug.WriteLine("jacie " + j); System.Diagnostics.Debug.WriteLine("jaciek " + k); ((TextView)linear.FindViewById(Resource.Id.idt)).Text = "Plik numer " + (j + 1) + " o nazwie " + files[k].filename; layouty.Add(linear); Button downloadb = linear.FindViewById <Button>(Resource.Id.downloadb); Button deleteb = linear.FindViewById <Button>(Resource.Id.deletefb); Button filestatsb = linear.FindViewById <Button>(Resource.Id.filestatsb); if (rank.Equals("1")) { deleteb.Visibility = ViewStates.Invisible; } downloadb.Click += (sender, e) => { System.Console.WriteLine("kliknalem"); View view = (View)sender; Android.App.AlertDialog.Builder builder = new Android.App.AlertDialog.Builder(this); builder.SetTitle("Pobieranie pliku"); builder.SetMessage("Czy na pewno chcesz pobrac?"); builder.SetPositiveButton("Tak", OkAction); builder.SetNegativeButton("Nie", CancelAction); var myCustomDialog = builder.Create(); System.Diagnostics.Debug.WriteLine("w downloadzie " + k); myCustomDialog.Show(); async void OkAction(object sender, DialogClickEventArgs e) { builder.Dispose(); System.Console.WriteLine("ok"); Button myButton = sender as Button; System.Console.WriteLine("tu"); TextView liczbapobrant = linear.FindViewById <TextView>(Resource.Id.liczbapobrant); int liczbapobran = int.Parse(liczbapobrant.Text); for (int i = 1; i <= liczbapobran; i++) { System.Console.WriteLine(liczbapobran.ToString()); } if (!System.IO.Directory.Exists(path + contentfolder)) { System.IO.Directory.CreateDirectory(path + "/" + contentfolder); System.Console.WriteLine("Stworzono folder"); } bar = new ProgressDialog(this); bar.SetProgressStyle(ProgressDialogStyle.Horizontal); bar.Max = (liczbapobran); bar.Progress = 0; bar.Show(); ilepobrac = liczbapobran; numerpobierania = 0; String text = null; String filename = files[k].filename; for (int i = 1; i <= liczbapobran; i++) { using (WebClient wc = new WebClient()) { var watch = System.Diagnostics.Stopwatch.StartNew(); wc.DownloadFile(new Uri(ip + "/file/download/" + files[k].id), path + "/" + contentfolder + "/" + files[k].filename); float czasdownload = 0; long roznicaping = 0; float avgping = 0; watch.Stop(); czasdownload = watch.ElapsedTicks; bar.Progress = bar.Progress + (bar.Max / ilepobrac); numerpobierania++; if (numerpobierania == ilepobrac) { bar.Dismiss(); text = "Plik pobrano " + ilepobrac + " razy i zapisano w folderze " + contentfolder; Toast.MakeText(Application.Context, text, ToastLength.Long).Show(); } URL url = new URL(ip + "/ping/"); float m; for (m = 1; m <= 10; m++) { watch = System.Diagnostics.Stopwatch.StartNew(); HttpURLConnection urlConnection = (HttpURLConnection)url.OpenConnection(); urlConnection.ConnectTimeout = 1000; urlConnection.ReadTimeout = 1000; watch.Stop(); var po = watch.ElapsedTicks; roznicaping += po; urlConnection.Disconnect(); System.Diagnostics.Debug.WriteLine(roznicaping / 10000); } avgping = roznicaping / m / 10000; czasdownload = czasdownload / 10000; System.Diagnostics.Debug.WriteLine("Tyle czasu pobieram: " + czasdownload); string urlstats = ip + "/file/updatestats/"; Pola.File filestat = new Pola.File(); filestat.temp_avg_latency = avgping; filestat.temp_download_time = czasdownload; filestat.temp_platform = GetString(Resource.String.jezyk); filestat.id = files[k].id; filestat.temp_filename = files[k].filename; filestat.temp_username = username; client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json")); var jsonpost = JsonConvert.SerializeObject(filestat); var content = new StringContent(jsonpost, Encoding.UTF8, "application/json"); string coto = await content.ReadAsStringAsync(); System.Diagnostics.Debug.WriteLine("coto: " + coto); var responsepost = await client.PutAsync(urlstats + filestat.id, content); System.Diagnostics.Debug.WriteLine("Statystyka numer " + numerpobierania); } } } void CancelAction(object sender, DialogClickEventArgs e) { builder.Dispose(); } }; deleteb.Click += (sender, e) => { View view = (View)sender; Android.App.AlertDialog.Builder builder = new Android.App.AlertDialog.Builder(this); builder.SetTitle("Usunięcie materiału"); builder.SetMessage("Czy na pewno usunąć ten materiał?"); builder.SetPositiveButton("Tak", OkAction); builder.SetNegativeButton("Nie", CancelAction); var myCustomDialog = builder.Create(); myCustomDialog.Show(); async void OkAction(object sender, DialogClickEventArgs e) { HttpClient client = new HttpClient(); System.Console.WriteLine("takie ip: " + ip + "/file/delete/" + files[k].id); var result = await client.DeleteAsync(ip + "/file/delete/" + files[k].id); var json = await result.Content.ReadAsStringAsync(); if (result.IsSuccessStatusCode) { Toast.MakeText(Application.Context, "Usunięto plik", ToastLength.Long).Show(); Finish(); StartActivity(Intent); } else { Toast.MakeText(Application.Context, "Wystąpił błąd", ToastLength.Long).Show(); } builder.Dispose(); } void CancelAction(object sender, DialogClickEventArgs e) { builder.Dispose(); } }; filestatsb.Click += (sender, e) => { View view = (View)sender; Intent StatystykiPliku = new Intent(this, typeof(StatystykiPliku)); StatystykiPliku.PutExtra("username", username); StatystykiPliku.PutExtra("rank", rank); StatystykiPliku.PutExtra("fileid", files[k].id.ToString()); StartActivity(StatystykiPliku); }; //newtask.Wait(); //}); } rysuj(layouty, linearLayout); } //}).Wait(); }); }
protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); Xamarin.Essentials.Platform.Init(this, savedInstanceState); SetContentView(Resource.Layout.activity_dodajplik); String ip = GetString(Resource.String.ip); SupportActionBar.Title = ("Dodawanie pliku"); SupportActionBar.SetDisplayHomeAsUpEnabled(true); string username = Intent.GetStringExtra("username"); string rank = Intent.GetStringExtra("rank"); //Android.App.ActionBar actionbar = Button przegladajb = FindViewById <Button>(Resource.Id.przegladajb); Button wyslijplikb = FindViewById <Button>(Resource.Id.wyslijplikb); Button cancelb = FindViewById <Button>(Resource.Id.cancelb); przegladajb.Click += (sender, e) => { if (ContextCompat.CheckSelfPermission(this, Android.Manifest.Permission.ReadExternalStorage) != Android.Content.PM.Permission.Granted) { ActivityCompat.RequestPermissions(this, new string[] { Android.Manifest.Permission.ReadExternalStorage }, 1); } else { PerformFileSearch(); } }; wyslijplikb.Click += async(sender, e) => { View view = (View)sender; if (uri == null) { Toast.MakeText(Application.Context, "Proszę wybrać plik", ToastLength.Long).Show(); } else if (getFileName(uri).Length > 40) { Toast.MakeText(Application.Context, "Nazwa pliku jest za długa", ToastLength.Long).Show(); } else { TextView liczbawysylan = FindViewById <TextView>(Resource.Id.liczbawysylan); int ilewysylan = int.Parse(liczbawysylan.Text); bar = new ProgressDialog(this); bar.SetProgressStyle(ProgressDialogStyle.Horizontal); bar.Max = (ilewysylan); bar.Progress = 0; bar.Show(); ilewyslac = ilewysylan; numerwysylania = 0; String text = null; String filename = getFileName(uri); for (int j = 1; j <= ilewyslac; j++) { using (var resolverStream = ContentResolver.OpenInputStream(uri)) using (var streamContent = new StreamContent(resolverStream)) using (var byteArrayContent = new ByteArrayContent(await streamContent.ReadAsByteArrayAsync())) using (var formDataContent = new MultipartFormDataContent()) using (var usernameContent = new StringContent(username)) using (var filenameContent = new StringContent(filename)) using (var client = new HttpClient()) { formDataContent.Add(byteArrayContent, "content", filename); formDataContent.Add(usernameContent, "autor"); var watch = System.Diagnostics.Stopwatch.StartNew(); var response = await client.PostAsync(ip + "/file/upload/", formDataContent); var json = await response.Content.ReadAsStringAsync(); if (response.IsSuccessStatusCode) { Pola.File file = Newtonsoft.Json.JsonConvert.DeserializeObject <Pola.File>(json); bar.Progress = bar.Progress + (bar.Max / ilewyslac); numerwysylania++; if (numerwysylania == ilewyslac) { bar.Dismiss(); text = "Plik wysłano " + ilewyslac + " razy"; Toast.MakeText(Application.Context, text, ToastLength.Long).Show(); } float czasupload = 0; long roznicaping = 0; float avgping = 0; await response.Content.ReadAsStreamAsync(); watch.Stop(); czasupload = watch.ElapsedTicks; URL url = new URL(ip + "/ping/"); float i; for (i = 1; i <= 10; i++) { watch = System.Diagnostics.Stopwatch.StartNew(); HttpURLConnection urlConnection = (HttpURLConnection)url.OpenConnection(); urlConnection.ConnectTimeout = 1000; urlConnection.ReadTimeout = 1000; watch.Stop(); var po = watch.ElapsedTicks; roznicaping += po; urlConnection.Disconnect(); urlConnection.Dispose(); } avgping = roznicaping / i / 10000; czasupload = czasupload / 10000; System.Diagnostics.Debug.WriteLine("Tyle czasu uploaduje: " + czasupload); string urlstats = ip + "/file/updatestats/"; Pola.File filestat = new Pola.File(); filestat.temp_avg_latency = avgping; filestat.temp_upload_time = czasupload; filestat.temp_platform = GetString(Resource.String.jezyk); filestat.id = file.id; filestat.temp_filename = file.filename; filestat.temp_username = username; using (var client2 = new HttpClient()) { client2.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json")); var jsonpost = JsonConvert.SerializeObject(filestat); var content = new StringContent(jsonpost, Encoding.UTF8, "application/json"); var responsepost = await client2.PutAsync(urlstats + file.id, content); } client.DefaultRequestHeaders.ConnectionClose = true; } } } } }; cancelb.Click += (sender, e) => { View view = (View)sender; Finish(); }; }
public void GetStatsMethod(String url) { Task.Run(async() => { HttpClient client = new HttpClient(); ServerStats stat = new ServerStats(); var result = await client.GetAsync(url); var json = await result.Content.ReadAsStringAsync(); if (result.IsSuccessStatusCode) { Pola.File tempfile = Newtonsoft.Json.JsonConvert.DeserializeObject <Pola.File>(json); idpliku = FindViewById <TextView>(Resource.Id.idpliku); nazwapliku = FindViewById <TextView>(Resource.Id.nazwapliku); autor = FindViewById <TextView>(Resource.Id.autor); rozmiarbw = FindViewById <TextView>(Resource.Id.rozmiarbw); rozmiarmbw = FindViewById <TextView>(Resource.Id.rozmiarmbw); pliki_j = FindViewById <TextView>(Resource.Id.download_j); pliki_c = FindViewById <TextView>(Resource.Id.download_c); czas_j = FindViewById <TextView>(Resource.Id.czas_j); czas_c = FindViewById <TextView>(Resource.Id.czas_c); czasraw_j = FindViewById <TextView>(Resource.Id.czasraw_j); czasraw_c = FindViewById <TextView>(Resource.Id.czasraw_c); czas_na_mb_j = FindViewById <TextView>(Resource.Id.czas_na_mb_j); czas_na_mb_c = FindViewById <TextView>(Resource.Id.czas_na_mb_c); czasraw_na_mb_j = FindViewById <TextView>(Resource.Id.czasraw_na_mb_j); czasraw_na_mb_c = FindViewById <TextView>(Resource.Id.czasraw_na_mb_c); ping_j = FindViewById <TextView>(Resource.Id.ping_j); ping_c = FindViewById <TextView>(Resource.Id.ping_c); RunOnUiThread(() => { idpliku.Text = fileid.ToString(); nazwapliku.Text = tempfile.filename.ToString(); autor.Text = tempfile.author.ToString(); rozmiarbw.Text = (tempfile.file_sizeB).ToString(); rozmiarmbw.Text = (tempfile.file_sizeMB).ToString(); pliki_j.Text = (tempfile.number_of_downloads_java).ToString(); pliki_c.Text = (tempfile.number_of_downloads_csharp).ToString(); czas_j.Text = Math.Round(tempfile.average_time_downloaded_java, 2).ToString(); czas_c.Text = Math.Round(tempfile.average_time_downloaded_csharp, 2).ToString(); czasraw_j.Text = Math.Round(tempfile.raw_average_time_downloaded_java, 2).ToString(); czasraw_c.Text = Math.Round(tempfile.raw_average_time_downloaded_csharp, 2).ToString(); czas_na_mb_j.Text = Math.Round(tempfile.time_per_megabyte_download_java, 2).ToString(); czas_na_mb_c.Text = Math.Round(tempfile.time_per_megabyte_download_csharp, 2).ToString(); czasraw_na_mb_j.Text = Math.Round(tempfile.raw_time_per_megabyte_download_java, 2).ToString(); czasraw_na_mb_c.Text = Math.Round(tempfile.raw_time_per_megabyte_download_csharp, 2).ToString(); ping_j.Text = Math.Round(tempfile.average_latency_java, 2).ToString(); ping_c.Text = Math.Round(tempfile.average_latency_csharp, 2).ToString(); }); } else { Console.WriteLine("{0} ({1})", (int)result.StatusCode, result.ReasonPhrase); } }); }