简体   繁体   English

无法使用Volley发布JSON

[英]Can't POST JSON using Volley

I've searched a lot but can't find out what's happening. 我进行了很多搜索,但无法找到正在发生的事情。 I wrote many variations of this code and here is the most recent one. 我编写了此代码的许多变体,这是最新的。

Android code Android代码

private static void post(@NonNull String url,
                         @NonNull Context context,
                         @NonNull final JSONObject jsonRequest,
                         @NonNull final ServerConnectionAdapter serverConnectionAdapter){
    Log.d(TAG, "post: URL = " + url);
    RequestQueue queue = Volley.newRequestQueue(context);
    JsonObjectRequest postRequest = new JsonObjectRequest(
            Request.Method.POST,
            url,
            jsonRequest,
            serverConnectionAdapter,
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    serverConnectionAdapter.onErrorResponse(null, error);
                }
            }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> map = new HashMap<>();
            // map.put("user", json.toString());
            map.put("user", "{\"id\":-1,\"name\":\"Gustavo Araujo\"}");
            return map;
        }
    };

    queue.add(postRequest);
}

Server (Java) code 服务器(Java)代码

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    System.out.println("doPost");
    System.out.println("REQUEST: ");
    for (Map.Entry<String, String[]> e : request.getParameterMap().entrySet()){
        System.out.print("  ");
        System.out.println(e.getKey() + ": " + java.util.Arrays.toString(e.getValue()));
    }

    System.out.println("end");
}

doPost method output doPost方法输出

doPost
REQUEST: 
end

I've been starring my screen for so long that I think I can't find where is the error. 我已经在屏幕上担任主角了很长时间,以至于我找不到错误所在。 I did find many examples, with and without the getParams(). 我确实发现了很多带有和不带有getParams()的示例。 I tried both, and didn't change anything. 我都尝试过,但没有做任何改变。

The ServerConnectionAdapter is an abstract class that I created to unify the response with the error listener. ServerConnectionAdapter是我创建的抽象类,用于通过错误侦听器统一响应。 I am sure that it is not the problem because it does work flawlessly with the GETs that I have. 我敢肯定这不是问题所在,因为它可以与我拥有的GET完美协作。

The String url also can't be wrong (otherwise the server wouldn't have been triggered. String url也可以是正确的(否则服务器不会被触发。

The jsonRequest also isn't the problem because I already used null there and didn't change the results at all. jsonRequest也不是问题,因为我在那里已经使用过null并且根本没有改变结果。

As I said, I've been starring this code for hours, all my assumptions may be completely wrong, that's why I'm asking for help. 就像我说的那样,我一直在为该代码加注星标几个小时,我所有的假设都可能完全错误,这就是为什么我寻求帮助。

The error photo you posted in comment in telling me that your request was successful, there isn't any issue from mobile side. 您在评论中张贴的错误照片,告诉我您的请求已成功,移动端没有任何问题。 The issue is from server side. 问题出在服务器端。 Because when you are making the request it's going on server but your server is not returning any proper Json. 因为当您发出请求时,它正在服务器上,但是服务器没有返回任何正确的Json。 It's returning nothing that's why you are getting that exception. 它什么也没有返回,这就是为什么您遇到该异常的原因。 Android request needs a Json in return but you are server is not returning that. Android请求需要一个Json作为回报,但您的服务器未返回该请求。

There is a possibility that the issue can be from app side and the cause for that is that maybe you are not sending the proper parameters to server. 问题可能出在应用程序方面,原因可能是您没有向服务器发送正确的参数。 But as far as I can see, the main issue is from server side. 但据我所知,主要问题是来自服务器端。 Because all these exceptions are not handled on server side. 因为所有这些异常不在服务器端处理。 Like when the parameters are wrong it should return a json with message telling you that the parameters are wrong. 就像参数错误时一样,它应该返回一个json消息,告诉您参数错误。

I was doing it wrong. 我做错了。 The way that I wrote the doPost method I was reaching the request parametes, like when you access http://localhost:8080/something?key=value what is not what I intend to do. 我编写doPost方法的方式达到了请求参数,例如,当您访问http://localhost:8080/something?key=value ,我不打算做什么。 I needed to read the body of the request. 我需要阅读请求的正文。 To do so, in Java, it should be as the code below: 为此,在Java中,应为以下代码:

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    // Get the body of the request
    BufferedReader reader = request.getReader();

    // Print each line
    String line;
    while ((line = reader.readLine()) != null){
        System.out.println(line);
    }
}

Thank you for Zohaib Hassan for trying to help. 感谢Zohaib Hassan的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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