从Android到Restful API的POST请求

时间:2015-04-11 00:51:58

标签: android json rest post parameters

我是一个自学的机器人学习者。在一个简单的企业Android项目上工作。需要帮助?

项目:

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是一个POJO。

我需要从Android调用Restful API的这种方法。我所做的所有研究都建议使用JSON。

我需要保留POJO的所有字段。这就是我创建POJO的原因。现在,我想重用这个POJO并将其作为参数传递给HttpPost客户端。但是,我无法在网上获得任何简单的解决方案。

我使用以下代码来调用Web服务:

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

获得以下错误:03-31 01:22:26.245:   E / AndroidRuntime(2195):java.lang.IllegalStateException:无法   执行活动的方法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):引起:   java.lang.reflect.InvocationTargetException 03-31 01:22:26.245:   E / AndroidRuntime(2195):at java.lang.reflect.Method.invoke(Native   方法)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更多03-31 01:22:26.245:   E / AndroidRuntime(2195):引起:   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更多03-31   01:22:26.280:I / art(2195):背景粘性并发标记扫描GC   释放5605(279KB)AllocSpace对象,0(0B)LOS对象,26%免费,   956KB / 1307KB,暂停15.142ms总计31.686ms

2 个答案:

答案 0 :(得分:0)

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

您的网址 - 您要发送数据的网址。

KEY - 用于发送数据的标签。(将数据保存在标签内)

VALUE - 您发送的值。

如果您要发送多个数据,请复制代码

nameValuePair.add(new BasicNameValuePair(&#34; KEY&#34;,VALUE));使用您的标记和值

答案 1 :(得分:0)

我按照Hephze的说明,现在收到以下错误。

我有一个基本问题是我的Pojo有3个字段。 Restful函数需要一个Pojo。但是,在Android客户端,我们传递了NameValuePair的实体。 Restful API将如何解释它?有没有办法将Pojo作为参数传递?

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