Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

API Help

Discussion in 'Scripting' started by FuzzyBalls, Jun 29, 2014.

  1. FuzzyBalls

    FuzzyBalls

    Joined:
    Jun 12, 2014
    Posts:
    47
    Hey,

    I'm playing around with API's to learn some new things, but I can't figure this out.

    I'm trying to call the SharedCount API: https://www.sharedcount.com which seems really simple, but I''m failing miserably..

    It's a web based API so it's examples are catered to web development, rather than Unity of course.

    But here's the example for jQuery:

    Code (Boo):
    1. jQuery.sharedCount = function(url, fn) {
    2.     url = encodeURIComponent(url || location.href);
    3.     var domain = "//sample.sharedcount.com/"; /* SET DOMAIN */
    4.     var apikey = "" /*API KEY HERE*/
    5.     var arg = {
    6.         data: {
    7.             url : url,
    8.             apikey : apikey
    9.         },
    10.         url: domain,
    11.         cache: true,
    12.         dataType: "json"
    13.     };
    14.     if ('withCredentials' in new XMLHttpRequest) {
    15.         arg.success = fn;
    16.     }
    17.     else {
    18.         var cb = "sc_" + url.replace(/\W/g, '');
    19.         window[cb] = fn;
    20.         arg.jsonpCallback = cb;
    21.         arg.dataType += "p";
    22.     }
    23.     return jQuery.ajax(arg);
    24. };

    And here's my attempt to do it in unity, and I've purposely blanked out my API key:

    Code (CSharp):
    1. public class GetShareCount : MonoBehaviour {
    2.     public string urlTest;
    3.    
    4.     void Start () {
    5.         string url = "http://free.sharedcount.com/?" + urlTest;
    6.  
    7.         WWWForm form = new WWWForm();
    8.        
    9.         form.AddField("domain", url);
    10.         form.AddField("apikey", "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" );
    11.        
    12.         WWW www = new WWW(url, form);
    13.         StartCoroutine(WaitForRequest(www));
    14.     }
    15.    
    16.     IEnumerator WaitForRequest(WWW www) {
    17.         yield return www;
    18.  
    19.         if (www.error == null)
    20.             Debug.Log("WWW Ok!: " + www.text);
    21.         else
    22.             Debug.Log("WWW Error: "+ www.error);
    23.     }
    24. }
    25.  
    I've tried a few things, but all of them result in a 405 method not allowed error from the API

    Does anyone have any insight?

    Thanks
     
  2. vmachupalli

    vmachupalli

    Joined:
    Apr 7, 2014
    Posts:
    8
    Hi,

    Did you try this as a unityScript(Javascript)? As jquery is a wrapper around javascript,i think this should work if you write your script in unityscript? give it a try and let me know.

    By the way, 405 is a client side error.So the request may not be hitting the server at all.
     
  3. FuzzyBalls

    FuzzyBalls

    Joined:
    Jun 12, 2014
    Posts:
    47
    Hey,

    No I haven't tried it in unity script, but I imagine the jquery wouldn't execute surely? It doesn't use unitys syntax right?

    And I'd rather keep it in C#, I prefer it.

    Besides I want to know why it's not working