简体   繁体   English

如何在客户端使用queryparam调用Json Post请求?

[英]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. 我尝试了MultipartEntityNameValuePair但都没有用。 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.

相关问题 如何使用Jersey 2.26客户端与queryParam进行HTTP POST请求? - How to do HTTP POST request with queryParam using Jersey 2.26 client? 如何将查询参数发送到Java客户端的POST请求并使用@QueryParam接收 - How to send query parameters to POST request from Java client, and receive using @QueryParam 如何在服务器端使用 java 从客户端的 HTTP POST 请求中接收和解析 JSON 对象 - How to receive and parse a JSON object from a HTTP POST request from client using java on the server side 如何在 Jersey 客户端 HTTP POST 请求中发布原始 json 文本? - How to post raw json text in Jersey Client HTTP POST request? Jersey Client / JAX-RS和可选(非默认)@QueryParam(客户端) - Jersey Client / JAX-RS and optional (not default) @QueryParam (client side) 使用Mockito在客户端测试POST请求 - Testing POST request in client side using Mockito 如何使用Jetty http客户端将JSON作为请求正文发布? - How to POST json as request body with Jetty http client? 如何在客户端调用方法 - How to call a method on the Client side JSON 用于调用 REST API 的 POST 请求的输入 - JSON Input for POST request to call REST API 如何在客户端 JavaEE 7 的 WebTarget.queryParam 名称中添加特殊字符 - How add special characters in WebTarget.queryParam name in client JavaEE 7
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM