title: How to do an HTTP Post in Android to web api
body: <p>I am trying to do a post to web api from android. The below is the code that I am using in android. The code is running without exception however the data is not inserted to the database. I included the code that I am using.</p>
  
  <pre><code>@Override
  protected List doInBackground(Void... params) 
  {
      List userDetails3 = new ArrayList();
      URL url = null;
      String urlshared = "http://10.0.0.9/MyWebApi/api/Student/PostAddStudent?userName="+"s4"+"&amp;"+"password="+"1234"+"&amp;"+"firsName="+"z"+"&amp;"+"lastName="+"g"+"&amp;"+"telephone="+"9160000000"+"&amp;"+"address="+"2300 xxx"+"&amp;"+"aliasMailId="+"s1.xx"+"&amp;"+"emailId="+"xx@gmail.com"+"&amp;"+"skypeId="+"z.g"+"";
  
      try {
              url = new URL(urlshared);
          }
      catch (MalformedURLException exception) 
          {
              exception.printStackTrace();
          }
          HttpURLConnection httpURLConnection = null;
          DataOutputStream dataOutputStream = null;
          try 
          {
              httpURLConnection = (HttpURLConnection) url.openConnection();
              httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
              httpURLConnection.setRequestMethod("POST");
  
              PrintWriter out = new PrintWriter(httpURLConnection.getOutputStream()); // this code is executed without exception
          } 
          catch (IOException exception) 
          {
              exception.printStackTrace(); 
          }
          return userDetails3; // (in debug) the code will run successfuly till here without exception 
  }
  </code></pre>
  
  <p>I am trying to access the PostAddStudent in the webapi. The below is my implementation of the controller and I excluded the codes inside the methods. For your information, I did test "PostAddStudent" and the other methods in the RegisterController by using the postman and they are working fine and the data inserted to database. </p>
  
  <pre><code>public class RegisterController : ApiController
  {      
      public IEnumerable&lt;Register&gt; GetRegisterAuth(string userName,string password)
      {
          //return instance of register
      }
  
      public IEnumerable&lt;Register&gt; 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&lt;Instructor&gt;(System.Net.HttpStatusCode.Created, inst);
           //if error exist then just return Badrequest
           var response = Request.CreateResponse&lt;Instructor&gt;(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&lt;Instructor&gt;(System.Net.HttpStatusCode.Created, inst);
           //if error exist then just return Badrequest
           var response = Request.CreateResponse&lt;Instructor&gt;(System.Net.HttpStatusCode.BadRequest, inst);            
      }   
  }
  </code></pre>
  
  <p>the below is the RouteConfig</p>
  
  <pre><code>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();
      }
  }
  </code></pre>
  
  <p>the below is the WebApiConfig</p>
  
  <pre><code>public static class WebApiConfig
  {
      public static void Register(HttpConfiguration config)
      {
          config.Routes.MapHttpRoute(
              name: "DefaultApi",
              routeTemplate: "api/{controller}/{id}",
              defaults: new { id = RouteParameter.Optional }
          );
      }
  }
  </code></pre>
  
  <p>I need the android code that can do the post to PostAddStudent.</p>
  
  <p>thanks</p>