简体   繁体   中英

How to call Json Post request with queryparam on client side?

I wrote both Service and CLient part of application. 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

However when I try to call it on my Android Application it is not working. While debuging on service side I see that phone and password parameters are 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 :

    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. 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. (you don't even need a POST request for this). Your objects are passed as string objects, not JSON objects.

In your client code , the URL is different because you're encoding the parameters as part of the POST request entity (payload). The parameters are packaged inside of the request/message body and not in the 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).

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).

I'm not familiar with GSON but your code might look something like

@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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM