gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 14:25:48 25 4
gpt4 key购买 nike

如何在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com