Android Http传输数据

public class HttpDownload {

    public String httpGet(String urlPath, String params) throws IOException {
        long ST = System.currentTimeMillis();
        String resultStr = "";
        HttpGet request = new HttpGet(urlPath + params);
        HttpClient httpClient = new DefaultHttpClient();
        httpClient.getParams().setIntParameter(HttpConnectionParams.SO_TIMEOUT, 10 * 1000);
        httpClient.getParams().setIntParameter(HttpConnectionParams.CONNECTION_TIMEOUT, 10 * 1000);
        HttpResponse response = httpClient.execute(request);
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            HttpEntity entity = response.getEntity();
            resultStr = EntityUtils.toString(entity, HTTP.UTF_8);
        }
        long ET = System.currentTimeMillis();
        return resultStr;
    }

    public String httpPostString(String urlPath, String params) throws IOException {
        long ST = System.currentTimeMillis();
        String resultStr = "";
        HttpPost request = new HttpPost(urlPath);
        HttpClient httpClient = new DefaultHttpClient();
        httpClient.getParams().setParameter("Content-Type", "application/json");
        httpClient.getParams().setParameter("Charset", HTTP.UTF_8);
        httpClient.getParams().setIntParameter(HttpConnectionParams.SO_TIMEOUT, 10 * 1000); // 超时设置
        httpClient.getParams().setIntParameter(HttpConnectionParams.CONNECTION_TIMEOUT, 10 * 1000);// 连接超时
        HttpResponse response;
        if (params != null) {
            StringEntity reqEntity = new StringEntity(params);
            reqEntity.setContentType("application/json");
            reqEntity.setContentEncoding("utf-8");
            request.setEntity(reqEntity);
        }
        response = httpClient.execute(request);
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            resultStr = EntityUtils.toString(response.getEntity());
        }
        long ET = System.currentTimeMillis();
        return resultStr;
    }

    public String httpSendData(String urlPath, String params) throws IOException {
        long ST = System.currentTimeMillis();
        StringBuffer sb = new StringBuffer();
        BufferedReader br = null;
        try {
            URL url = new URL(urlPath);
            //打开连接
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            //设置提交方式
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setRequestMethod("POST");
            //post方式不能使用缓存
            conn.setUseCaches(false);
            conn.setInstanceFollowRedirects(true);
            //设置连接超时时间
            conn.setConnectTimeout(10 * 1000);
            //配置本次连接的Content-Type,配置为application/x-www-form-urlencoded
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            //维持长连接
            conn.setRequestProperty("Connection", "Keep-Alive");
            //设置浏览器编码
            conn.setRequestProperty("Charset", HTTP.UTF_8);
            DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
            //将请求参数数据向服务器端发送
            dos.writeBytes(params);
            dos.flush();
            dos.close();
            if (conn.getResponseCode() == 200) {
                //获得服务器端输出流
                br = new BufferedReader(new InputStreamReader(conn.getInputStream(), HTTP.UTF_8));
                int c = 0;
                while ((c = br.read()) != -1) {
                    sb.append((char) c);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw new IOException("系统错误");
        } finally {
            try {
                if (br != null)
                    br.close();
            } catch (IOException e) {
                e.printStackTrace();
                throw new IOException("系统错误");
            }
        }
        long ET = System.currentTimeMillis();
        return sb.toString();
    }

    public String postFileParts(BaseActivity baseActivity, String param, List<String> sendPathList, String url) throws Exception {
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(url);
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.setCharset(Charset.forName("utf-8"));
        for (int i = 0; i < sendPathList.size(); i++) {
            builder.addBinaryBody("fileData", new File(sendPathList.get(i)));
        }
        builder.addTextBody("", "");
        HttpEntity entity = builder.build();
        post.setEntity(entity);
        HttpResponse response = client.execute(post);
        if (response.getStatusLine().getStatusCode() == 200) {
            response = client.execute(post);
        }
        return "";

    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值