Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have a web api that's return list of employee

below is my api code
C#
[Route("api/Employee")]
    public HttpResponseMessage Get()
    {
        var employees= EmployeesRepository.GetAllEmployees();
        HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, employees);
        return response;
    }


What I have tried:

strring url="http://localhost:6680/api/Employee";

httpClient clint=new httpClient();
Posted
Updated 19-Dec-17 20:33pm
v4

HttpClient[^] is a C# object, not of Java. That would only work if you are building Android app using Xamarin. I am not about Xamarin, but ASP.NET team has surely shared the source code for consuming ASP.NET Web API in C# code. Read it here, Calling a Web API From a .NET Client in ASP.NET Web API 2 (C#) | The ASP.NET Site[^]

On the other hand if you are going to develop Android application, then you cannot use C# code and instead you are going to use the native Java APIs for networking. HttpURLConnection[^] object is one of such objects. For native Java Android sample read this, Connecting to the Network | Android Developers[^]. You would require to use this API URL, then consume the API. That would return the JSON content (or XML based on the configuration), for which you would need to have some JSON serializers. Read this thread for a tutorial on that, Android JSON Parser Tutorial[^].
 
Share this answer
 
//Use reference for System.Json
// Gets data from the passed API URL.
private async Task<JsonValue> GetMethodAsync (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;
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900