【问题标题】:Xamarin Android Rest API to PassSlot - nameResolutionFailureXamarin Android Rest API 到 PassSlot - nameResolutionFailure
【发布时间】:2014-08-19 13:17:13
【问题描述】:

我正在使用 REST API 访问 PassSlot 以尝试生成通行证/优惠券。似乎当我运行程序时出现错误:“错误:NameResolutionFailure”。

我有:

public static async Task<string> SendAndReceiveJsonRequest()
        {
            string responseStr = null;
            string uri = "https://api.passslot.com/v1/templates/my-template-ID/pass";

            // Create a json string with a single key/value pair.

            var json = new JObject (new JProperty ("lastName", lastName),
                                    new JProperty ("firstName", firstName),
                                    new JProperty ("percentOff", percentOff),
                                    new JProperty ("offerDescription", offerDescription),
                                    new JProperty ("entityName", entityName),
                                    new JProperty ("expiry", expiry));

            //Console.WriteLine ("Jake's JSON " + json.ToString ());

            using (var httpClient = new HttpClient ())

            {    

                httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("My-Key-Here-xxx-xxx-xxx");
                //create the http request content

                HttpContent content = new StringContent(json.ToString());


                try
                {
                    // Send the json to the server using POST

                    Task<HttpResponseMessage> getResponse = httpClient.PostAsync(uri, content);
                    // Wait for the response and read it to a string var

                    HttpResponseMessage response = await getResponse;
                    responseStr = await response.Content.ReadAsStringAsync();

                }
                catch (Exception e)
                {
                    Console.WriteLine("Error communicating with the server: " + e.Message);
                }
            }

            return responseStr;
        }

我通过 Nexus 4 在 Android 4.4 上运行它。我使用的是 3G(不是 wifi)。

关于这里可能发生的事情以及我为什么会收到错误的任何提示。

【问题讨论】:

  • 问题似乎是在调试模式下运行时。在发布模式下很好。
  • 也许尝试发布一些应用程序输出

标签: android json rest xamarin httprequest


【解决方案1】:

如果 url 请求在本地网络中,请检查您的 url ip 是否在 hosts 设备中设置(智能手机、平板电脑的 hosts 设置,无论您使用什么来测试)

PD。 要在 android 中编辑主机设置设备,您可以使用此应用 https://play.google.com/store/apps/details?id=com.nilhcem.hostseditor

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-26
    相关资源
    最近更新 更多