网络工具类 post

public static String postData(String url,String phone,String pswd){

try {
    HttpURLConnection connection = null;

    URL url1 = new URL(url);
    connection = (HttpURLConnection) url1.openConnection();
    connection.setReadTimeout(5000);
    connection.setConnectTimeout(5000);
    connection.setRequestMethod("POST");

    String body = "phone=" + URLEncoder.encode(phone) + "&pwd=" + URLEncoder.encode(pswd);
    connection.getOutputStream().write(body.getBytes());

    if (connection.getResponseCode() ==  HttpURLConnection.HTTP_OK){
        InputStream inputStream = connection.getInputStream();
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        BufferedReader reader = new BufferedReader(inputStreamReader);

        StringBuilder builder = new StringBuilder();
        String str = "";
        while ((str = reader.readLine()) != null){
            builder.append(str);
        }

        connection.disconnect();
        Log.i("aaa", builder.toString());
        return builder.toString();
    }

} catch (Exception e) {
    e.printStackTrace();
}

return null;

}

猜你喜欢

转载自blog.csdn.net/black_amber/article/details/89305128