首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UniMag卡读取器API在Xamarin上安装过程中不接受xml配置文件

UniMag卡读取器API在Xamarin上安装过程中不接受xml配置文件
EN

Stack Overflow用户
提问于 2018-11-29 16:58:52
回答 1查看 101关注 0票数 0

我正在实现UniMag信用卡滑块API在Xamarin Android上,这样我的应用程序就可以读取信用卡。这个API只适用于原生Android,所以我按照这里的说明来创建API的.jar文件的包装器。它的工作原理很棒,而且非常简单。

在连接和断开滑块设备时,我可以初始化api并获得侦听器回调。由于并非所有的Android设备都受API支持,所以UniMagReader需要读取提供的配置xml文件。

我遇到的问题是无法让API读取文件。

用于此的API调用是;

代码语言:javascript
复制
var reader = new UniMagReader(new UniMagMessage(), Android.App.Application.Context);
reader.SetSaveLogEnable(false);

reader.SetVerboseLoggingEnable(true);
reader.RegisterListen();

string fileNameWithPath = GetConfigurationFileFromRaw();
reader.SetXMLFileNameWithPath(fileNameWithPath);
reader.LoadingConfigurationXMLFile(false);

我尝试过将文件放在Resources.Raw、Resources.Assets和根目录中,并尝试了一些向API提供URL的方法。

演示应用程序就是这样表示的:

代码语言:javascript
复制
  private string GetXMLFileFromRaw(string fileName)
    {
        //the target filename in the application path
        string fileNameWithPath = null;
        fileNameWithPath = fileName;

        try
        {
            //using (var inStream = Android.App.Application.Context.Assets.Open("default_config.xml")) // assest file access
            using (var inStream = Android.App.Application.Context.Resources.OpenRawResource(Resource.Raw.default_config)) // raw file access
            {
                var length = GetStreamLength(inStream);
                byte[] buffer = new byte[length];
                inStream.Read(buffer, 0, (int)length);
                inStream.Close();
                Android.App.Application.Context.DeleteFile(fileNameWithPath);
                var fout = Android.App.Application.Context.OpenFileOutput(fileNameWithPath, Android.Content.FileCreationMode.Private);
                fout.Write(buffer, 0, (int)length);
                fout.Close();

                // to refer to the application path
                var fileDir = Android.App.Application.Context.FilesDir;
                fileNameWithPath = fileDir.Parent + Path.DirectorySeparatorChar + fileDir.Name;
                fileNameWithPath += Path.DirectorySeparatorChar + "default_config.xml";
            }
        }
        catch (System.Exception e)
        {
            fileNameWithPath = null;
        }
        return fileNameWithPath;
    }

    // Return the length of a stream that does not have a usable Length property
    public static long GetStreamLength(Stream stream)
    {
        long originalPosition = 0;
        long totalBytesRead = 0;

        if (stream.CanSeek)
        {
            originalPosition = stream.Position;
            stream.Position = 0;
        }

        try
        {
            byte[] readBuffer = new byte[4096];

            int bytesRead;

            while ((bytesRead = stream.Read(readBuffer, 0, 4096)) > 0)
            {
                totalBytesRead += bytesRead;
            }
        }
        finally
        {
            if (stream.CanSeek)
            {
                stream.Position = originalPosition;
            }
        }

        return totalBytesRead;
    }

这是我尝试过的另一种方式:

代码语言:javascript
复制
var file = new Java.IO.File(Android.Net.Uri.Parse("file:///default_config.xml").ToString());
var uri = file.AbsolutePath;

我得到的错误来自于API。当将刷卡连接到设备时,我看到:

代码语言:javascript
复制
 [UMSDK] SDK: headset attached
 [UMSDK] SDK: reader attached, but no config loaded

从OnReceiveMsgFailureInfo回调中,我看到“XML文件不存在,自动更新被禁用”。

在应用程序输出中,我看到:

代码语言:javascript
复制
[UMSDK] UmXmlParser: parsing XML failed due to exception
[UMSDK] org.apache.harmony.xml.ExpatParser$ParseException: At line 1, column 0: not well-formed (invalid token)
[UMSDK]     at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:519)
[UMSDK]     at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:478)
[UMSDK]     at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:316)
[UMSDK]     at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:279)
[UMSDK]     at com.idtechproducts.acom.AcomXmlParser.parseFile(AcomXmlParser.java:91)
[UMSDK]     at com.idtechproducts.unimagsdk.UniMagConfigHelper.loadingXMLFile(UniMagConfigHelper.java:116)
[UMSDK]     at com.idtechproducts.unimagsdk.UniMagConfigHelper.loadingXMLFile(UniMagConfigHelper.java:46)
[UMSDK]     at IDTech.MSR.uniMag.uniMagReader.loadingConfigurationXMLFile(uniMagReader.java:496)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-11-30 01:06:05

该示例将文件放置在“资产”或“原始”文件夹中。要使用资产或原始文件夹中的文件,您必须获得一个流,然后将该流写入应用程序本身之外可访问的文件(添加到IDE中的应用程序项目的任何文件最终都打包在APK本身中,因此APK本身之外的任何东西都无法访问该文件)。您发送的Java示例确实做到了这一点(从文章中的示例复制,但缩写为只显示特定行):

代码语言:javascript
复制
private string GetXMLFileFromRaw(string fileName)
{
...
    try
    {
        // Gets the stream from the raw resource
        using (var inStream = Android.App.Application.Context.Resources.OpenRawResource(Resource.Raw.default_config)) // raw file access
        {
            // Reads the stream into a buffer
            ...
            inStream.Read(buffer, 0, (int)length);
            ...
            // Deletes any existing file
            Android.App.Application.Context.DeleteFile(fileNameWithPath);
            // Writes the stream to a file
            ...
            fout.Write(buffer, 0, (int)length);
            ...
        }
    }
    ...
}

因此,首先设置文件名和路径变量:

代码语言:javascript
复制
string filename = "default_config.xml";
string directory = Android.App.Application.Context.FilesDir.AbsolutePath;
string fullPath = Path.Combine(directory, filename);
string xmlFileContents;

那就找条小溪。

如果资产中的default_config.xml文件:

代码语言:javascript
复制
using (StreamReader sr = new StreamReader(Assets.Open(filename)))

如果原始资源中的default_config.xml:

代码语言:javascript
复制
using (StreamReader sr = new StreamReader(Resources.OpenRawResource(Resource.Raw.default_config))

然后执行以下操作:

代码语言:javascript
复制
{
    xmlFileContents = sr.ReadToEnd();
    if (File.Exists(fullPath)) {
        File.Delete(fullPath);
    }
    File.WriteAllText(fullPath, xmlFileContents);
}

然后可以将fullPath传递给API:

代码语言:javascript
复制
reader.SetXMLFileNameWithPath(fullPath);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53544051

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档