[英]How to call Json Post request with queryparam on client side?
I wrote both Service and CLient part of application. 我写了应用程序的Service和CLient部分。 I tested my service with "
Postman
" application and it is working fine with url = http://192.168.2.50:8084/FaceBusinessService/webresources/service/login?phone=123456789&password=1234
我使用“
Postman
”应用程序测试了我的服务,它在url = http://192.168.2.50:8084/FaceBusinessService/webresources/service/login?phone=123456789&password=1234
可以正常工作
However when I try to call it on my Android Application it is not working. 但是,当我尝试在Android应用程序上调用它时,它无法正常工作。 While debuging on service side I see that phone and password parameters are NULL.
在服务端进行调试时,我看到电话和密码参数为NULL。
Here is my service side : 这是我的服务方面:
@Path("login")
@POST
@Produces("application/json")
public String postJson(@QueryParam("phone")String phone, @QueryParam("password") String password) {
String info = null;
try {
UserInfo userInfo = null;
UserModel userModel = new UserModel();
userInfo = userModel.isPersonRegistered(phone, password);
Gson gson = new Gson();
System.out.println(gson.toJson(userInfo));
info = gson.toJson(userInfo);
} catch (Exception e) {
System.out.println("Exception: " + e.getMessage());
}
return info;
}
Here is my android app side : 这是我的Android应用程序端:
private UserInfo loginUser(String phone, String password) {
UserInfo userInfo = null;
HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost("http://192.168.2.27:8084/FaceBusinessService/webresources/service/login");
try {
/*
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("phone", new StringBody(phone));
entity.addPart("password", new StringBody(password));
post.setEntity(entity);
*/
List<NameValuePair> params = new ArrayList<NameValuePair>(2);
params.add(new BasicNameValuePair("phone", phone));
params.add(new BasicNameValuePair("password", password));
post.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
Log.d(TAG, "POST String: " + post.toString());
try {
HttpResponse response = httpClient.execute(post);
if (response.getEntity().getContentLength() > 0) {
String json_string = EntityUtils.toString(response.getEntity());
JSONObject jsonObject = new JSONObject(json_string);
// TODO
return userInfo;
}
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (JSONException e) {
e.printStackTrace();
return null;
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
return null;
}
I tried both MultipartEntity
and NameValuePair
but none of them worked. 我尝试了
MultipartEntity
和NameValuePair
但都没有用。 Could you give me idea how to handle this issue? 您能告诉我如何处理此问题吗?
Note that when testing with Postman you passed parameters (user name and password) as part of the URL (URL encoded), which can be directly retrieved on the server side. 请注意,在使用Postman进行测试时,您将参数(用户名和密码)作为URL(URL编码)的一部分传递,可以在服务器端直接检索。 (you don't even need a POST request for this).
(您甚至不需要POST请求)。 Your objects are passed as string objects, not JSON objects.
您的对象作为字符串对象而不是JSON对象传递。
In your client code , the URL is different because you're encoding the parameters as part of the POST request entity (payload). 在客户代码中,URL不同,因为您将参数编码为POST请求实体(有效负载)的一部分。 The parameters are packaged inside of the request/message body and not in the URL.
参数打包在请求/消息主体内部,而不在URL中。
Now since your URL doesn't have the parameters, you should retrieve them by deserializing the request (desderialize the JSON request into a UserInfo object). 现在,由于您的URL没有参数,您应该通过反序列化请求(将JSON请求反序列化为UserInfo对象)来检索它们。
Note that you should rewrite your server side code completely as it should accept a application/JSON object but it apparently should return/produce a String object (plain/text or application/HTML). 请注意,您应该完全重写服务器端代码,因为它应该接受应用程序/ JSON对象,但是显然应该返回/产生一个String对象(纯文本/文本或应用程序/ HTML)。
I'm not familiar with GSON but your code might look something like 我不熟悉GSON,但是您的代码可能看起来像
@Path("login")
@POST
@Produces("text/plain")
@Consumes("application/json")
public String postJson(UserInfo ui) {
String info = null;
try {
UserInfo userInfo = null;
UserModel userModel = new UserModel();
userInfo = userModel.isPersonRegistered(ui.phone, ui.password);
Gson gson = new Gson();
System.out.println(gson.toJson(userInfo));
info = gson.toJson(userInfo);
} catch (Exception e) {
System.out.println("Exception: " + e.getMessage());
}
return info;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.