title: POST request from Android to Restful API
body: <p>I am a self starter android learner. Working on a simple enterprise Android project. Need help?</p>
  
  <p>Project:</p>
  
  <pre><code>Backend - Restful Web Service running on Tomcat. It has an insertMethod which takes a Java Object as an argument. The Method is as follows:
  @RequestMapping(value = "/project/insert", method = RequestMethod.POST)
      public @ResponseBody void insertProject(@RequestBody ProjectDTO proj)
      {
              ser.addProject(proj);
      }
  </code></pre>
  
  <p>ProjectDTO is a POJO. </p>
  
  <p>I need to call this method of the Restful API from Android. All the research that I have done is suggesting using JSON.</p>
  
  <p>I need the all the fields of the POJO to be persisted. That is why I created a POJO. Now, I want to reuse this POJO and pass it as a parameter to the HttpPost Client. However, I am not able to get any simple solution on the net. </p>
  
  <p>I am using the following code to call the web service:</p>
  
  <pre><code>HttpClient httpClient = new DefaultHttpClient();
          HttpPost post = new HttpPost("http://localhost:8080/Project-100/user/java/insert");
          post.setHeader("content-type", "application/json");
  
          JSONObject data = new JSONObject();
          try {
              data.put("first_name", f_name);
              data.put("last_name", l_name);
              data.put("email", em);
              StringEntity entity = new StringEntity(data.toString());
              post.setEntity(entity);
              HttpResponse resp = httpClient.execute(post);
  }
  </code></pre>
  
  <blockquote>
    <p>Getting the following error: 03-31 01:22:26.245:
    E/AndroidRuntime(2195): java.lang.IllegalStateException: Could not
    execute method of the activity 03-31 01:22:26.245:
    E/AndroidRuntime(2195):   at
    android.view.View$1.onClick(View.java:4020) 03-31 01:22:26.245:
    E/AndroidRuntime(2195):   at
    android.view.View.performClick(View.java:4780) 03-31 01:22:26.245:
    E/AndroidRuntime(2195):   at
    android.view.View$PerformClick.run(View.java:19866) 03-31
    01:22:26.245: E/AndroidRuntime(2195):     at
    android.os.Handler.handleCallback(Handler.java:739) 03-31
    01:22:26.245: E/AndroidRuntime(2195):     at
    android.os.Handler.dispatchMessage(Handler.java:95) 03-31
    01:22:26.245: E/AndroidRuntime(2195):     at
    android.os.Looper.loop(Looper.java:135) 03-31 01:22:26.245:
    E/AndroidRuntime(2195):   at
    android.app.ActivityThread.main(ActivityThread.java:5257) 03-31
    01:22:26.245: E/AndroidRuntime(2195):     at
    java.lang.reflect.Method.invoke(Native Method) 03-31 01:22:26.245:
    E/AndroidRuntime(2195):   at
    java.lang.reflect.Method.invoke(Method.java:372) 03-31 01:22:26.245:
    E/AndroidRuntime(2195):   at
    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
    03-31 01:22:26.245: E/AndroidRuntime(2195):   at
    com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 03-31
    01:22:26.245: E/AndroidRuntime(2195): Caused by:
    java.lang.reflect.InvocationTargetException 03-31 01:22:26.245:
    E/AndroidRuntime(2195):   at java.lang.reflect.Method.invoke(Native
    Method) 03-31 01:22:26.245: E/AndroidRuntime(2195):   at
    java.lang.reflect.Method.invoke(Method.java:372) 03-31 01:22:26.245:
    E/AndroidRuntime(2195):   at
    android.view.View$1.onClick(View.java:4015) 03-31 01:22:26.245:
    E/AndroidRuntime(2195):   ... 10 more 03-31 01:22:26.245:
    E/AndroidRuntime(2195): Caused by:
    android.os.NetworkOnMainThreadException 03-31 01:22:26.245:
    E/AndroidRuntime(2195):   at
    android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
    03-31 01:22:26.245: E/AndroidRuntime(2195):   at
    java.net.InetAddress.lookupHostByName(InetAddress.java:418) 03-31
    01:22:26.245: E/AndroidRuntime(2195):     at
    java.net.InetAddress.getAllByNameImpl(InetAddress.java:252) 03-31
    01:22:26.245: E/AndroidRuntime(2195):     at
    java.net.InetAddress.getAllByName(InetAddress.java:215) 03-31
    01:22:26.245: E/AndroidRuntime(2195):     at
    org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:142)
    03-31 01:22:26.245: E/AndroidRuntime(2195):   at
    org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:169)
    03-31 01:22:26.245: E/AndroidRuntime(2195):   at
    org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:124)
    03-31 01:22:26.245: E/AndroidRuntime(2195):   at
    org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:365)
    03-31 01:22:26.245: E/AndroidRuntime(2195):   at
    org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:560)
    03-31 01:22:26.245: E/AndroidRuntime(2195):   at
    org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:492)
    03-31 01:22:26.245: E/AndroidRuntime(2195):   at
    org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:470)
    03-31 01:22:26.245: E/AndroidRuntime(2195):   at
    com.manek.collaborationandroid.activity.SignUp.createUser(SignUp.java:70)
    03-31 01:22:26.245: E/AndroidRuntime(2195):   ... 13 more 03-31
    01:22:26.280: I/art(2195): Background sticky concurrent mark sweep GC
    freed 5605(279KB) AllocSpace objects, 0(0B) LOS objects, 26% free,
    956KB/1307KB, paused 15.142ms total 31.686ms</p>
  </blockquote>