如何在Android中将HTTP发布到web api

时间:2015-08-14 08:37:41

标签: java android url asp.net-web-api httpurlconnection

我正在尝试从android发布到web api的帖子。以下是我在android中使用的代码。代码正在运行,但数据未插入数据库。我包含了我正在使用的代码。

public class RegisterController : ApiController
{      
    public IEnumerable<Register> GetRegisterAuth(string userName,string password)
    {
        //return instance of register
    }

    public IEnumerable<Register> GetRegisterByUserName(string userName)
    {
        //return instance of register
    }

    public HttpResponseMessage PostAddInstructor(string instUserName, string instPassword, string instFirsName, string instLastName, string instTelephone, string instAddress, string instAliasMailId, string instEmailId, string instSkypeId)
    {
         //add to database and if okay will return System.Net.HttpStatusCode.Created       
         var response = Request.CreateResponse<Instructor>(System.Net.HttpStatusCode.Created, inst);
         //if error exist then just return Badrequest
         var response = Request.CreateResponse<Instructor>(System.Net.HttpStatusCode.BadRequest, inst);            
    }

    public HttpResponseMessage PostAddStudent(string userName, string password, string firsName, string lastName, string telephone, string address, string aliasMailId, string emailId, string skypeId)
    {
         //add to database and if okay will return System.Net.HttpStatusCode.Created       
         var response = Request.CreateResponse<Instructor>(System.Net.HttpStatusCode.Created, inst);
         //if error exist then just return Badrequest
         var response = Request.CreateResponse<Instructor>(System.Net.HttpStatusCode.BadRequest, inst);            
    }   
}

我正在尝试访问webapi中的PostAddStudent。下面是我对控制器的实现,我排除了方法中的代码。为了您的信息,我做了测试&#34; PostAddStudent&#34;使用邮递员在RegisterController中的其他方法,它们工作正常,数据插入数据库。

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

        //config.EnableSystemDiagnosticsTracing();
    }
}

以下是RouteConfig

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}

以下是WebApiConfig

int testFunc(int n){
    if(n < 3) return 0;
    int num = 7;
    for(int j = 1; j <= n; j *= 2) num++;
    for(int k = n; k > 1; k--) num++;
    return testFunc(n/3) + num;
}

我需要可以发布到PostAddStudent的安卓代码。

感谢

0 个答案:

没有答案