0
votes

I am a self starter android learner. Working on a simple enterprise Android project. Need help?

Project:

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);
    }

ProjectDTO is a POJO.

I need to call this method of the Restful API from Android. All the research that I have done is suggesting using JSON.

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.

I am using the following code to call the web service:

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);
}

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

2
Here's what to research: Caused by: android.os.NetworkOnMainThreadExceptionnasch

2 Answers

0
votes
  //sending Data

   HttpClient httpClient = new DefaultHttpClient();

      HttpPost httpPost = new HttpPost("Your URL");

    List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
    nameValuePair.add(new BasicNameValuePair("KEY", VALUE));

    //Encoding POST data
    try {
          httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));

    } 
          catch (UnsupportedEncodingException e) 

         {
       e.printStackTrace();
    }

    try {

        HttpResponse response = httpClient.execute(httpPost);
     }

YOUR URL - the url that you wants to send your data.

KEY - a tag used for sending data.(keep data inside a tag)

VALUE - value that you send.

If you have multiple data to send then replicate the code

nameValuePair.add(new BasicNameValuePair("KEY", VALUE)); with your tag and value

0
votes

I followed the instructions by Hephze, and now getting the following error.

1 basic questions I have is that my Pojo has 3 fields. The Restful function expects a Pojo. But, at the Android client, we are passing an Entity of NameValuePair. How will the Restful API interpret it? Is there a way to pass a Pojo as a parameter?

04-11 13:12:30.953: E/AndroidRuntime(1965): java.lang.IllegalStateException: Could not execute method of the activity
04-11 13:12:30.953: E/AndroidRuntime(1965):     at android.view.View$1.onClick(View.java:4020)
04-11 13:12:30.953: E/AndroidRuntime(1965):     at android.view.View.performClick(View.java:4780)
04-11 13:12:30.953: E/AndroidRuntime(1965):     at android.view.View$PerformClick.run(View.java:19866)
04-11 13:12:30.953: E/AndroidRuntime(1965):     at android.os.Handler.handleCallback(Handler.java:739)
04-11 13:12:30.953: E/AndroidRuntime(1965):     at android.os.Handler.dispatchMessage(Handler.java:95)
04-11 13:12:30.953: E/AndroidRuntime(1965):     at android.os.Looper.loop(Looper.java:135)
04-11 13:12:30.953: E/AndroidRuntime(1965):     at android.app.ActivityThread.main(ActivityThread.java:5257)
04-11 13:12:30.953: E/AndroidRuntime(1965):     at java.lang.reflect.Method.invoke(Native Method)
04-11 13:12:30.953: E/AndroidRuntime(1965):     at java.lang.reflect.Method.invoke(Method.java:372)
04-11 13:12:30.953: E/AndroidRuntime(1965):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
04-11 13:12:30.953: E/AndroidRuntime(1965):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
04-11 13:12:30.953: E/AndroidRuntime(1965): Caused by: java.lang.reflect.InvocationTargetException
04-11 13:12:30.953: E/AndroidRuntime(1965):     at java.lang.reflect.Method.invoke(Native Method)
04-11 13:12:30.953: E/AndroidRuntime(1965):     at java.lang.reflect.Method.invoke(Method.java:372)
04-11 13:12:30.953: E/AndroidRuntime(1965):     at android.view.View$1.onClick(View.java:4015)
04-11 13:12:30.953: E/AndroidRuntime(1965):     ... 10 more
04-11 13:12:30.953: E/AndroidRuntime(1965): Caused by: android.os.NetworkOnMainThreadException
04-11 13:12:30.953: E/AndroidRuntime(1965):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
04-11 13:12:30.953: E/AndroidRuntime(1965):     at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
04-11 13:12:30.953: E/AndroidRuntime(1965):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
04-11 13:12:30.953: E/AndroidRuntime(1965):     at java.net.InetAddress.getAllByName(InetAddress.java:215)
04-11 13:12:30.953: E/AndroidRuntime(1965):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:142)
04-11 13:12:30.953: E/AndroidRuntime(1965):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:169)
04-11 13:12:30.953: E/AndroidRuntime(1965):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:124)
04-11 13:12:30.953: E/AndroidRuntime(1965):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:365)
04-11 13:12:30.953: E/AndroidRuntime(1965):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:560)
04-11 13:12:30.953: E/AndroidRuntime(1965):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:492)
04-11 13:12:30.953: E/AndroidRuntime(1965):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:470)
04-11 13:12:30.953: E/AndroidRuntime(1965):     at com.manek.collaborationandroid.activity.SignUp.createUser(SignUp.java:82)
04-11 13:12:30.953: E/AndroidRuntime(1965):     ... 13 more