服务器没有从android恢复我的POST请求

时间:2016-03-16 22:49:20

标签: android post httpurlconnection

我正在向我的服务器发送POST请求,只是为了检查我的连接是否正常工作,但它没有从我的应用程序中恢复任何数据。我已经尝试了所有的东西,这些是我的结论:1。查询以适当的方式生成(我打印出来并且看起来不错)。 2.代码不会抛出任何错误。 3.代码一直运行到最后。

这是我的代码:

 public static void addLocation(final Location location, final String title, final String author, final String text, final boolean anonymous){
    Thread nt = new Thread(){
        @Override
    public void run(){
            try{

                Map<String, String> parameters = new HashMap<String, String>();

                String slocation = location.getLatitude()+"@"+location.getLongitude();
                String sanonymous;
                if(anonymous){
                    sanonymous = "true";
                }else{
                    sanonymous = "false";
                }

                parameters.put("location", slocation);
                parameters.put("author", author);
                parameters.put("title", title);
                parameters.put("text", text);
                parameters.put("anonymous", sanonymous);


                URL urlToRequest = new URL("http://mashiron.xyz/_03z/pia/proc.php");
                HttpURLConnection urlConnection =
                        (HttpURLConnection) urlToRequest.openConnection();
                urlConnection.setDoOutput(true);
                urlConnection.setRequestMethod("POST");
                urlConnection.setRequestProperty("Content-Type",
                        "application/x-www-form-urlencoded");


                String postParameters = createQueryStringForParameters(parameters);

                urlConnection.setFixedLengthStreamingMode(
                        postParameters.getBytes().length);
                PrintWriter out = new PrintWriter(urlConnection.getOutputStream());

               // System.out.println(receiveResponse(urlConnection));
                out.close();
                System.out.println("1# "+postParameters);
            }catch(Exception e){
                System.out.println("2");

                e.printStackTrace();
            }
        }

    };
    nt.start();
}

这是我用来从hashmap生成查询的工具,但正如我所说,我认为问题不在那里。

public static String createQueryStringForParameters(Map<String, String> parameters) {
    StringBuilder parametersAsQueryString = new StringBuilder();
    if (parameters != null) {
        boolean firstParameter = true;

        for (String parameterName : parameters.keySet()) {
            if (!firstParameter) {
                parametersAsQueryString.append(PARAMETER_DELIMITER);
            }

            parametersAsQueryString.append(parameterName)
                    .append(PARAMETER_EQUALS_CHAR)
                    .append(URLEncoder.encode(
                            parameters.get(parameterName)));

            firstParameter = false;
        }
    }
    System.out.println("4");

    return parametersAsQueryString.toString();
}

非常感谢!

1 个答案:

答案 0 :(得分:0)

您已经完成了所有权利,但您忘记将内容写入OutputStream。打开out

后添加此行
out.write(postParameters);
相关问题