2017-05-31 6 views
0

HTTP要求を実行してXamarinで応答本文を取得することは可能ですか?彼らのドキュメントから多くのコード例を試してみましたが、それは私のAndroidデバイスでは動作しませんので、次のような例外があります:NameResolutionFailure。Xamarin WebClientのエラーを解決する方法NameResolutionFailure?

私のコードは次のとおりです。

 Uri uri = new Uri("http://www.google.com"); 
     string ret = "ERROR"; 
     try 
     { 
      using (WebClient webClient = new WebClient()) 
      { 
       //You can set webClient.Headers there 
       webClient.Encoding = System.Text.Encoding.UTF8; 
       ret = webClient.DownloadString(uri); 
       Console.WriteLine("Data: " + ret); 
      } 
     } 
     catch (Exception ex) 
     { 
      ret = ex.Message; 
      Console.WriteLine("Error: " + ret); 
     } 

私はあなたがWIFI経由の要求をやっているときにこのエラーがoccuresことが判明、しかしseriousely?

"IPアドレスでホスト名を置き換える"のような汚い解決策はうまくいきません。私はXamarinを初めて使っていて、悪い印象を持っています。

+0

uがNewtonsoft.Jsonを使用してみましたのです。 –

+0

いいえ、どのように使用しますか?またはリンク? –

+0

これは第三者の図書館ですか?なぜxamarinにはこれに固有の解決策がないのですか? –

答えて

0

私はNewtonsoft.Jsonを使用してデータをフェッチする方法を簡単な例を書いています:

public static async Task<Details> getDetails(string authKey, string AppVersionandBuild) 
{ 

    var response = await JsonWebCall<Details>.GetDirect(
     JsonWebCall<object>.baseURl + "MyApp/GetDetails", 
     authKey, 
     AppVersionandBuild); 

    return response.Item1; 
} 

そして、これは私のJsonWebCallクラス

public class JsonWebCall<T> 
{ 
    public static string baseURl = "http://YourServiceURL/"; 

    /// <summary> 
    /// Gets the client. 
    /// </summary> 
    /// <returns></returns> 
    public static HttpClient GetClient() 
    { 
     var timeout = new TimeSpan(0, 10, 0); 
     var client = new HttpClient(new ModernHttpClient.NativeMessageHandler()); 
     // Due to a bug in Xamarin's HttpClient, this value MUST exceed the timeout of the native http client. 
     // The REAL timeout will be the native http client's timeout which will properly throw the timeout exception. 
     client.Timeout = timeout.Add(new TimeSpan(0, 1, 0)); 
     return client; 
    } 

    /// <summary> 
    /// Gets the message. 
    /// </summary> 
    /// <param name="method">The method.</param> 
    /// <param name="url">The URL.</param> 
    /// <param name="authkey">The authkey.</param> 
    /// <param name="Manufacturer">The manufacturer.</param> 
    /// <param name="Model">The model.</param> 
    /// <param name="OS">The os.</param> 
    /// <param name="OSVersion">The os version.</param> 
    /// <param name="UniqueIdentifier">The unique identifier.</param> 
    /// <param name="AppVersionandBuild">The application versionand build.</param> 
    /// <returns></returns> 
    public static HttpRequestMessage GetMessage(HttpMethod method, string url, string authkey, string Manufacturer, string Model, string OS, string OSVersion, string UniqueIdentifier, string AppVersionandBuild) 
    { 
     var message = new HttpRequestMessage(method, url); 

     if (!string.IsNullOrWhiteSpace(authkey)) 
      message.Headers.Add("X-AUTH-KEY", new List<string>() { authkey }); 
     if (!string.IsNullOrWhiteSpace(UniqueIdentifier)) 
      message.Headers.Add("X-DEVICE-ID", new List<string>() { UniqueIdentifier }); 
     if (!string.IsNullOrWhiteSpace(Model)) 
      message.Headers.Add("X-DEVICE-MODEL", new List<string>() { Model }); 
     if (!string.IsNullOrWhiteSpace(Manufacturer)) 
      message.Headers.Add("X-DEVICE-MFG", new List<string>() { Manufacturer }); 
     if (!string.IsNullOrWhiteSpace(OSVersion)) 
      message.Headers.Add("X-OS-VERSION", new List<string>() { OSVersion }); 
     if (!string.IsNullOrWhiteSpace(OS)) 
      message.Headers.Add("X-OS-NAME", new List<string>() { OS }); 
     if (!string.IsNullOrWhiteSpace(AppVersionandBuild)) 
      message.Headers.Add("X-APP-VERSION", new List<string>() { AppVersionandBuild }); 
     return message; 
    } 
    /// <summary> 
    /// Get data that is not encapulated in an odata call 
    /// </summary> 
    /// <param name="url">the url of the service action</param> 
    /// <param name="authkey">The authentication key required y the server</param> 
    /// <param name="DeviceID">the device id of the device making this service call</param> 
    /// <returns></returns> 
    public async static Task<Tuple<T>> Get(string url, string authKey, string Manufacturer, string Model, string OS, string OSVersion, string UniqueIdentifier, string AppVersionandBuild) 
    { 
     var client = GetClient(); 
     var req = GetMessage(HttpMethod.Get, url, authKey, Manufacturer, Model, OS, OSVersion, UniqueIdentifier, AppVersionandBuild); 
     var response = await client.SendAsync(req).ConfigureAwait(false); 
     T returnedObject = default(T); 
     returnedObject = JsonConvert.DeserializeObject<T>(await response.Content.ReadAsStringAsync().ConfigureAwait(false)); 

     return new Tuple<T>(returnedObject); 
    } 
    /// <summary> 
    /// Gets the request. 
    /// </summary> 
    /// <param name="method">The method.</param> 
    /// <param name="url">The URL.</param> 
    /// <param name="authkey">The authkey.</param> 
    /// <param name="AppVersionandBuild">The application versionand build.</param> 
    /// <returns></returns> 
    public static HttpRequestMessage GetRequest(HttpMethod method, string url, string authkey, string AppVersionandBuild) 
    { 
     var message = new HttpRequestMessage(method, url); 

     if (!string.IsNullOrWhiteSpace(authkey)) 
      message.Headers.Add("X-AUTH-KEY", new List<string>() { authkey }); 
     if (!string.IsNullOrWhiteSpace(AppVersionandBuild)) 
      message.Headers.Add("X-APP-VERSION", new List<string>() { AppVersionandBuild }); 
     return message; 
    } 

    /// <summary> 
    /// Gets the direct. 
    /// </summary> 
    /// <param name="url">The URL.</param> 
    /// <param name="authKey">The authentication key.</param> 
    /// <param name="AppVersionandBuild">The application versionand build.</param> 
    /// <returns></returns> 
    public async static Task<Tuple<T>> GetDirect(string url, string authKey, string AppVersionandBuild) 
    { 
     var client = GetClient(); 
     var req = GetRequest(HttpMethod.Get, url, authKey, AppVersionandBuild); 
     var response = await client.SendAsync(req).ConfigureAwait(false); 
     T returnedObject = default(T); 
     var result = await response.Content.ReadAsStringAsync().ConfigureAwait(false); 
     returnedObject = JsonConvert.DeserializeObject<T>(result); 
     return new Tuple<T>(returnedObject); 
    } 
} 
+0

私はそれを試してみると、あなたに戻って、おかげで、私はどのように私はVisual StudioでNewtonsoft.Jsonをインポートする必要がありますか? –

+0

btw AppVersionandBuildとauthKeyは何ですか?シンプルなHTTPリクエストを行うのはなぜそんなに複雑なのですか? –

+1

これらは必須です。新しいバージョンにアップグレードする場合は、古いバージョンを使用しているユーザーをサポートする必要があります。その場合は、バージョンを使用しているユーザーに基づいてリクエストを一意にするために、アプリケーションのバージョンやビルドなどを渡す必要があります。 –

関連する問題