android http post提交数据

来源:互联网 发布:逆回购 知乎 编辑:程序博客网 时间:2024/05/21 05:37
    public String submitPostData(String strUrlPath, Map<String, String> params, String encode) {        byte[] data = getRequestData(params, encode).toString().getBytes();//获得请求体        try {            java.net.URL url = new URL(strUrlPath);            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();            httpURLConnection.setConnectTimeout(3000);            httpURLConnection.setDoInput(true);            httpURLConnection.setDoOutput(true);            httpURLConnection.setRequestMethod("POST");            httpURLConnection.setUseCaches(false);            httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");            httpURLConnection.setRequestProperty("Content-Length", String.valueOf(data.length));            OutputStream outputStream = httpURLConnection.getOutputStream();            outputStream.write(data);            int response = httpURLConnection.getResponseCode();            if (response == HttpURLConnection.HTTP_OK) {                InputStream inptStream = httpURLConnection.getInputStream();                return dealResponseResult(inptStream);            }        } catch (IOException e) {            return "err: " + e.getMessage().toString();        }        return "-1";    }    public StringBuffer getRequestData(Map<String, String> params, String encode) {        StringBuffer stringBuffer = new StringBuffer();        try {            for (Map.Entry<String, String> entry : params.entrySet()) {                stringBuffer.append(entry.getKey())                        .append("=")                        .append(URLEncoder.encode(entry.getValue(), encode))                        .append("&");            }            stringBuffer.deleteCharAt(stringBuffer.length() - 1);        } catch (Exception e) {            e.printStackTrace();        }        return stringBuffer;    }    public String dealResponseResult(InputStream inputStream) {        String resultData = null;        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();        byte[] data = new byte[1024];        int len = 0;        try {            while ((len = inputStream.read(data)) != -1) {                byteArrayOutputStream.write(data, 0, len);            }        } catch (IOException e) {            e.printStackTrace();        }        resultData = new String(byteArrayOutputStream.toByteArray());        return resultData;    }
0 0