0

I'm trying to get API response by passing service url and json parameter. Url and Parameter passing properly to the requestAPI function, but doesn't give response from PostAsync method. Url is the API location, parameter is the Category Id. When I'm running same API in the browser, it gives correct response. But not in app.

This is requestAPI function.

public async Task<ResponseObject> requestAPI(string urlString, string jsonParameters)
        {
            await Task.Delay(2000); // NOTE: just to simulate a HTTP request over the wire

            try
            {
                var json = JsonConvert.SerializeObject(jsonParameters);
                HttpContent httpContent = new StringContent(json, Encoding.UTF8, "application/json");

                HttpResponseMessage response = null;

                if (true)       // no issue here.
                {                                                    
                    response = await client.PostAsync(urlString, httpContent);
                }
                else
                {
                    response = await client.PutAsync(urlString, httpContent);
                }

                if (response.IsSuccessStatusCode)
                {
                    Debug.WriteLine(@"              TodoItem successfully saved.");
                    var returnVal = await response.Content.ReadAsStringAsync();
                    return new ResponseObject{
                        jsonString = returnVal,
                        isSuccess = true,
                        error = null
                    };
                }
                else
                {
                    return new ResponseObject
                    {
                        jsonString = null,
                        isSuccess = false,
                        error = null
                    };
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(@"              ERROR {0}", ex.Message);
                return new ResponseObject
                {
                    jsonString = null,

                    isSuccess = false,
                    error = null
                };
            }
        }

Category Id comes from this method.

private async void loadBookList(string categoryId)
        {
            IsBusy = true;

            if (CrossConnectivity.Current.IsConnected)
            {
                ResponseObject responseObject = await _apiService.requestAPI("http://192.168.0.35/kiyawamu-api/index.php/rest/V1/mobileappintegration/getbookdetailsbycategory", Convert.ToString(categoryId));

                if (responseObject.isSuccess)
                {
                    var jsonObject = JsonConvert.DeserializeObject<BookListJsonObject>(responseObject.jsonString);
                    CategoryBooks = new ObservableCollection<Book>(jsonObject.booksByCategory);
                }
                else
                {
                    giveAlertForCommonError();
                }
            }
        }

I tried following solutions, but doesn't work.

  1. Url used as a uri var uri = new Uri(urlString);
  2. jsonParameter also used as a string
  3. GetAsync also used

Any assistance on this will be greatly appreciated.

Mohan Perera
  • 370
  • 1
  • 4
  • 15
  • Android or iOS? Is this code in a PCL? – Daniel Jun 28 '18 at 08:24
  • `but doesn't give response from PostAsync method` what do you mean by this, what status code are you getting. how are you calling this – TheGeneral Jun 28 '18 at 08:26
  • This code in PCL @Daniel. I'm running only Android project. – Mohan Perera Jun 28 '18 at 08:40
  • When run the debugger, it gives response -> `response = {StatusCode: 404, ReasonPhrase: 'Not Found', Version: 1.1, Content: System.Net.Http.StreamContent` @TheGeneral – Mohan Perera Jun 28 '18 at 08:42
  • 1
    This is an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). It does return a response. Not Found is a response. Check the body of the response to see if there is a message about the request. – Nkosi Jun 28 '18 at 09:19
  • You coorect @Nkosi. Not Found is also response. But that means there are no any value in response. In my working responses, it gives -> `response = {StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent` – Mohan Perera Jun 28 '18 at 09:47

1 Answers1

1

If you're running it on Android and the code is in a PCL, try to add ModernHttpClient so you can use the native HttpClientHandler.

https://github.com/paulcbetts/ModernHttpClient

But I also recommend all to upgrade from PCL to .NET standard. If you do so you don't have to install a NuGet package to use HttpCliennt. And then you can select implementation of HttpClientHandler in project settings.

Daniel
  • 1,537
  • 1
  • 13
  • 16