我将我的Android应用程序中的参数发送到后端并尝试在我的POST方法中检索我的Android客户端发送的参数,但即使客户端发送的参数不是空,我也会继续获取空参数。
Java POST方法:
@POST
@Produces({ "application/json" })
@Path("/login")
public LoginResponse Login(@FormParam("email") String email, @FormParam("password") String password) {
LoginResponse response = new LoginResponse();
if(email != null && password != null && email.length() != 0 && password.length() != 0){
//Detect if null or empty
//Code
}
return response;
}
Android客户端:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://MY_APP_NAME.appspot.com/user/login");
String json = "";
JSONObject jsonObject = new JSONObject();
try {
jsonObject.accumulate("email", "roger@gmail.com");
jsonObject.accumulate("password", "123");
json = jsonObject.toString();
StringEntity se = new StringEntity(json);
httppost.setEntity(se);
httppost.setHeader("Content-Type", "application/json");
httppost.setHeader("ACCEPT", "application/json");
HttpResponse httpResponse = httpclient.execute(httppost);
}
catch(Exception ex) { }
我相信方法和客户端的Content-Type也是一样的。为什么我没有从Java后端方法接收参数?
选中:
答案 0 :(得分:0)
我们希望得到你,试试NameValuePair
public void postData() {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/yourscript.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "123"));
nameValuePairs.add(new BasicNameValuePair("string", "Hey"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// Catch Protocol Exception
} catch (IOException e) {
// Catch IOException
}
}
答案 1 :(得分:0)
如果您遇到类似问题,我建议您使用Fiddler 这是一个免费的http检查器和调试器,通过它您可以看到您的应用程序发送到后端服务器的http请求和后端答案。
祝你好运