Unity3D C#WWW类:每次请求后执行代码 [英] Unity3D C# WWW class: execute code after every request

查看:255
本文介绍了Unity3D C#WWW类:每次请求后执行代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网上找不到任何线索,因为我想很多人都放弃了Unity的WWW类来学习复杂的东西.我正在使用WWW类与我的REST API进行通信,并且需要找到一种在每次请求后 后执行一些代码的方法. (如果响应为401,我需要检查响应代码并执行一些默认行为)

I couldn't find any clues for this online as I suppose a lot of people have abandoned Unity's WWW class for complex stuff. I'm using the WWW class to talk to my REST API, and I need to find a way to execute some code after every request. (I need to check the response code and execute some default behaviour if the response is 401)

有没有简单的方法可以实现这一目标?

Is there a simple way to achieve this?

(我正在使用协程发送请求)

(I'm using coroutines to send the requests)

谢谢.

更新:当前代码示例

Auth.cs:

public IEnumerator Login(WWWForm data, Action<APIResponse> success, Action<APIResponse> failure)
    {
        WWW request = new WWW (API + "auth/authenticate", data);
        yield return request;
        if (request.responseHeaders ["STATUS"] == "HTTP/1.1 401 Unauthorized") {
            //Do something, I want to do this on every request, not just on this login method
        }
        if (request.error != null) {
            failure (new APIResponse(request));
        }
        else {
            //Token = request.text.Replace("\"", "");
            Token = request.text;
            Debug.Log (Token);
            success (new APIResponse (request));
        }

    }

用法:

StartCoroutine (auth.Login (data, new Action<APIResponse> (response => {
            //Do stuff

        }), new Action<APIResponse> (response => {
            //Do stuff
        })));

推荐答案

我没有使用带有Rest api的WWW来实现异步"方式,使用超时和异常处理对我有效:

I am not using WWW with rest api to achieve the "async" way, using the time out and exception handling works for me:

public static string PostJson(string host, string resourceUrl, object json)
{
    var client = new RestClient(host);
    client.Timeout = Settings.LIGHT_RESPONSE_TTL; //set timeout duration

    var request = new RestRequest(resourceUrl, Method.POST);
    request.RequestFormat = DataFormat.Json;
    request.AddBody(json);

    try
    {
        var response = client.Execute(request);
        return response.Content;
    }
    catch (Exception error)
    {
        Utils.Log(error.Message);
        return null;
    }

}

要使用此功能:

var result = JsonUtils.PostJson("http://192.168.1.1:8080", "SomeEndPoints/abc/def", jsonString);
if (string.IsNullOrEmpty(result))
{
     //Error
}
else
{
      //Success
}

更新:为确保此类调用不会阻止UI,请使用以下代码:

Update: to make sure such calls do not block the UI, use below code:

Loom.RunAsync(() => {
    var result = JsonUtils.PostJson("http://192.168.1.1:8080", "SomeEndPoints/abc/def", jsonString);
    if (!string.IsNullOrEmpty(result)) {

    }
});

您可以在此处下载Loom.可以在下面的GIF动画中演示此类代码的示例用法,请注意,UI(圆形指示器)未被阻止!

You can download Loom here. Example use of such code can be demonstrated with below GIF animation, note that the UI (circular indicator) is not blocked!

这篇关于Unity3D C#WWW类:每次请求后执行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆