2

我创建了一个宁静的网络服务:

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

PostParams 是一个 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));
            }
        }
    }
}

我正在尝试使用 HttpUrlConnection 从 android 调用此 Web 服务,但我的对象在我的 Web 服务中是 null pojo。

 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();

    }

}

信息被发送到 Web 服务,但对象为空。如果我更改在 Web 服务中接收的对象类型,如果它有效!:

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

但我需要在网络服务中接收一个对象!有可能的?

4

1 回答 1

0

您为 HashMap 制作了 POJO,但您可以只使用JsonObjectGson 已经提供的并且基本上就像 HashMap 一样。然后,你toString得到它,你就有了一个可以发送到服务器的 JSON 字符串。Gson.toJson如果转换正确,则无需担心。

您的服务器正在接受一个 JSON 字符串,定义为

@Consumes("application/json")

因此,这应该是方法

public String guardarDato(String jsonObject) {

您将需要添加 Gson 作为服务器的依赖项,然后您应该能够将 JSON 获取到一个对象,例如PostParams params = Gson.fromJson(jsonObject, PostParams.class)假设数据是从客户端正确发送的,但如前所述,您的 POJO 不会添加仅在 aJsonObject或 plain之上的许多功能HashMap

于 2016-04-18T21:43:10.803 回答