Basically I get
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object occurred
when I try to get the token from the restservice.
when it gets to
public async Task<T> PostResponseLogin<T>(string weburl, FormUrlEncodedContent content) where T : class
{
var response = await client.PostAsync(weburl, content);
var jsonResult = response.Content.ReadAsStringAsync().Result;
var responseObject = JsonConvert.DeserializeObject<T>(jsonResult);
return responseObject;
}
on the return respondObject; it goes to the appDelegate and throws the exception.
-I'm currently in the learning process of C#/Xamarin, so if I have made a simple mistake that you notice, that will be why. Thanks for your help.
I've added MainPage = new NavigationPage(new LoginPage()); so I can navigate between pages, that is working.
edit: It was suggested that this might be a possible duplicate of another ticket with an unhandled exception. While it did help me to understand that error better, it's still more specific than that.
public class RestService
{
HttpClient client;
string grant_type = "password";
public RestService()
{
client = new HttpClient();
client.MaxResponseContentBufferSize = 256000;
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded' "));
}
public async Task<Token> Login(User user)
{
var postData = new List<KeyValuePair<string, string>>();
postData.Add(new KeyValuePair<string, string>("grant_type", grant_type));
postData.Add(new KeyValuePair<string, string>("Email", user.Email));
postData.Add(new KeyValuePair<string, string>("Password", user.Password));
var content = new FormUrlEncodedContent(postData);
var weburl = "https://blahblah.com/auth/login";
var response = await PostResponseLogin<Token>(weburl, content);
DateTime dt = new DateTime();
dt = DateTime.Today;
response.expireDate = dt.AddSeconds(response.expireIn);
return response;
}
public async Task<T> PostResponseLogin<T>(string weburl, FormUrlEncodedContent content) where T : class
{
var response = await client.PostAsync(weburl, content);
var jsonResult = response.Content.ReadAsStringAsync().Result;
var responseObject = JsonConvert.DeserializeObject<T>(jsonResult);
return responseObject;
}
public async Task<T> PostResponse<T>(string weburl, string jsonstring) where T : class
{
var Token = App.TokenDatabase.GetToken();
string ContentType = "application/json";
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", Token.accessToken);
try
{
var Result = await client.PostAsync(weburl, new StringContent(jsonstring, Encoding.UTF8, ContentType));
if (Result.StatusCode == System.Net.HttpStatusCode.OK)
{
var JsonResult = Result.Content.ReadAsStringAsync().Result;
try
{
var ContentResp = JsonConvert.DeserializeObject<T>(JsonResult);
return ContentResp;
}
catch { return null; }
}
}
catch { return null; }
return null;
}
public async Task<T> GetResponse<T>(string weburl) where T : class
{
var Token = App.TokenDatabase.GetToken();
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", Token.accessToken);
try
{
var response = await client.GetAsync(weburl);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
var JsonResult = response.Content.ReadAsStringAsync().Result;
try
{
var ContentResp = JsonConvert.DeserializeObject<T>(JsonResult);
return ContentResp;
}
catch
{
return null;
}
}
}
catch
{
return null;
}
return null;
}
}