从asp.net网站API返回的图像zebble
问题描述:
我试图从网上API返回的图像zebble这样的:从asp.net网站API返回的图像zebble
网页API:
public HttpResponseMessage GetImage()
{
var memoryStream = custom logic to create image
var result = new HttpResponseMessage(HttpStatusCode.OK);
result.Content = new ByteArrayContent(memoryStream.ToArray());
result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/png");
return result;
}
zebble:
var imageData = await Get<byte[]>(url);
var imageView = new ImageView { ImageData = imageData }
但Get
抛出异常:
Exception thrown: 'Newtonsoft.Json.JsonReaderException' in Newtonsoft.Json.dll
Exception thrown: 'Newtonsoft.Json.JsonReaderException' in Newtonsoft.Json.dll
Exception thrown: 'System.Exception' in Zebble.UWP.dll
Exception thrown: 'System.Exception' in mscorlib.ni.dll
ERROR: HttpGet -> 'url' failed.
WARNING: Failed to convert API response to Byte[]
###############################################
Base issue: Unexpected character encountered while parsing value: �. Path '', line 1, position 1.
--------------------------------------
STACK TRACE:
at Zebble.Framework.BaseApi.<GetFromTheServer>d__24`1.MoveNext()
at Zebble.Framework.BaseApi.<Get>d__25`1.MoveNext()
任何想法如何解决这个问题?
答
您的WebApi必须返回字符串。您可以使用Base64字符串来传输图像数据。
第1步:更改Web API代码:
var memoryStream = new MemoryStream(); //TODO: custom logic to create image
image.Save(memoryStream, ImageFormat.Png);
return Ok(Convert.ToBase64String(memoryStream.ToArray()));
更多细节:http://zebble.net/docs/get-apis
第2步:在你Zebble代码,使用Zebble的WebAPI代理打电话给你的WebAPI返回Base64字符串,转换为byte []并设置为ImageView的源代码:
var base64 = await Api.Get<string>(url);
var imageData = Convert.FromBase64String(base64);
myImageView.ImageData = imageData;
更多详细信息:http://zebble.net/docs/calling-a-get-api-in-the-mobile-app