Local webapi project doesn't work in xamarin.android

armansparmansp USMember
edited May 2016 in Xamarin.Android

hi
i create a solution with 2 projects

1- project is web Api
2- is xamarin.android project

in Web API project i have a Url (http://localhost:27635/api/books/1/details)
and this return details of book like this

{"query":{"Title":"Midnight Rain","Genre":"Fantasy","PublishDate":"2000-12-16T00:00:00","Description":"A former architect battles an evil sorceress.","Price":14.95,"Author":"Ralls, Kim"}}
"}

and in xamarin project:
I used this code for consuming web api project
public class MainActivity : Activity
{

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

       :
            Button button = FindViewById<Button>(Resource.Id.getWeatherButton);

            // When the user clicks the button ...
            button.Click += async (sender, e) => {

                // Get the latitude and longitude entered by the user and create a query.
                string url = "http://localhost:8080/api/books/1/details";
                //string url = "http://169.254.129.9/api/books/1/details";

                // Fetch the information asynchronously, 
                // parse the results, then update the screen:
                JsonValue json = await FetchWeatherAsync(url);
                ParseAndDisplay(json);
            };
        }
        // Gets data from the passed URL.
        private async Task<JsonValue> FetchWeatherAsync(string url)
        {
            // Create an HTTP web request using the URL:
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(url));
            request.ContentType = "application/json";
            request.Method = "GET";

            // Send the request to the server and wait for the response:
            using (WebResponse response = await request.GetResponseAsync())
            {
                // Get a stream representation of the HTTP web response:
                using (Stream stream = response.GetResponseStream())
                {
                    // Use this stream to build a JSON document object:
                    JsonValue jsonDoc = await Task.Run(() => JsonObject.Load(stream));
                    Console.Out.WriteLine("Response: {0}", jsonDoc.ToString());

                    // Return the JSON document:
                    return jsonDoc;

                }
            }
        }
        // Parse the weather data, then write temperature, humidity, 
private void ParseAndDisplay(JsonValue json)
        {
            TextView location = FindViewById<TextView>(Resource.Id.locationText);


            JsonValue weatherResults = json["query"];


            location.Text = weatherResults["Title"];

        }
    }

When I use my local webapi project , Below code will not be executed

using (Stream stream = response.GetResponseStream())
{
 // Use this stream to build a JSON document object:
 JsonValue jsonDoc = await Task.Run(() => JsonObject.Load(stream));
Console.Out.WriteLine("Response: {0}", jsonDoc.ToString());

  // Return the JSON document:
return jsonDoc;
}

But,When I use an external web api is working correctly

Sign In or Register to comment.