Hello,
I’m trying to load a texture from the web onto a UI element and I get an error
"You are trying to load data from a www stream which had the following error when downloading.
Protocol “http not supported or disabled in libcurl”
I don’t see much on the web about this. Has anybody run into this issue before? Here is sample code I’m using (don’t mind the image - It’s a sample image from json who’s load is failing for me):
private string url="http://i.telegraph.co.uk/multimedia/archive/03387/President-Barack-O_3387037b.jpg";
private IEnumerator setImage() {
ASCIIEncoding asciiEncoding = new ASCIIEncoding();
byte[] bytes = asciiEncoding.GetBytes(url);
string clean = asciiEncoding.GetString(bytes);
WWW www = new WWW(clean);
yield return www;
RawImage ri = gameObject.GetComponent<RawImage>();
ri.texture = www.texture;
}
Any help would be greatly appreciated!
After copying and pasting it, I had absolutely no problems running this script. Copy the URL from your own post here and paste it back into the script to see if it has some sort of formatting that’s messing it up… Also, I used the fully-qualified name for the ASCIIEncoding class (System.Text.ASCIIEncoding) so I wouldn’t have to include the namespace it’s in, so try that?
Hmm, you’re right! the script works when I hardcode the URL. I should have tried that before posting. In my project the url is parsed from json. I thought it may be the order in which I’m executing but I put a waitforseconds at the top of the script and I get the same error. Anything else I might be missing?
As I said, the code you posted works fine. That being the case, the error is from something you haven’t posted. You just said that the url is parsed from json- you need to escape the string so that an extra ’ or " in there doesn’t interfere with processing that string, maybe? If you want further help for this, you really need to post the code that’s giving you a problem, which is not what’s up here.
1 Like
Sure thing… Here’s a little test I made that is failing for me. i’m using jsonObject from the asset store
(https://kharma.unity3d.com/en/#!/content/710). Thanks for your help!
// add this sript to Canvas element with RawImage
// using jsonObject plugin from https://kharma.unity3d.com/en/#!/content/710
using UnityEngine;
using System.Collections;
using System.Net;
using System.Text;
using UnityEngine.UI;
public class JSONTest : MonoBehaviour {
public string search = "barack obama";
private string url = "https://ajax.googleapis.com/ajax/services/search/news?v=1.0&rsz=8&q=";
private string imgUrl;
void Start () {
Search();
}
private void Search() {
GetData(Utils.escape(search));
}
void GetData(string search) {
string searchURL = url + search;
Debug.Log(searchURL);
WWW www = new WWW(url + search);
StartCoroutine(WaitForRequest(www));
}
IEnumerator WaitForRequest(WWW www) {
while(!www.isDone);
yield return www;
if(www.error == null) {
Debug.Log("REQUEST SUCCESS!");
// JSONObject j = JSONObject.Create(WWW.UnEscapeURL(www.text));
JSONObject j = JSONObject.Create(www.text);
Debug.Log(j);
ParseData(j);
} else {
Debug.Log("REQUEST ERROR!");
Debug.Log(www.error);
}
}
void ParseData(JSONObject o) {
imgUrl = o["responseData"]["results"][0]["image"]["url"].ToString();
StartCoroutine(setImage ());
}
private IEnumerator setImage() {
// yield return new WaitForSeconds(3);
ASCIIEncoding asciiEncoding = new ASCIIEncoding();
byte[] bytes = asciiEncoding.GetBytes(imgUrl);
string clean = asciiEncoding.GetString(bytes);
WWW www = new WWW(clean);
yield return www;
RawImage ri = GameObject.Find(gameObject.name + "/RawImage").GetComponent<RawImage>();
ri.texture = www.texture;
}
}
Oops i forgot a little helper class
using UnityEngine;
using System.Collections;
using System.Text;
using System;
public class Utils : MonoBehaviour {
public static string escape(object @string)
{
string str = Convert.ToString(@string);
string str2 = "0123456789ABCDEF";
int length = str.Length;
StringBuilder builder = new StringBuilder(length * 2);
int num3 = -1;
while (++num3 < length)
{
char ch = str[num3];
int num2 = ch;
if ((((0x41 > num2) || (num2 > 90)) &&
((0x61 > num2) || (num2 > 0x7a))) &&
((0x30 > num2) || (num2 > 0x39)))
{
switch (ch)
{
case '@':
case '*':
case '_':
case '+':
case '-':
case '.':
case '/':
goto Label_0125;
}
builder.Append('%');
if (num2 < 0x100)
{
builder.Append(str2[num2 / 0x10]);
ch = str2[num2 % 0x10];
}
else
{
builder.Append('u');
builder.Append(str2[(num2 >> 12) % 0x10]);
builder.Append(str2[(num2 >> 8) % 0x10]);
builder.Append(str2[(num2 >> 4) % 0x10]);
ch = str2[num2 % 0x10];
}
}
Label_0125:
builder.Append(ch);
}
return builder.ToString();
}
}
One of the updates apparently has to do with escaped quotes, which would explain this problem. I’m not sure if they updated the scripts on the AssetStore or if you got the updated version if they did, but here’s the link:
http://wiki.unity3d.com/index.php?title=JSONObject
1 Like
Wow! I didn’t even notice those quotes! It makes so much sense now since the full error is:
You are trying to load data from a www stream which had the following error when downloading.
Protocol "http not supported or disabled in libcurl
I thought it just missed the close quotes somewhere.
I tested by replacing the ParseData function with
voidParseData(JSONObjecto) {
stringtempURL = o["responseData"]["results"][0]["image"]["url"].ToString();
string[] temp = tempURL.Split('\"');
imgUrl = temp[1];
StartCoroutine(setImage ());
}
The quotes are removed from the URL and everything seems to be working fine. Thanks for your help Lysander!!
…im sure theres a better way to remove those quotes - quick and dirty version for now!