0
我正在使用來自Unity網站的示例代碼爲WWW類發出API請求,但文本響應是垃圾。它看起來像 。當我記錄響應標題時,我得到了一個200響應,除了CONTENT-TYPE是image/jpeg以外,一切似乎都正常。我嘗試了幾個不同的隨機.json文件來測試它們,它們都返回相同的東西。請求圖像用作紋理確實有效。Unity WWW文本響應是亂碼
public class SpeechReq : MonoBehaviour {
//public string url = "https://gist.githubusercontent.com/wethecode/1f79baf168680afb0f2d/raw/755f9fb71dcc34df811b4bc26448d88a0f97f34d/snippets.json";
public string url = "https://gist.githubusercontent.com/damienh/fea91ab710475d499a09/raw/893065428badd8bfdc7b39fe17675b8aa031ac51/gistfile1.json";
IEnumerator Start()
{
WWW www = new WWW(url);
yield return www;
string respText = www.text;
Debug.Log(respText);
//Output: ����
byte[] resp = www.bytes;
var str = System.Text.Encoding.Default.GetString(resp);
Debug.Log(str);
//Output: ÿØÿà
if (www.responseHeaders.Count > 0)
{
foreach (KeyValuePair<string, string> entry in www.responseHeaders)
{
Debug.Log(entry.Value + "=" + entry.Key);
//Output: HTTP/1.0 200 OK=STATUS
//...
//image/jpeg=CONTENT-TYPE
}
}
}
}
謝謝,但一切似乎做的是消除。我現在得到一個而不是4個,但它實際上並不解析文本。如果我跳過前4個字節,這只是一個空的迴應。控制檯輸出是:不正確的JSON格式: – willjfield
@willjfield使用類似Wireshark或Fiddler的東西來精確地查看返回的內容......如果我將其粘貼到瀏覽器中,但可以看到該鏈接,比application/json。 –