Ok I am struggling to build this HttpPost thing correctly... I built an ASP.Net web api with mvc 4, and am currently trying to pull data from one of the controllers in my android app. This is my code in android (java) but I do not know how to write it correctly to interface with ASP.Net (like the headers, the namevaluepairs, etc). I will post the controller code as well.
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(http://proopt.co.za.winhost.wa.co.za/api/Course);
List<NameValuePair> nameValue = new ArrayList<NameValuePair>();
nameValue.add(new BasicNameValuePair("", ""));
httppost.setEntity(new UrlEncodedFormEntity(nameValue));
httppost.setHeader("Content-type", "application/json");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
And my controller as follows:
// GET api/Course/5
public Course GetCourse(string id)
{
Course course = db.Courses.Find(id);
if (course == null)
{
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
}
return course;
}
The URL I use for the http post is http://proopt.co.za.winhost.wa.co.za/api/Course
Please assist me, thanks.
UPDATE 2017
Use a RESTful API to interface with remote databases. Your client app should make use of some token-based authentication, and Retrofit 2.0 is a fantastic library for consuming remote REST APIs.