zoukankan      html  css  js  c++  java
  • Android为TV端助力 post带数据请求方式,传递的数据格式包括json和map

    如下:

    public static String httpPost(String url, String json) {
    try {
    URL u = new URL(url);
    HttpURLConnection httpURLConnection = (HttpURLConnection) u.openConnection();
    httpURLConnection.setConnectTimeout(TIMEOUT);
    httpURLConnection.setDoInput(true);
    httpURLConnection.setDoOutput(true);
    httpURLConnection.setRequestMethod("POST");
    httpURLConnection.setUseCaches(false);

    httpURLConnection.setRequestProperty("Content-Type",
    "application/json");

    httpURLConnection.setRequestProperty("Content-Length",
    String.valueOf(json.getBytes().length));

    OutputStream outputStream = httpURLConnection.getOutputStream();
    outputStream.write(json.getBytes());

    int response = httpURLConnection.getResponseCode();
    if (response == HttpURLConnection.HTTP_OK) {
    InputStream inptStream = httpURLConnection.getInputStream();
    return dealResponseResult(inptStream);
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    return "";
    }

    private static 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;
    }

    如果传的值不是json格式,而是map就可以采取下面这种格式

    public static String httpPost(String url, Map<String, String> params) {
    byte[] data = getRequestData(params, "utf-8").toString().getBytes();
    try {
    URL u = new URL(url);
    HttpURLConnection httpURLConnection = (HttpURLConnection) u.openConnection();
    httpURLConnection.setConnectTimeout(TIMEOUT);
    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 (Exception e) {
    e.printStackTrace();
    }
    return "";
    }

    private static 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); // remove the last "&"
    } catch (Exception e) {
    e.printStackTrace();
    }
    return stringBuffer;
    }

  • 相关阅读:
    Javascript高级程序设计笔记(很重要尤其是对象的设计模式与继承)
    javascript面向对象技术基础总结
    cURL范例(包括错误输出和详情输出)
    javascript知识点总结
    php memcache知识点总结
    php mcrypt加密实例
    spl处理文件(文件详细信息、文件遍历、查询指定行、写入CSV文件)
    table-layout 属性
    backface-visibility 属性 :隐藏被旋转的 div 元素的背面
    HTML 5 全局 contenteditable 属性
  • 原文地址:https://www.cnblogs.com/xiaoxiaing/p/5383688.html
Copyright © 2011-2022 走看看