public async Task <JsonValue> Fetchdata(string url) { lv = (ListView)FindViewById(Resource.Id.listView1); int id = Intent.GetIntExtra("ID", 46); cb = new CreateEventDatabase(); var Tables = cb.Table(); HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(url)); request.ContentType = "application/json"; request.Method = "GET"; try { // Send the request to the server and wait for the response: using (WebResponse response = await request.GetResponseAsync()) { // Get a stream representation of the HTTP web response: using (System.IO.Stream stream = response.GetResponseStream()) { JsonValue jsonDoc = await Task.Run(() => JsonObject.Load(stream)); JSONObject parentObject = new JSONObject(jsonDoc.ToString()); JSONArray jsonArray = parentObject.OptJSONArray("Apps"); for (int i = 0; i < jsonArray.Length(); i++) { JSONObject jsonObject = jsonArray.GetJSONObject(i); //Declares the Class Events and inserts the respons from the api to the class variables. //this structure of the method will be used as the standard in the other activity where a class will //take the api response and add it to the view. Events eventData = new Events() { ID = int.Parse(id.ToString()), Name = jsonObject.GetString("ProjectName"), Image = jsonObject.GetString("Banner"), ProjectID = jsonObject.GetInt("ProjectID"), BannerisIcon = jsonObject.GetBoolean("BannerIsIcon") }; //add the class to and List events.Add(eventData); //Insert the list to SQLite cb.InsertIntoTable(eventData); } lv.Adapter = new CustomListAdapter(this, events); // Return the JSON document: return(url); } } } catch (Exception) { //Writes out a toast if id is wrong and goes back to MainActivity Toast.MakeText(this, "Kunde inte hitta Id: " + id.ToString(), ToastLength.Long).Show(); var ValueActivity = new Intent(this, typeof(MainActivity)); StartActivity(ValueActivity); return(null); } }
protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); RequestWindowFeature(WindowFeatures.NoTitle); // Set our view from the "main" layout resource SetContentView(Resource.Layout.activity_main); //First thing is to create the whole database with this class. CreateEventDatabase db = new CreateEventDatabase(); db.createdatabase(); editText = (EditText)FindViewById(Resource.Id.Input_Edittext); button = (Button)FindViewById(Resource.Id.Send_Button); Contactbutton = (Button)FindViewById(Resource.Id.Contact_Button); Aboutbutton = (Button)FindViewById(Resource.Id.About_Button); //Button click with a if statment that checks if the inputfield is empty. Also a filter that allows maximum 4 characters to the editText. //The url gets the ID that the user writes in the editText and inserts it to the url. //also the id that the user types in will be sent as a int to the next Activity. editText.SetFilters(new IInputFilter[] { new InputFilterLengthFilter(4) }); button.Click += (sender, e) => { int id = 0; if (editText.Text == "") { Toast.MakeText(this, "Vänligen skriv in ett giltigt Id!", ToastLength.Long).Show(); } else { //api url edittext= the id string url = "https://api.itmmobile.com/customers/" + editText.Text.TrimEnd() + "/apps?base64={base64}"; id = int.Parse(editText.Text); var ValueActivity = new Intent(this, typeof(ResultActivity)); ValueActivity.PutExtra("APIurl", url); ValueActivity.PutExtra("ID", id); StartActivity(ValueActivity); } }; //Two methods that takes the user to a new Acitvity Contactbutton.SetTextColor(Android.Graphics.Color.White); Contactbutton.SetBackgroundColor(Android.Graphics.Color.ParseColor("#000000")); Contactbutton.Click += (sender, e) => { var ValueActivity = new Intent(this, typeof(Contactactivity)); StartActivity(ValueActivity); }; Aboutbutton.SetTextColor(Android.Graphics.Color.White); Aboutbutton.SetBackgroundColor(Android.Graphics.Color.ParseColor("#000000")); Aboutbutton.Click += (sermder, e) => { var ValueActivity = new Intent(this, typeof(Abountactivityy)); StartActivity(ValueActivity); }; }