[英]How to save accesstoken to string from API (C# Xamarin Studio)
I just started learning programming and I am currently trying out developing an android mobile application. 我刚刚开始学习编程,我正在尝试开发一个Android移动应用程序。 I have watched a lot of tutorials and trying to find solutions online, but i can't seem to get a hang of it. 我已经看了很多教程,并试图在网上找到解决方案,但我似乎无法掌握它。 So i am now reaching out to you guys for some help. 所以我现在正在向你们寻求帮助。
I am working in Xamarin Studio, C#, and I am trying to create a loginsystem with the help of a REST API, and i am not really sure how i save the accesstoken to a string so that i can use it in further requests. 我在Xamarin Studio,C#工作,我正在尝试使用REST API创建一个登录系统,我不确定如何将accesstoken保存到字符串,以便我可以在进一步的请求中使用它。
FILE: MainActivity.cs 文件:MainActivity.cs
namespace APItest
{
[Activity(Label = "APItest", MainLauncher = true)]
public class MainActivity : Activity
{
private WebClient mClient;
private Uri mUrl;
private List<string> mItems;
private ListView mListView;
` public string acesstoken;`
` protected override void OnCreate(Bundle savedInstanceState)`
` {`
` base.OnCreate(savedInstanceState);`
SetContentView(Resource.Layout.Main);
mListView = FindViewById<ListView>(Resource.Id.myListView);
` mClient = new WebClient();`
` mUrl = new Uri("http://...link.../login");`
` mClient.DownloadDataAsync(mUrl);`
mClient.Headers.Add("User", ”email@gmail.com");
mClient.Headers.Add("Pass", ”myPassword”;
mClient.Headers.Add("Content-Type", "application/json");
mClient.Headers.Add("Accept", "application/json");
accesstoken = mClient.Headers.Get(”Accesstoken").ToString();
` mItems = new List<string>();`
` mItems.Add(accesstoken);`
ArrayAdapter adapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleListItem1, mItems);
``
mListView.Adapter = adapter;
}
}
`}
In Postman API tool (I send username and password in headers and receive following text) 在Postman API工具中(我在标题中发送用户名和密码并接收以下文本)
{
"Result": {
”mRegister": {
"allowed": 0,
”Testmsg": "**"
},
` …`
"token": ”EB9TEBINlVOASM0Ok04RlIjI8JGMVNVV1smFu5MT"
}
}
I know i have a lot to learn, but i would really appreciate your help so i can get started. 我知道我有很多需要学习的东西,但我真的很感谢你的帮助,所以我可以开始了。
Thank you in advance! 先感谢您!
你有没有尝试过:
accesstoken = mClient.Headers.Get("token").ToString();
Getting token form json string. 获取令牌形式的json字符串。
Using Newtonsoft.Json library, create your response Json object. 使用Newtonsoft.Json库,创建响应Json对象。 And get the token string. 并获取令牌字符串。
Following example show how to create a User
json object to get the username and password: 下面的示例演示如何创建User
json对象以获取用户名和密码:
using Newtonsoft.Json;
public class MainActivity : Activity
{
protected override async void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
BlobCache.ApplicationName = "AkavacheText";
string json = @"{ 'Username': 'Mike','Password': 'Ma'}";
SetContentView(Resource.Layout.Main);
var getData = JsonConvert.DeserializeObject<User>(json);
System.Console.WriteLine(getData.Username+"---"+ getData.Password);
}
}
public class User
{
public string Username { get; set; }
public string Password { get; set; }
}
string json
should be your response string. string json
应该是你的响应字符串。 User
object should be your response object that structure depends on your json format. User
对象应该是您的响应对象,该结构取决于您的json格式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.