Set display picture of StoredContact from byte[] - windows-phone-8

I have a byte[] containing picture data. I want to set this picture data as a display picture of a StoredContact in windows phone 8. I have tried:
byte[] data = ...
ContactStore store = await ContactStore.CreateOrOpenAsync(ContactStoreSystemAccessMode.ReadWrite, ContactStoreApplicationAccessMode.ReadOnly);
StoredContact contact = await store.FindContactByRemoteIdAsync(remoteId);
using (IInputStream stream = new MemoryStream(data).AsInputStream())
{
await contact.SetDisplayPictureAsync(stream);
}
but i get System.UnauthorizedAccessException on the AsInputStream()-call. I have the ID_CAP_CONTACTS-capability set and the contact belongs to a custom data store, to which i have write access. Anyone know how to solve this?
EDIT:
I get the data from a web request, using the following to extract the byte data:
DataContractJsonSerializer serializer = new DataContractJsonSerializer(new PhotoObject().GetType());
var result = (PhotoObject)serializer.ReadObject(stream);
byte[] data = Convert.FromBase64String(result.Data);
[DataContract]
class PhotoObject
{
[DataMember(Name = "size")]
public string Size { get; set; }
[DataMember(Name = "data")]
public string Data { get; set; }
}

I finally managed to get around this problem. The first thing I did was to create the stream using:
new MemoryStream(data, 0, data.Length, true, true).AsInputStream();
instead of
new MemoryStream(data).AsInputStream();
This got rid of the UnauthorizedException, however it gave me an ArgumentException: "Value does not fall within the expected range" on the SetDisplayPictureAsync-call instead. I still do not know why it works, but i got around this by writing the data to temporary file, which I then open and pass to SetDisplayPictureAsync. This is my code for doing this:
//Write bytes to file then open file and pass stream to setdisplaypicture.
StorageFolder localFolder = ApplicationData.Current.LocalFolder;
var folder = await localFolder.CreateFolderAsync("Photo_Temp_Folder", CreationCollisionOption.OpenIfExists);
var file = await folder.CreateFileAsync("photo_" + contact.Id, CreationCollisionOption.ReplaceExisting);
uint written = 0;
using (var writeStream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
while (written < data.Length)
{
written += await writeStream.WriteAsync(data.AsBuffer());
}
}
using (var accessStream = await file.OpenAsync(FileAccessMode.Read))
{
await contact.SetDisplayPictureAsync(accessStream);
await contact.SaveAsync();
}
await file.DeleteAsync();
Hope it helps someone.

Related

How to Consume an API in ASP.NET Core?

I need to consume an API in my MVC project. the actions in API are secured, So you need to access a token (JWT) to consume it. I face an error every time I try to deserialize the response into the model (Player). It says *Could not cast or convert from System.String to MyMVC.Models.Player*
When I run a debugger, the piece of code including deserialization is in red in the internal server error page.
Here is the action in API
[HttpGet]
[Authorize]
public ActionResult<List<Player>> GetAllPlayers()
{
var players = _applicationDbContext.Players.OrderBy(p => p.Name).Select(p=> p.Name).ToList();
return Ok(players);
}
This is the action in the MVC project
public async Task<IActionResult> GetPlayers()
{
var token = HttpContext.Session.GetString("Token");
List<Player> players = new List<Player>();
var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost:53217/api/player");
var client = _clientFactory.CreateClient();
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
HttpResponseMessage response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
if(response.StatusCode == System.Net.HttpStatusCode.OK)
{
var strResponse = await response.Content.ReadAsStringAsync();
players = JsonConvert.DeserializeObject<List<Player>>(strResponse);
}
return View(players);
}
Sami Kuhmonen 's comment is right.
var players = _applicationDbContext.Players.OrderBy(p => p.Name).Select(p=> p.Name).ToList();
From here we can get name list not player list.
Name list contain string name. Player list contain object player1 {name="xx",age="xx"}
But
List<Player> players = new List<Player>();
var strResponse = await response.Content.ReadAsStringAsync();
players = JsonConvert.DeserializeObject<List<Player>>(strResponse);
here we need player list contain object players .
You can use below code in your API to get the playerlist.
var players = _applicationDbContext.Players.ToList();
I reproduce your problem. Then I use that method to solve it.
Update
Create a new class User contain the property that you want.
public class User
{
public int Id { get; set; }
public string Name { get; set; }
}
Then use the below code
var players = _applicationDbContext.Players.Select (P=>new User { Name=P.Name, Id=P.Id} ).ToList();
In mvc change List<Player> players = new List<Player>(); to
List<User> players = new List<User>();
Result:

Why are PostAsJsonAsync's parameters not included in the post as JSON content?

IT is very disapointing that after one week i cannot solve a simple problem of posting a JSON content to a Web Server's API. I think I will quit this attempt to use Xamarin.
I am trying to post the JSON parameters below using PostAsJsonAsync in a Xamarin app. The program does post the site but the parameters are not encoded as JSON content. Does anyone know why?
public async void Login()
{
var formcontent = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string,string>("email","marcio#netopen.com.br"),
new KeyValuePair<string, string>("password","Xy345")
});
var FlyVIPAPI = new HttpClient();
var request = await FlyVIPAPI.PostAsJsonAsync("http://www.ik.com.br/app/api/LoginUser.php", formcontent);
var response = await request.Content.ReadAsStringAsync();
//var res = JsonConvert.DeserializeObject(response);
var RepostaJson = Newtonsoft.Json.Linq.JObject.Parse(response);
System.Diagnostics.Debug.WriteLine(RepostaJson["success"]);
System.Diagnostics.Debug.WriteLine(RepostaJson["error"]);
return;
}
public class LoginRequest
{
public string email { get; set; }
public string password { get; set; }
}
public async void Login()
{
using (var FlyVIPAPI = new HttpClient())
{
// Create Request object
var requestObj = new LoginRequest { email = "marcio#netopen.com.br", password = "Xy345" };
// Serialize to JSON string
var formcontent = JsonConvert.SerializeObject(requestObj);
// Create HTTP content
var content = new StringContent(formcontent, Encoding.UTF8, "application/json");
// POST Request
var request = await FlyVIPAPI.PostAsync("http://www.ik.com.br/app/api/LoginUser.php", content);
// Read Response
var response = await request.Content.ReadAsStringAsync();
....
}
}
Additionally, I would suggest wrapping your HttpClient in a using statement so that is will be disposed of once your code block is done. Freeing up resources.

trying to access an external JSON file using netwonsoft in mvc4 c#

I'm trying to read an external json to display data on screen. What am I doing worng here?
public void QuarterlyReport(object sender, EventArgs e)
{
JObject qData1 = JObject.Parse(System.IO.File.ReadAllText(#"~/json/quarterlyData.json"));
// read JSON directly from a file
using (StreamReader file = System.IO.File.OpenText(#"~/json/quarterlyData.json"))
using (JsonTextReader reader = new JsonTextReader(file))
{
JObject Qdata2 = (JObject) JToken.ReadFrom(reader);
}
string Qdata = Newtonsoft.Json.JsonConvert.SerializeObject(qData1);
}
public async Task<FileStreamResult> Index()
{
var _reportingService = new ReportingService("https://mysite.jsreportonline.net", "myemail#gmail.com", "password");
var report = await _reportingService.RenderAsync("VyxOYwH7Ze", new { Qdata });
//add the stream to be used by browser
MemoryStream ms = new MemoryStream();
//copy whatever JS is sending to us
report.Content.CopyTo(ms);
//start at content point
ms.Position = 0;
//send this to browser
return File(ms, report.ContentType.MediaType);
}
I can't seem to get the vaule into the variable Qdata. What is it that I am doing wrong in the method?
The line where you declare Qdata:
string Qdata = Newtonsoft.Json.JsonConvert.SerializeObject(qData1);
is not in the same scope as this line:
var report = await _reportingService.RenderAsync("VyxOYwH7Ze", new { Qdata });
Yes, the problem was that
JObject qData1 = JObject.Parse(System.IO.File.ReadAllText(#"~/json/quarterlyData.json"));
And
string Qdata = Newtonsoft.Json.JsonConvert.SerializeObject(qData1);
Needed to be in the same scope as
var report = await _reportingService.RenderAsync("VyxOYwH7Ze", new { Qdata });

Windows phone can't save CookieContainer

I apologize for my english :)
I catch cookies from server. And try to save it in order to use cookeis later.
var Settings = IsolatedStorageSettings.ApplicationSettings;
CookieContainer _Cookie = new CookieContainer()
_Cookie.Add(new Uri("http://www.portal.fa.ru/Job/SearchResultDiv"), response.Cookies);
Settings.Clear();
Settings["UserID"] = userID;
Settings["Cookie"] = _Cookie;
Settings.Save();
Ok it working. But after restart app cookie has losted. (Object has remain but cookies count = 0). I don't know.
So i try to convert from CookieContainer to array byte than save and load it when i need it.
public static byte[] ToByte(CookieContainer data)
{
byte[] CookieByte;
DataContractSerializer serializer = new DataContractSerializer(typeof(CookieContainer));
using (var memoryStream = new MemoryStream())
{
serializer.WriteObject(memoryStream, data);
CookieByte = memoryStream.ToArray();
}
return CookieByte;
}
public static CookieContainer FromByte(byte[] data)
{
CookieContainer Cookie;
DataContractSerializer serializer = new DataContractSerializer(typeof(CookieContainer));
using (var memoryStream = new MemoryStream(data))
{
Cookie = (CookieContainer)serializer.ReadObject(memoryStream);
}
return Cookie;
}
But this code did not work again. When i convert to byte and back i losing cookies (count = 0).
So what can i do?
PS write pls your code when you deal with authorization and cookies. Thx

Windows Phone 8 append to JSON file

I'm working on a Windows Phone 8 app.
I'm having issue appending to my JSON file.
It works fine if I keep the app open but once I close it and come back in it starts back writing from the beginning of the file.
Relevant code:
private async void btnSave_Click(object sender, RoutedEventArgs e)
{
// Create a entry and intialize some values from textbox...
GasInfoEntries _entry = null;
_entry = new GasInfoEntries();
_entry.Gallons = TxtBoxGas.Text;
_entry.Price = TxtBoxPrice.Text;
_GasList.Add(_entry);
//TxtBlockPricePerGallon.Text = (double.Parse(TxtBoxGas.Text) / double.Parse(TxtBoxPrice.Text)).ToString();
// Serialize our Product class into a string
string jsonContents = JsonConvert.SerializeObject(_GasList);
// Get the app data folder and create or open the file we are storing the JSON in.
StorageFolder localFolder = ApplicationData.Current.LocalFolder;
StorageFile textfile = await localFolder.CreateFileAsync("gasinfo.json", CreationCollisionOption.OpenIfExists); //if get await operator error add async to class (btnsave)
//open file
using (IRandomAccessStream textstream = await textfile.OpenAsync(FileAccessMode.ReadWrite))
{
//write JSON string
using (DataWriter textwriter = new DataWriter(textstream))
//using (DataWriter textwriter = new DataWriter(textstream))
{
textwriter.WriteString(jsonContents);
await textwriter.StoreAsync(); //writes buffer to store
}
}
}
private async void btnShow_Click(object sender, RoutedEventArgs e)
{
StorageFolder localfolder = ApplicationData.Current.LocalFolder;
try
{
// Getting JSON from file if it exists, or file not found exception if it does not
StorageFile textfile = await localfolder.GetFileAsync("gasinfo.json");
using (IRandomAccessStream textstream = await textfile.OpenReadAsync())
{
//read text stream
using (DataReader textreader = new DataReader(textstream))
{
//get size ...not sure what for think check the file size (lenght) then based on next 2 commands waits until its all read
uint textlength = (uint)textstream.Size;
await textreader.LoadAsync(textlength);
//read it
string jsonContents = textreader.ReadString(textlength);
// deserialize back to gas info
_GasList = JsonConvert.DeserializeObject<List<GasInfoEntries>>(jsonContents) as List<GasInfoEntries>;
displayGasInfoEntries();
}
}
}
catch
{
txtShow.Text = "something went wrong";
}
}
private void displayGasInfoEntries()
{
txtShow.Text = "";
StringBuilder GasString = new StringBuilder();
foreach (GasInfoEntries _entry in _GasList)
{
GasString.AppendFormat("Gallons: {0} \r\n Price: ${1} \r\n", _entry.Gallons, _entry.Price); // i think /r/n means Return and New line...{0} and {1} calls "variables" in json file
}
txtShow.Text = GasString.ToString();
}
Thanks
Do you call the btnShow_Click each time you've started the app? Because otherwise the _GasList will be empty; if you now call the btnSave_Click all previous made changes will be lost.
So please make sure, that you restore the previously saved json data before you add items to the _GasList.