0

I have a web api controller:

// POST: api/CountriesAPI
[ResponseType(typeof(Country))]
public async Task<IHttpActionResult> PostCountry(Country country)
{
    if (!ModelState.IsValid)
    {
        return BadRequest(ModelState);
    }

    db.Countries.Add(country);
    await db.SaveChangesAsync();

    return CreatedAtRoute("DefaultApi", new { id = country.CountryID }, country);
}

I don't know how how to consume this from android. please help.

2

2 Answers 2

1

I've used the Android HTTP Client available here and found it very simple and easy to use.

You can then do a POST with code something like below:

public class HTTPClient 
{
    private static AsyncHttpClient client = new AsyncHttpClient();

    public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler)
    {
        client.get(url, params, responseHandler);
    }

    public static void get(String url, FileAsyncHttpResponseHandler responseHandler)
    {
        client.get(url, responseHandler);
    }

    public static void post(Context context, String url, StringEntity entity, String contentType, AsyncHttpResponseHandler responseHandler)
    {
        client.post(context, url, entity, contentType, responseHandler);
    }
}


HTTPClient.post(this, <server_url>, entity, "application/json", new AsyncHttpResponseHandler() {
    @Override
    public void onSuccess(String response)
    {
        // Do Something
    }

    @Override
    public void onFailure(Throwable error, String content)
    {
        // Do Something else
    }
});
Sign up to request clarification or add additional context in comments.

Comments

1

You can try Libraries like Volley (Requires you to write boilerplate code) or RetroFit

You can make get and post requests using them, do read about Pojos and model creation before you start. And also how do Callbacks work.

1 Comment

thank you so much. however I am still not able to call PUT from postman either, can you please provide me a link, etc. thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.