我怎样才能访问它是由POST方法(Android版)中的球衣POST注释发送的数据? [英] How can I access the data which is sent by POST method (Android) in the jersey POST annotation?

查看:144
本文介绍了我怎样才能访问它是由POST方法(Android版)中的球衣POST注释发送的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何可以访问哪些由POST方法(Android版)中的球衣POST注释发送的数据?
Android客户端:

 公共类TestClient的延伸活动{    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.layout_main);
        HttpClient的HttpClient的=新DefaultHttpClient();
        HttpPost httppost =新HttpPost(myURL);        尝试{            清单<&的NameValuePair GT; namevaluepairs中=新的ArrayList<&的NameValuePair GT;(2);
            nameValuePairs.add(新BasicNameValuePair(用户,用户1));
            nameValuePairs.add(新BasicNameValuePair(密码,密码1));
            httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));            httpclient.execute(httppost);        }赶上(ClientProtocolException E){        }赶上(IOException异常五){        }    }}

以下是由Tomcat的本地主机上运行的服务器的code:

  @Path(测试)
公共类的测试{
     @得到
     @Produces(MediaType.TEXT_PLAIN)
        公共字符串respondAsReady(){
            返回运行;
        }     @POST
     @Consumes(MediaType.APPLICATION_FORM_URLEN codeD)
     @Produces(MediaType.TEXT_PLAIN还是其他?)              //这里我想程序后(使用的System.out.println)的数据是由使用POST方法在客户端上载的控制台。
        }
}


解决方案

无论客户端是PHP,HTML,或使用HttpClient的服务器端不应该关心的Andr​​oid。你应该能够使用@FormParam注解来获得的值。请参见问题的答案我张贴在评论,或的这个例子

您应该可以你的答案改变​​像简单的东西:

  @POST
@Consumes(MediaType.APPLICATION_FORM_URLEN codeD)
公共无效themethodname(@FormParam(用户)字符串的用户,
                          @FormParam(密码)字符串密码){
    的System.out.println(用户名:+用户+,+密码:+密码);
}

How can I access the data which is sent by POST method (Android) in the jersey POST annotation? Android Client:

public class TestClient extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_main);
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("myURL");

        try {

            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("user", "user1"));
            nameValuePairs.add(new BasicNameValuePair("password", "password1"));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            httpclient.execute(httppost);

        } catch (ClientProtocolException e) {

        } catch (IOException e) {

        }

    }

}

The following is the code of the Server which is run on Localhost by Tomcat:

@Path("test")
public class Test {
     @GET
     @Produces(MediaType.TEXT_PLAIN)
        public String respondAsReady() {
            return "Running";
        }

     @POST
     @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
     @Produces(MediaType.TEXT_PLAIN or other?)

              //Here I'd like the program to post (with System.out.println) the data to the console which is uploaded by the client using the POST method.


        }
}

解决方案

Regardless of whether the client is PHP, html, or android using httpclient the server side shouldn't care. You should be able to use the @FormParam annotation to get the values. See the answer in the question I posted in the comments, or this example.

You should be able to change your answer to something simpler like :

@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public void themethodname(@FormParam("user") String user, 
                          @FormParam("password") String password) {
    System.out.println("Username: "+user+", "+"Password: "+password);
}

这篇关于我怎样才能访问它是由POST方法(Android版)中的球衣POST注释发送的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆