2016-07-06 2 views
3

xamarin 응용 프로그램에서 사용하는 웹 API가 있습니다. 웹 API 호출을 처음 실행할 때마다 성공합니다. 그러나, 모든 연속적인 호출이 실패 : 응용 프로그램 자 마린Xamarin 응용 프로그램에서 두 번째 API 호출이 실패합니다. 첫 번째 호출은 항상 ok를 반환합니다.

예 : 제대로

[Activity(Label = "App1", MainLauncher = true, Icon = "@drawable/icon")] 
    public class MainActivity : Activity 
    { 
     int count = 1; 

     protected override void OnCreate(Bundle bundle) 
     { 
      base.OnCreate(bundle); 

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

      // Get our button from the layout resource, 
      // and attach an event to it 
      Button button = FindViewById<Button>(Resource.Id.MyButton); 

      button.Click += async delegate 
      { 
       var cl = new HttpClient(); 

       cl.BaseAddress = new Uri("http://10.0.5.220/api/"); 

       var request = new HttpRequestMessage(HttpMethod.Get, "values"); 
       HttpResponseMessage res = null; 
       button.Text = ""; 
       try 
       { 
        res = await cl.SendAsync(request); 
        var stream = await res.Content.ReadAsStreamAsync(); 

        StreamReader sr = new StreamReader(stream); 

        button.Text = await sr.ReadToEndAsync(); 
       } 
       catch (Exception ex) 
       { 

       } 
      }; 
     } 
    } 

첫 번째 호출이 반환 "값 1"과 "값 2"를

초 전화, 나는 다시 버튼 (및 이후를 클릭 할 때 하나는) :

{System.Net.WebException: Error: ConnectFailure (No route to host) ---> System.Net.Sockets.SocketException: No route to host at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP) [0x000cb] in /Users/builder/data/lanes/3415/7db2aac3/source/mono/mcs/class/System/System.Net.Sockets/Socket.cs:1313 at System.Net.WebConnection.Connect (System.Net.HttpWebRequest request) [0x0019b] in /Users/builder/data/lanes/3415/7db2aac3/source/mono/mcs/class/System/System.Net/WebConnection.cs:195 --- End of inner exception stack trace --- at System.Net.HttpWebRequest.EndGetResponse (IAsyncResult asyncResult) [0x0005e] in /Users/builder/data/lanes/3415/7db2aac3/source/mono/mcs/class/System/System.Net/HttpWebRequest.cs:1005 at System.Threading.Tasks.TaskFactory 1[TResult].FromAsyncCoreLogic (IAsyncResult iar, System.Func 2 endFunction, System.Action 1 endAction, System.Threading.Tasks.Task 1 promise, Boolean requiresSynchronization) [0x00014] in /Users/builder/data/lanes/3415/7db2aac3/source/mono/external/referencesource/mscorlib/system/threading/Tasks/FutureFactory.cs:550

그러나 이것은 단지 아이폰 OS 응용 프로그램이 제대로 작동, 안드로이드 응용 프로그램에서 발생합니다.

컨트롤러의 중단 점에 절대로 도달하지 않습니다. 컨트롤러에 도달하기 전에 요청이 취소 된 것 같습니다.

답변

0

HTTP 요청, 스트림 및 모든 내용에 익숙하지 않기 때문에 내 대답은 문제와 완전히 관련이 없을 수 있습니다. 그렇다면 의견이나 편집을 통해 나를 수정하십시오.

응답을받는 방법으로 StreamReader를 열 수 있습니다. 이것을 대신 사용하십시오 :

using(StreamReader sr = new StreamReader(stream)) { 
    button.Text = await sr.ReadToEndAsync(); 
} 

이렇게하면 작업 완료 후 StreamReader를 닫을 수 있습니다.

관련 문제