2

I created a restful web service:

@POST
@Path("grd")
@Consumes("application/json")
@Produces("text/plain")
public String guardarDato(PostParams jsonObject) {
/*
 something here
}

PostParams is a pojo:

public class PostParams {

    private final  Map<String, String> postParamsList;

    public PostParams() {
        postParamsList = new HashMap<>();
    }

    public void addParameter(String name, String value) {
        postParamsList.put(name, value);
    }

    public String getPostParamsList(String name) {
        return postParamsList.get(name);
    }

    public void getPostParamsMap() {
        if (postParamsList.isEmpty()) {
            System.out.println("Parametros vacios");
        } else {
            for (String key : postParamsList.keySet()) {
                System.out.println("Clave: " + key + " -> Valor: " +         postParamsList.get(key));
            }
        }
    }
}

I am trying to call this web service from android with HttpUrlConnection, but my object is null pojo in my web services.

 try {

        URL url = new URL("http://localhost:8080/VfqCh/hgt/opli/grd");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/json");
       // conn.setRequestProperty("Accept", "application/json");

        PostParams postParams = new PostParams();
        postParams.addParameter("h", "51rt3");
        postParams.addParameter("x", "dsfsd8698sdfs");
        postParams.addParameter("ax", "Dairon");
        postParams.addParameter("tf", "D");
        // String input = "{\"qty\":100,\"name\":\"iPad 4\"}";
        Gson gson = new Gson();
        String gString = gson.toJson(postParams, PostParams.class);
        try (OutputStreamWriter printout = new OutputStreamWriter(conn.getOutputStream())) {
            printout.write(gString);
            printout.flush();
        }

        if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
            throw new RuntimeException("Failed : HTTP error code : "
                    + conn.getResponseCode());
        }

        BufferedReader br = new BufferedReader(new InputStreamReader(
                (conn.getInputStream())));

        String output;
        System.out.println("Output from Server .... \n");
        while ((output = br.readLine()) != null) {
            System.out.println(output);
        }

        conn.disconnect();

    } catch (MalformedURLException e) {

        e.printStackTrace();

    } catch (IOException e) {

        e.printStackTrace();

    }

}

The information is send to web services but the object is null. if I change the type of object that is receive in the web services if it works!:

@POST
@Path("grd")
@Consumes("application/json")
@Produces("text/plain")
public String guardarDato(String jsonObject) {
/*
 something here
}

But i need receive an object in the web services! It is possible?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Dayron Cediel
  • 87
  • 2
  • 7

1 Answers1

0

You made a POJO for a HashMap, but you could have just used a JsonObject that is already provided by Gson and basically acts like a HashMap. Then, you toString it, and you have a JSON string you can send to a server. There is no need for Gson.toJson and worrying if it was converted correctly.

Your server is accepting a JSON string, as defined by

@Consumes("application/json")

Therefore, this should be the method

public String guardarDato(String jsonObject) {

You will need to add Gson as a dependency for your server, then you should be able to get the JSON to an object with something like PostParams params = Gson.fromJson(jsonObject, PostParams.class) assuming the data was sent correctly from the client side, but as stated earlier, your POJO doesn't add much functionality on top of just a JsonObject or plain HashMap

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • yes, I can do that and it works. But i need use: public String guardarDato(PostParams jsonObject) { It is possible? – Dayron Cediel Apr 18 '16 at 21:49
  • You can just `PostParams params = Gson.fromJson(jsonObject)` inside the method. I don't think you can have anything other than a string or an int as a parameter to your method. – OneCricketeer Apr 18 '16 at 21:51
  • Also, I just did see the mention of Android, so using `localhost` does need to be replaced with the IP address of the web server. – OneCricketeer Apr 18 '16 at 21:52
  • yes, i change localhost for my Ip in the code. The problem is that i am migrating to HttpUrlConnection and all web services were made:@Consumes("application/json") public String guardarDato(PostParams jsonObject). – Dayron Cediel Apr 25 '16 at 21:18