Xamarin Android Rest API到PassSlot - nameResolutionFailure

时间:2014-08-19 13:17:14

标签: android json rest xamarin httprequest

我使用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)。

有关此处可能发生的事情以及我收到错误的原因的任何提示。

1 个答案:

答案 0 :(得分:0)

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

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