【问题标题】:UniMag card reader API won't accept xml config file during setup on XamarinUniMag 读卡器 API 在 Xamarin 上设置期间不接受 xml 配置文件
【发布时间】:2019-05-01 19:31:08
【问题描述】:

我正在 Xamarin Android 上实现 UniMag 信用卡滑块 API,以便我的应用可以读取信用卡。该 API 仅适用于原生 Android,因此我按照说明 here 创建了一个围绕 API 的 .jar 文件的包装器。这很好用而且超级简单。

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

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

用于此的 API 调用是;

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 和 Root 目录中,并尝试了几种向 API 提供 URL 的方法。

这是演示应用程序建议的方式:

  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;
    }

这是我尝试的另一种方法:

var file = new Java.IO.File(Android.Net.Uri.Parse("file:///default_config.xml").ToString());
var uri = file.AbsolutePath;

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

 [UMSDK] SDK: headset attached
 [UMSDK] SDK: reader attached, but no config loaded

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

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

[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)

【问题讨论】:

    标签: android xml xamarin jar unimag


    【解决方案1】:

    该示例将文件放置在 Assets 或 Raw 文件夹中。要使用 Assets 或 Raw 文件夹中的文件,您必须获取一个流,然后将该流写入应用程序本身外部可访问的文件(添加到 IDE 中应用程序项目的任何文件最终都打包在APK 本身,因此 APK 本身之外的任何东西都无法访问)。您发送的 Java 示例就是这样做的(从您帖子中的示例复制,但缩写为仅显示特定行):

    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);
                ...
            }
        }
        ...
    }
    

    所以先设置文件名和路径变量:

    string filename = "default_config.xml";
    string directory = Android.App.Application.Context.FilesDir.AbsolutePath;
    string fullPath = Path.Combine(directory, filename);
    string xmlFileContents;
    

    然后获取一个流。

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

    using (StreamReader sr = new StreamReader(Assets.Open(filename)))
    

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

    using (StreamReader sr = new StreamReader(Resources.OpenRawResource(Resource.Raw.default_config))
    

    然后执行以下操作:

    {
        xmlFileContents = sr.ReadToEnd();
        if (File.Exists(fullPath)) {
            File.Delete(fullPath);
        }
        File.WriteAllText(fullPath, xmlFileContents);
    }
    

    然后您可以将fullPath 传递给API:

    reader.SetXMLFileNameWithPath(fullPath);
    

    【讨论】:

    • 谢谢你。我找到了类似的解决方案,但使用了 BinaryReader 和 BinaryWriter。您的解决方案要好得多! xml 文件被创建并提供给 API。不幸的是,API 仍然没有说没有加载配置文件。我想另一个帖子的另一个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-18
    • 1970-01-01
    • 1970-01-01
    • 2021-10-27
    • 2023-04-01
    • 1970-01-01
    相关资源
    最近更新 更多