Android应用程式Json [英] Android app Json

查看:107
本文介绍了Android应用程式Json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写android应用并遇到问题.

I writing android app and faced problem.

我有API并将其写入文件.

I have API and write it to file.

写作守则

 string url2 = "http://new.murakami.ua/?mkapi=getProducts";
        JsonValue json = await FetchAsync(url2);

string path =  System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);

                string filename = System.IO.Path.Combine(path, "myfile.txt");
                using (var streamWriter = new StreamWriter(filename, true))
                {
                    streamWriter.Write(json.ToString());
                    streamWriter.Close();


                }
ParseAndDisplay1(json);
        ParseAndDisplay2(json);
        ParseAndDisplay3(json);
        ParseAndDisplay4(json);
        ParseAndDisplay5(json);
        ParseAndDisplay6(json);
        ParseAndDisplay7(json);
        ParseAndDisplay8(json);

    }
 private async Task<JsonValue> FetchAsync(string url)
    {
        // Create an HTTP web request using the URL:
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(url));
        request.ContentType = "application/json";
        request.Method = "GET";

        // 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 (Stream stream = response.GetResponseStream())
            {
                // Use this stream to build a JSON document object:
                JsonValue jsonDoc = await Task.Run(() => JsonObject.Load(stream));
                //dynamic data = JObject.Parse(jsonDoc[15].ToString);
                Console.Out.WriteLine("Response: {0}", jsonDoc.ToString());


                // Return the JSON document:
                return jsonDoc;
            }
        }
    }

我想从中读取信息并显示.

I want to read information from it and display.

我尝试了类似的方法,但是没有用.

I tried something like this, but it don't works.

        private void ParseAndDisplay1(JsonValue json)
    {



        TextView productname = FindViewById<TextView> (Resource.Id.posttittle);
        TextView price = FindViewById<TextView> (Resource.Id.price);
        TextView weight = FindViewById<TextView> (Resource.Id.weight);
        productname.Click += delegate {
            var intent404 = new Intent (this, typeof(SoupesDetailActivity1));
            StartActivity (intent404);
        };
        string path = System.Environment.GetFolderPath (System.Environment.SpecialFolder.Personal);
        string filename = System.IO.Path.Combine (path, "myfile.txt");

        using (var streamReader = new StreamReader (filename, true)) {
            JsonValue firstitem = json [81];

            productname.Text = firstitem ["post_title"];
            price.Text = firstitem ["price"] + " грн";
            weight.Text = firstitem ["weight"] + "г";
        }



    }

您能帮我解决这个问题吗?

Can you help me with this problem?

推荐答案

正在使用Xamarin的人.但是这里有一些技巧,告诉我如何在Android本机代码中解决它.

Man you are using Xamarin. But here some tips of how I solve it in Android native code.

  1. 我将文件保存在Assets文件夹中;
  2. 他们以.json扩展名而不是.txt的身份保存了这笔交易.
  3. 我使用以下代码访问了它:

  1. I saved the file in the Assets folder;
  2. them saved the fale with .json extension not .txt;
  3. I accessed it using this code:

private static String loadJSONFromAsset(Context context, String fileName) { String json = null; try { InputStream is = context.getAssets().open("contactos/" + fileName + ".json"); int size = is.available(); byte[] buffer = new byte[size]; is.read(buffer); is.close(); json = new String(buffer); } catch (IOException ex) { ex.printStackTrace(); return null; } return json; }

private static String loadJSONFromAsset(Context context, String fileName) { String json = null; try { InputStream is = context.getAssets().open("contactos/" + fileName + ".json"); int size = is.available(); byte[] buffer = new byte[size]; is.read(buffer); is.close(); json = new String(buffer); } catch (IOException ex) { ex.printStackTrace(); return null; } return json; }

之后,我毫无问题地解析JSON.

After that I parse the JSON with no problem.

它运行完美,希望对您有所帮助.

And it works perfectly, I hope it will help you.

这篇关于Android应用程式Json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆