1

在我的应用程序中,使用下面的代码向服务器发送值。

public String sendHttpRequest(String params) {
    // TODO Auto-generated method stub
    URL url;
    String result = new String();
    try {
        url = new URL(AUTHENTICATION_SERVER_ADDRESS+params);
        System.out.println("url :"+url.toString());
        HttpURLConnection connection;
        connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
                    /*PrintWriter out = new PrintWriter(connection.getOutputStream());

        out.println(params);
        out.close();*/
        BufferedReader in = new BufferedReader(new InputStreamReader(
                connection.getInputStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            System.out.println("inputLine :"+inputLine);
            result = result.concat(inputLine);
        }
        in.close();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    if (result.length() == 0) {
        result = HTTP_REQUEST_FAILED;
    }
    System.out.println("Result :"+result);
    return result;
}

参数=?用户名=*&email=a@a.com&action=signUpUser&number=** * &password= &port=21241

在这种方法中,所有值都被添加到服务器。

但是如果我按照以下方式做同样的事情,所有的价值都不会被更新。

public String sendHttpRequest(String params) {
    // TODO Auto-generated method stub
    URL url;
    String result = new String();
    try {
        url = new URL(AUTHENTICATION_SERVER_ADDRESS);
        System.out.println("url :"+url.toString());
        HttpURLConnection connection;
        connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);

        PrintWriter out = new PrintWriter(connection.getOutputStream());

        out.println(params);
        out.close();

        BufferedReader in = new BufferedReader(new InputStreamReader(
                connection.getInputStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            System.out.println("inputLine :"+inputLine);
            result = result.concat(inputLine);
        }
        in.close();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    if (result.length() == 0) {
        result = HTTP_REQUEST_FAILED;
    }
    System.out.println("Result :"+result);
    return result;
}

为什么会这样?我做错什么了吗?

4

0 回答 0