예제 #1
0
        public static string ReadFromRawResource(Context context, int resourceid)
        {
            Stream resourcereader = context.Resources.OpenRawResource(resourceid);
            Writer writer         = new Java.IO.StringWriter();

            try
            {
                BufferedReader reader = new BufferedReader(new InputStreamReader(resourcereader, "UTF-8"));
                string         line   = reader.ReadLine();
                while (line != null)
                {
                    writer.Write(line);
                    line = reader.ReadLine();
                }
            } catch (Exception e)
            {
            } finally
            {
                try
                {
                    resourcereader.Close();
                } catch (Exception e)
                {
                }
            }

            return(writer.ToString());
        }
예제 #2
0
        // >> autocomplete-remote-do-in-background-xamarin
        protected override Java.Lang.Void RunInBackground(params string[] @params)
        {
            HttpURLConnection urlConnection = null;

            try
            {
                URL url = new URL
                              ("http://www.telerik.com/docs/default-source/ui-for-ios/airports.json?sfvrsn=2");

                urlConnection = (HttpURLConnection)url.OpenConnection();

                urlConnection.RequestMethod        = "GET";
                urlConnection.UseCaches            = false;
                urlConnection.AllowUserInteraction = false;
                urlConnection.Connect();
                HttpStatus status = urlConnection.ResponseCode;

                if (status.Equals(HttpStatus.Ok))
                {
                    BufferedReader reader = new BufferedReader
                                                (new InputStreamReader(urlConnection.InputStream));
                    char[] buffer = new char[1024];
                    int    n;
                    Writer writer = new Java.IO.StringWriter();

                    while ((n = reader.Read(buffer)) != -1)
                    {
                        writer.Write(buffer, 0, n);
                    }

                    string json = writer.ToString();

                    try
                    {
                        JSONObject jObj = new JSONObject(json);
                        data = jObj.GetJSONArray("airports");
                    }
                    catch (JSONException ex)
                    {
                        ex.PrintStackTrace();
                    }
                }
            }
            catch (Java.IO.IOException e)
            {
                e.PrintStackTrace();
            }
            finally
            {
                if (urlConnection != null)
                {
                    urlConnection.Disconnect();
                }
            }

            return(null);
        }
예제 #3
0
        private string convertJSONFileContentToAString(InputStream inputStream)
        {
            char[] buffer       = new char[1024];
            Writer stringWriter = new StringWriter();

            try
            {
                //Reader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
                Reader bufferedReader = null; //new BufferedReader(new InputStreamReader(inputStream));
                int    n;
                while ((n = bufferedReader.Read(buffer)) != -1)
                {
                    stringWriter.Write(buffer, 0, n);
                }
            }
            finally
            {
                stringWriter.Close();
            }
            return(stringWriter.ToString());
        }