【发布时间】:2013-08-25 21:42:54
【问题描述】:
好的,我正在努力正确地构建这个 HttpPost 东西......我用 mvc 4 构建了一个 ASP.Net Web api,目前正试图从我的 android 应用程序中的一个控制器中提取数据。这是我在 android (java) 中的代码,但我不知道如何正确编写它以与 ASP.Net 交互(如标题、名称值对等)。我也会发布控制器代码。
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();
我的控制器如下:
// 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;
}
我用于 http 帖子的 URL 是 http://proopt.co.za.winhost.wa.co.za/api/Course
请帮助我,谢谢。
2017 年更新
使用 RESTful API 与远程数据库交互。您的客户端应用程序应该使用一些基于令牌的身份验证,Retrofit 2.0 是一个用于使用远程 REST API 的出色库。
【问题讨论】:
标签: java android json asp.net-web-api http-post