Issue
I am facing a weird error in my Android Xamarin Forms app. Unable to find the root cause of it. Following is my code which gets the API response and deserializes it to an object.
public async static Task<T2> ExecuteRequest<T1, T2>(T1 t1, EndPoint endPoint, bool getParams = false, HttpVerb method = HttpVerb.POST)
{
string resStr = "";
try
{
object data = t1;
if (endPoint.ToString() != EndPoints.Login.ToString() && getParams == false)
data = Newtonsoft.Json.JsonConvert.SerializeObject(t1);
HttpUtils.RestClient restClient = new HttpUtils.RestClient(APIPath + endPoint, HttpVerb.POST, data.ToString());
restClient.ContentType = "application/json";
restClient.Method = method;
resStr = await (endPoint != EndPoints.Login && getParams == false ? restClient.MakeRequest() : restClient.MakeRequest(data.ToString()));
if (string.IsNullOrEmpty(resStr))
return default(T2);
return Newtonsoft.Json.JsonConvert.DeserializeObject<T2>(resStr);
}
catch (Newtonsoft.Json.JsonSerializationException e)
{
Device.BeginInvokeOnMainThread(async () => await Application.Current.MainPage.DisplayAlert("Error", e.Message + "\n----------\n" + resStr, "OK"));
return default(T2);
}
catch (System.Net.WebException)
{
Device.BeginInvokeOnMainThread(async () => await Application.Current.MainPage.DisplayAlert("Not Connected", "Please check your Internet connection and restart the app.", "OK"));
return default(T2);
}
}
And following is how I am trying to login using above method to the backend system:
Services.API.ResponseModels.Login res = await APIAccess.ExecuteRequest<string, Services.API.ResponseModels.Login>(req, APIAccess.EndPoints.Login);
Object Class:
public class Login
{
public string access_token { get; set; }
public string token_type { get; set; }
public int expires_in { get; set; }
public string userName { get; set; }
public string issued { get; set; }
public string expires { get; set; }
}
I am receiving a response correctly in following JSON format:
{
"access_token": "<access token received here>",
"token_type": "bearer",
"expires_in": 2591999,
"userName": "<userName received here>",
".issued": "Sun, 16 Jan 2022 09:41:06 GMT",
".expires": "Tue, 15 Feb 2022 09:41:06 GMT"
}
But when in Debug mode NewtonSoft deserializes it correctly and takes user to another screen and when in Release mode it shows following error:
I tried adding a default constructor normally and with [JsonConstructor]
attribute but it's not working. I cleaned and removed Bin and Obj on all projects and also updated NewtonSoft JSON package. Nothing seems to work in Release mode.
Solution
You can add a PreserveAttribute
to your class library:
public sealed class PreserveAttribute : Attribute
{
public bool AllMembers;
public bool Conditional;
public PreserveAttribute (bool allMembers, bool conditional)
{
AllMembers = allMembers;
Conditional = conditional;
}
public PreserveAttribute ()
{
}
}
And then use that attribute on your JSON model/class:
[Preserve(AllMembers = true)]
public class Login
{
public string access_token { get; set; }
public string token_type { get; set; }
public int expires_in { get; set; }
public string userName { get; set; }
public string issued { get; set; }
public string expires { get; set; }
}
Answered By - Rahul Sharma
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.