2016-03-06 73 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