2016-11-04 83 views
-2

消耗問題的REST服務,我有使用ASP.NET的Web API,它返回一個JSON清單和接收,以及,在.NET平臺做一個POSTGET我使用的東西像一個API:如何使用Android Studio的

private string UrlLocalTest = "http://192.168.0.102:3000/api/"; 

    public async Task<HttpResponseMessage> PostWeb(Model model) 
    { 
     HttpClient client = new HttpClient(); 
     Uri url = new Uri(string.Concat(Url, "gone/POST")); 
     return await client.PostAsJsonAsync(url, model); 
    } 

    public async Task<HttpResponseMessage> GET(string name) 
    { 
     var client = new HttpClient(); 
     Uri url = new Uri(string.Concat(UrlLocalTestTwo, "/GET" + name)); 
     var response = await client.GetAsync(url); 
     return response; 
    } 

,並返回使用oauth2我的WebAPI令牌它是這樣:

public async Task<HttpResponseMessage> Authenticate(string pEmail = null, string pPassword = null) 
    { 
     var Client = new HttpClient(); 

      var _tokenArgs = new FormUrlEncodedContent(new[] 
      { 
       new KeyValuePair<string,string>("username",pEmail), 
       new KeyValuePair<string, string>("password",pPassword), 
       new KeyValuePair<string, string>("grant_type","password") 
      }); 
      Uri url = new Uri(UrlLocalTest); 
      return await Client.PostAsync(url, _tokenArgs);     
    } 

我如何執行使用的是Android這些方法和措施?我發現接近this的東西,但我相信在Android上更復雜。

回答

1

看一看OkHttp。它提供了對基於HTTP的服務的簡單訪問,而不需要太多的抽象。爲什麼你認爲在Android上它會更復雜?

+0

謝謝,我會檢查這個環節,因爲我是從.NET來了,有基本的WPF相同的代碼在表單,工作在UWP,我們只需要添加一個參考和做過,但使用android中,我看到的是不同的東西,如Java語言,但不是所有的「普通」Java庫上的Java庫。謝謝。 –

相關問題