android - 如何访问 Jersey POST 注释中由 POST 方法 (Android) 发送的数据?

标签 android json jersey jax-rs

如何在 jersey POST 注释中访问通过 POST 方法 (Android) 发送的数据? 安卓客户端:

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) {

        }

    }

}

以下是Tomcat运行在Localhost的Server的代码:

@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.


        }
}

最佳答案

无论客户端是 PHP、html 还是使用 httpclient 的 android,服务器端都不应该关心。您应该能够使用 @FormParam 注释来获取值。请参阅 question I posted in the comments 中的答案, 或 this example .

您应该能够将答案更改为更简单的内容,例如:

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

关于android - 如何访问 Jersey POST 注释中由 POST 方法 (Android) 发送的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12784504/

相关文章:

PHP json_encode 将行作为对象而不是数组返回

json - Swift - 迭代字典数组,同时从每个字典中检索一个值

java - Android中的Facebook登录弹出两个登录界面

java - 使用原始类中的自定义 Android AnalogClock 设置时间

javascript - AJAX 语法错误 : JSON. 解析:意外字符

jquery - 响应状态 200 正常,但使用 jquery 错误函数

javascript - 如何使用 jersey/jackson 生成多种类型的 json 数组

java - Log4j2 在控制台上打印工作找到,但没有创建日志文件

android - 如何使用钛在android中应用幻灯片动画?

java - 如何通过单击 ListView 项并将值发送到对话框来显示来创建自定义对话框?