简体   繁体   中英

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. 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.

FILE: 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)


{
"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.

Using Newtonsoft.Json library, create your response Json object. And get the token string.

Following example show how to create a User json object to get the username and password:

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. User object should be your response object that structure depends on your json format.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM