Example #1
0
        protected ObservableCollection <tescoApiJson> doHmtl(params string[] strParams)
        {
            string requestType = strParams[1];
            string strResponse = "";
            string httpType    = "";

            if (requestType != "tescoData")
            {
                if (appApplicaiton.acct == null)
                {
                    return(null);
                }
                else
                {
                    idToken  = appApplicaiton.acct.IdToken;
                    strPost += "idToken=" + idToken + "&";
                }
            }

            HttpURLConnection uRLConnection = null;

            try
            {
                if (requestType == "getSpecific" || requestType == "removeSpecific")
                {
                    httpType      = "POST";
                    uRLConnection = openConnection(strParams[0], "Php_Landing.php", requestType, httpType, uRLConnection);
                    strPost      += "Pan= " + strParams[2] + "&";
                }
                else if (requestType == "addNew")
                {
                    httpType      = "POST";
                    uRLConnection = openConnection(strParams[0], "Php_Landing.php", requestType, httpType, uRLConnection);
                    strPost      += "Pan= " + strParams[2] + "&";
                    strPost      += "Amount= " + strParams[3] + "&";
                    strPost      += "expiryDate=" + strParams[4] + "&";
                    strPost      += "boolRemoveItem=" + strParams[5] + "&";
                }
                else if (requestType == "tescoData")
                {
                    httpType = "GET";
                    string urlHeader = "?gtin=" + strParams[2];
                    uRLConnection = openConnection(strParams[0], urlHeader, requestType, httpType, uRLConnection);
                    uRLConnection.SetRequestProperty("Ocp-Apim-Subscription-Key", "b775ff6e5f284518851939473019dd7a");
                }
                else if (requestType == "getAll")
                {
                    httpType      = "POST";
                    uRLConnection = openConnection(strParams[0], "Php_Landing.php", requestType, httpType, uRLConnection);
                }

                if (httpType == "POST")
                {
                    OutputStream   outputPost = new BufferedOutputStream(uRLConnection.OutputStream);
                    BufferedWriter writer     = new BufferedWriter(new OutputStreamWriter(uRLConnection.OutputStream, "UTF-8"));

                    writer.Write(strPost);

                    writer.Flush();
                    writer.Close();
                    uRLConnection.OutputStream.Close();

                    System.Console.WriteLine("Output String : {0}", outputPost.ToString());
                }


                int responseCode = (int)uRLConnection.ResponseCode;

                if (responseCode == (int)HttpStatus.Ok)
                {
                    string         line;
                    BufferedReader br = new BufferedReader(new InputStreamReader(uRLConnection.InputStream));
                    if (requestType == "tescoData")
                    {
                        strResponse += "[";
                    }
                    while ((line = br.ReadLine()) != null)
                    {
                        strResponse += line;
                    }
                    if (requestType == "tescoData")
                    {
                        strResponse += "]";
                    }
                }
                else if (responseCode == (int)HttpStatus.Unauthorized)
                {
                    strResponse = "[{'Error': 'Unauthorized User' }]";
                }
                else
                {
                    strResponse = "Nothing Received";
                }
                strResponse = Regex.Unescape(strResponse);
                strResponse = strResponse.Replace(@"\", "");

                rootJson = JsonConvert.DeserializeObject <ObservableCollection <tescoApiJson> >(strResponse);

                if (requestType == "tescoData")
                {
                    foreach (tescoApiJson item in rootJson)
                    {
                        item.flags = new Dictionary <string, string>();
                        item.flags.Add("dataReturned", "true");
                    }
                }
            }
            catch (MalformedURLException error)
            {
                System.Console.WriteLine("Malformed URL: {0} ", error);
            }
            catch (SocketTimeoutException error)
            {
                System.Console.WriteLine("Socket Timeout: {0} ", error);
            }
            catch (Exception error)
            {
                System.Console.WriteLine("Error : {0}", error);
            }
            finally
            {
                uRLConnection.Disconnect();
            }



            return(rootJson);
        }