小编mar*_*aan的帖子

如何处理来自httpclient的数据

我正在开发一个新的Windows Phone 8应用程序.我正在连接到一个返回有效json数据的webservice.我正在使用longlistselector来显示数据.当我在GetAccountList()中使用字符串json时,这很好用; 但是当从DataServices类接收数据时,我收到错误"无法将类型'System.Threading.Tasks.Task'隐式转换为字符串".不知道出了什么问题.欢迎任何帮助.谢谢!

DataServices.cs

    public async static Task<string> GetRequest(string url)
    {
        HttpClient httpClient = new HttpClient();

        await Task.Delay(250);

        HttpResponseMessage response = await httpClient.GetAsync(url);
        response.EnsureSuccessStatusCode();
        string responseBody = await response.Content.ReadAsStringAsync();
        Debug.WriteLine(responseBody);
        return await Task.Run(() => responseBody);
    }
Run Code Online (Sandbox Code Playgroud)

AccountViewModel.cs

 public static List<AccountModel> GetAccountList()
    {
        string json = DataService.GetRequest(url);
        //string json = @"{'accounts': [{'id': 1,'created': '2013-10-03T16:17:13+0200','name': 'account1 - test'},{'id': 2,'created': '2013-10-03T16:18:08+0200','name': 'account2'},{'id': 3,'created': '2013-10-04T13:23:23+0200','name': 'account3'}]}";
        List<AccountModel> accountList = new List<AccountModel>();

        var deserialized = JsonConvert.DeserializeObject<IDictionary<string, JArray>>(json);

        JArray recordList = deserialized["accounts"];


        foreach (JObject record …
Run Code Online (Sandbox Code Playgroud)

c# json mvvm windows-phone-8 asynchttpclient

5
推荐指数
1
解决办法
5456
查看次数

登录后每个请求上的Angular $ http设置cookie

我在AngularJS中使用$ http调用时遇到麻烦.我创建了以下服务

    crApp.service('authService', function ($http) {

    var urlBase = 'http://192.168.xx.xx:8081';

    // POST api/login/
    this.login = function (credentials) {
    return $http.post(urlBase + "/api/login", credentials);
    };

  // POST api/logout/
  this.logout = function () {
    return $http.post(urlBase + "/api/logout/", "");
  };

}); //end service

crApp.service('dataService', function ($http) {

  var urlBase = 'http://192.168.xx.xx:8081';

  // POST api/query/
  this.pull = function (query) {
    return $http.post(urlBase + "/api/query", query);
  };
Run Code Online (Sandbox Code Playgroud)

从控制器我调用登录方法:

$scope.login = function(){
    authService.login(credentials)
        .success(function(data) {
            console.log("RESULT LOGIN: " + data );
        })
        .error(function(data, status, headers, …
Run Code Online (Sandbox Code Playgroud)

javascript cookies angularjs

5
推荐指数
1
解决办法
1万
查看次数