2016-03-06 166 views
0

我使用Kodi API,通过asp.net控制我的htpc。特别是名为“Playlist.Add”的功能。 JSON的我送是这样的:Kodi中的非英文字符API

{"jsonrpc":"2.0","method":"Playlist.Insert","params":{"playlistid":0,"position":0,"item":{"file":"smb://server/Ferry Corsten/Beautiful/Ferry Corsten - Beautiful (Extended).mp3"}},"id":1} 

这是工作的罚款。但是当这样的字符串中有一些没有英文字符时:

{"jsonrpc":"2.0","method":"Playlist.Insert","params":{"playlistid":0,"position":0,"item":{"file":"smb://server/01-Zum Geburtstag viel Glück.mp3"}},"id":1} 

它只是抛出一个“RequestCanceled”异常。

我的C#源代码是这样的:

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(_url); 
       string authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(_username + ":" + _password)); 
       webRequest.Headers["Authorization"] = "Basic " + authInfo; 

       webRequest.Method = "POST"; 
       webRequest.UserAgent = "KodiControl"; 
       webRequest.ContentType = "application/json"; 

       webRequest.ContentLength = json.Length; 
       using (var streamWriter = new StreamWriter(webRequest.GetRequestStream())) 
       { 
        streamWriter.Write(json); 
        streamWriter.Flush(); 
        streamWriter.Close(); 
       } 

唯一的例外是在streamWriter.Flush()抛出。 所以我有什么做了这个要求?``

+0

只是一个猜测:也许你必须用UTF-8编码请求。 – andpei

回答

0

我建议你看看Kodi addon unicode paths 此之后指南将帮助你防止与科迪非拉丁字符的共同问题。

Python只能在内部使用unicode字符串,转换为输出中的特定编码。 (或输入)”。为了使字符串文字的unicode默认情况下,添加

from __future__ import unicode_literals 

附加组件路径

path = addon.getAddonInfo('path').decode('utf-8') 

.decode('utf-8')告诉科迪使用utf-8解码给定函数。 科迪的getAddonInfo返回UTF -8编码的字符串,我们将其解码为一个unicode。

浏览对话框

dialog = xbmcgui.Dialog() 
directory = dialog.browse(0, 'Title' , 'pictures').decode('utf-8') 

dialog.browse()返回一个可能包含一些非拉丁字符的UTF-8编码字符串。因此解码为unicode!

+0

而不是仅仅链接到一个指南,或许你可以在这个帖子中提供一些具体的信息,以防链接中断和未来的用户需要帮助。 – Fencer04