0

I have a function which sends a Vector3 pos to a robot via RestSharp and the response of the POST call looks like this:

  • {present_effector_pos: true/false}

I want to parse the true or false and return it as boolean.

public bool postEffectroPos(Vector3 pos, float speed_n)
    {
        var client = new RestClient("http://"+robotIP);
        var request = new RestRequest("/present/effector_position_with_speed.json", Method.POST) { RequestFormat = RestSharp.DataFormat.Json };
        effectorPos a = new effectorPos();
        a.x = pos.x;
        a.y = pos.z;
        a.z = pos.y;
        a.speed = speed_n;
        request.AddBody(a);
        bool move = false;
        try
        {
            client.ExecuteAsync(request, response =>
                {
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        string[] tmp = response.Content.Split(':');
                        string t = tmp[1].Remove(tmp[1].Length - 1);
                        move = bool.Parse(t);
                        Debug.Log(move);
                    }
                    else
                    {
                        Debug.Log(response.StatusCode);
                    }
                });
        }
        catch (Exception error)
        {
            Debug.Log(error);
        }
        return move;
    }

But no matter to what value the variable move is changed in the if clause through parsing the method always returns false.

KonfuPanda
  • 243
  • 1
  • 4
  • 10
  • What is the content of `t`? I'd guess you may have a leading space in there somewhere? – Brendan Green Jun 21 '15 at 13:24
  • It should be "true" or "false" because the `Debug.Log(move)` call afterwards prints the right value(so `True` or `False`) but if I call `Debug.Log(move`) right before the `return` statement it is still `False`. – KonfuPanda Jun 21 '15 at 13:28
  • 2
    I think you'd need to await for the async method - it's probably returning `false` after it starts the task. – cbr Jun 21 '15 at 13:30

0 Answers0