【发布时间】:2014-05-08 15:38:29
【问题描述】:
我正在尝试从我的 android 应用程序向 REST WebService 发送一些简单的字符串。但是,在我的 WebService 中,我总是收到一个空值。 [编辑]我使用 Multipart 是因为将来我还想连同字符串一起发送图像。
这是我的安卓代码:
HttpPost httppost = new HttpPost(url);
MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
entityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
entityBuilder.addTextBody("email", "teste");
entityBuilder.addTextBody("password", "123");
httppost.setEntity(entityBuilder.build());
response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if(statusCode == HttpStatus.SC_OK) {
Toast.makeText(mContext, "=]", Toast.LENGTH_SHORT).show(); //This line is executed
} else {
Toast.makeText(mContext, "=[", Toast.LENGTH_SHORT).show();
}
这是我的网络服务代码:
@POST
@Path("/cadastrarUsuario")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
public UsuarioVO postUsuario(@PathParam("email") final String email) {
System.out.println("E-mail: " + email);
return usuario;
}
我总是收到无效的电子邮件。我猜@PathParam 或@FormParam 在这里不会真正起作用。但是接收参数的正确方法是什么?
【问题讨论】:
标签: android web-services rest post multipart