Xamarin VS2017 ...在休息模式下的应用

时间:2018-04-09 03:18:13

标签: xamarin.android

我正在尝试使用Xamarin和VS2017。我正在关注Xamarin网站上的这个tutorial。我正在尝试运行它,但是当我点击按钮时,我将应用程序处于中断模式。见下文:

enter image description here

应用程序会加载Json数据并运行ParseAndDisplay方法。该方法完成后,将显示错误。

我不知道如何调试它。它没有给出任何具体细节。怎么会尝试调试这个? 代码如下:

using Android.App;
using Android.Widget;
using Android.OS;
using Android.Content;
using Android.Views;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using System;
using System.Json;
using Android.Graphics;

namespace App1
{
    [Activity(Label = "App1", MainLauncher = true)]
    public class MainActivity : Activity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            try
            {
                Button btnRefresh = FindViewById<Button>(Resource.Id.btnRefresh);

                string url = "http://data.com/json";
                btnRefresh.Click += async (sender, e) => {
                    JsonValue json = await FetchAsync(url);
                    ParseAndDisplay(json);
                };

        }

        // Gets weather data from the passed URL.
        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));
                    Console.Out.WriteLine("Response: {0}", jsonDoc.ToString());

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

        // Parse the weather data, then write temperature, humidity, 
        // conditions, and location to the screen.
        private void ParseAndDisplay(JsonValue json)
        {
            // Extract the array of name/value results for the field name "weatherObservation". 
            string title = json["title"];
        }
    }
}

AXML文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button
        android:id="@+id/btnRefresh"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="Get Data" />
    <TextView
        android:id="@+id/tvTitle"
        android:text="@string/tvTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="left" />
    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/ivImage"
        android:scaleType="fitCenter" />
    <TextView
        android:id="@+id/tvAltText"
        android:text="fklsdjfl"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

我会在FetchAsync的第一个语句上设置一个断点,逐行逐行,直到它爆炸为止。

这很可能是由“http://data.com/json”造成的,而“Writing Akka Streams Connectors for existing APIs”不存在,而我的猜测是response.GetResponseStream()