【问题标题】:java restful web services retrieving post datajava RESTful Web 服务检索帖子数据
【发布时间】:2015-09-09 05:22:12
【问题描述】:

我收到来自 android volley 的 JSON 对象 POST 请求。我正在尝试使用以下代码读取 json。

    @POST
    @Path("/driver/insertTripLog")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.TEXT_PLAIN)
    public String insertTripLog(InputStream incomingData) throws Exception
    {
        StringBuilder sb = new StringBuilder();
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(incomingData));
            String line = null;
            while ((line = in.readLine()) != null) {
                sb.append(line);
            }
        } catch (Exception e) {
            System.out.println("Error Parsing: - ");
        }
        String data=sb.toString();
        System.out.println("Data Received: " +data );
     return data;
}

我得到的输出是 json 对象,但特殊字符是用 %XX 格式编码的。

样本输出:

json=%5B%7B%22name%22%3A%22value%22%7D%5D

如何正确读取从 volley 发送的 post json 数据。

【问题讨论】:

    标签: json web-services post parameters android-volley


    【解决方案1】:

    使用URLDecoder 类来处理您看到的百分比编码内容:

    String data = sb.toString();
    data = java.net.URLDecoder.decode(data, "UTF-8");
    

    输出:

    json=[{"name":"value"}]
    

    【讨论】:

    • 在输出中我不想显示 json=
    • 然后用data.substring(5)去掉前导文字json=
    • @Tim Biegeleisen 如果我想在来自 android volley 的 post 请求中发送多个参数,我如何才能在 rest webservices 中读取多个参数
    • @venkatasivaiah 我认为您应该为此提出一个新问题。如果我的答案解决了您的第一个问题,您最好将我的答案标记为正确。
    • 您的答案是正确的。我有一个新问题,如果我想在来自 android volley 的 post 请求中发送多个参数,我如何通过小示例读取 rest webservices 中的多个参数
    猜你喜欢
    • 2017-01-09
    • 1970-01-01
    • 2016-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-26
    • 1970-01-01
    • 2011-12-20
    相关资源
    最近更新 更多