Search code examples
androidrestxamarin.androidvisual-studio-2015watson-iot

REST POST not responding in Xamarin.Android App. It is responding with Postman but not app.


I am sending POST request from Xamarin.Android appl on VS 2015 using RestSharp but no response is arriving. I increased timeout but no response. IT IS WORKING WITH POSTMAN but not with Android app.

 RestClient client = new RestClient("https://" + orgId + ".internetofthings.ibmcloud.com");
 RestRequest request = new RestRequest("/api/v0002/application/types/" + typeId + "/devices/" + deviceId + "/events/" + typeId + deviceId, Method.POST);

        byte[] byteArray = Encoding.UTF8.GetBytes(username + ":" + password);
        string authenticationToken = Convert.ToBase64String(byteArray);

        request.AddHeader("Authorization", "Basic " + authenticationToken);
        request.Timeout = 2000000;
        request.AddJsonBody(newVal);


        IRestResponse response = client.Execute(request);
        return response.StatusCode.ToString();`

I am sending only "newVal" (string) in the body. This works with Postman. Please let me know if more explanation is required. THANK YOU SO MUCH.

Environment: Visual Studio 2015, RESTSharp, Xamarin.Android, Server: IBM BlueMix


Solution

  • This is resolved, Issue was with the 'content type'. I was adding content type as:

    request.AddHeader("Content-Type","application/json");
    

    but Content type was not setting for some reason and that's why my request was being shut from server and returned status code 0. So instead, I did the following and it worked fine.

    request.JsonSerializer.ContentType = "application/json; charset=utf-8";