Exemple #1
0
        public static Makanan GetDetailMakanan(string query)
        {
            Makanan makanan = new Makanan();
            var     client  = new RestClient("https://trackapi.nutritionix.com/v2/natural/nutrients");
            var     request = new RestRequest(Method.POST);

            request.AddHeader("x-app-id", appId);
            request.AddHeader("x-app-key", appKey);
            request.AddHeader("content-type", "application/x-www-form-urlencoded");
            request.AddParameter(
                "application/x-www-form-urlencoded",
                $"query={query}",
                ParameterType.RequestBody
                );

            IRestResponse response      = client.Execute(request);
            JsonObject    obj           = (JsonObject)SimpleJson.DeserializeObject(response.Content);
            JsonArray     listMakanan   = obj["foods"] as JsonArray;
            JsonObject    detailMakanan = listMakanan[0] as JsonObject;

            makanan.nama        = (string)detailMakanan["food_name"];
            makanan.sajian      = detailMakanan["serving_weight_grams"].ToString();
            makanan.kalori      = detailMakanan["nf_calories"].ToString();
            makanan.karbohidrat = detailMakanan["nf_total_carbohydrate"].ToString();
            makanan.protein     = detailMakanan["nf_protein"].ToString();
            makanan.lemak       = detailMakanan["nf_total_fat"].ToString();

            return(makanan);
        }
Exemple #2
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.activity_daftar_gizi_hasil);

            // set toolbar
            AndroidX.AppCompat.Widget.Toolbar toolbar = FindViewById <AndroidX.AppCompat.Widget.Toolbar>(Resource.Id.toolbar);
            SetSupportActionBar(toolbar);
            SupportActionBar.SetDisplayHomeAsUpEnabled(true);

            // Create your application here
            string  query      = Intent.GetStringExtra("query");
            Makanan resultList = ApiClient.GetDetailMakanan(query);

            TextView tvNamaMakanan = FindViewById <TextView>(Resource.Id.tvNamaMakanan);
            TextView tvSajian      = FindViewById <TextView>(Resource.Id.tvSajian);
            TextView tvKalori      = FindViewById <TextView>(Resource.Id.tvKalori);
            TextView tvKarbohidrat = FindViewById <TextView>(Resource.Id.tvKarbohidrat);
            TextView tvProtein     = FindViewById <TextView>(Resource.Id.tvProtein);
            TextView tvLemak       = FindViewById <TextView>(Resource.Id.tvLemak);

            tvNamaMakanan.Text = resultList.nama.ToString() + " kal";
            tvSajian.Text      = "Jumlah sajian per " + resultList.sajian.ToString() + " g";
            tvKalori.Text      = resultList.kalori.ToString() + " g";
            tvKarbohidrat.Text = resultList.karbohidrat.ToString() + " g";
            tvProtein.Text     = resultList.protein.ToString() + " g";
            tvLemak.Text       = resultList.lemak.ToString() + " g";

            // button onclick
            Button btnKembali = this.FindViewById <Button>(Resource.Id.btnKembali);

            btnKembali.Click += (sender, e) => {
                Finish();
            };
        }