【发布时间】:2016-08-06 14:10:11
【问题描述】:
我正在用 Xamarin C# 编写 Android 应用程序。
我从 JSON 中解析信息,然后将其显示在字段中。
我使用这个代码:
string url2 = "http://papajohn.pp.ua/?mkapi=getProductsByCat&cat_id=74";
JsonValue json = await FetchAsync(url2);
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;
}
}
}
但我有一个问题。当我打开 Activity 时,它会冻结 2-3 秒,然后所有信息都会显示在字段中。
我能否顺利下载该数据。第一个字段,然后是第二个字段,以此类推。
如果可以的话,怎么做?
【问题讨论】:
-
你可以使用 Volley,例如androidhive.info/2014/09/android-json-parsing-using-volley
-
这适用于 Java。我使用 C# @AllLelopath
-
显示您如何调用
FetchAsync并在您的视图中设置值。 -
你在哪里调用 FetchAsync?在事件处理程序中还是在哪里?能否提供更多信息?
标签: c# android json xamarin xamarin.android