HttpUrlconnnection请求

package com.bwei.http;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import org.apache.http.HttpConnection;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

public class Httpconnect {
public String getHttpUrLConnection(String loginurl, String username,
String password) {
String result = “”;
// get方式提交参数
String loginURL = loginurl + “?username=” + username + “&password=”
+ password;
try {

        // 新建链接地址
        URL url = new URL(loginURL);
        // 获得链接
        HttpURLConnection httpconnection = (HttpURLConnection) url
                .openConnection();
        // 设置链接超时时间
        httpconnection.setConnectTimeout(5 * 1000);
        // 设置读取超时时间
        httpconnection.setReadTimeout(5 * 1000);
        // 获得输入流
        InputStream in = httpconnection.getInputStream();
        // 获得返回数据,通过缓存读取
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String str = "";
        while ((str = br.readLine()) != null) {
            result += str;
        }
        br.close();
        in.close();
        httpconnection.disconnect();

        Log.e("---------", result);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return result;
}

public String postgeturlconnection(String urllogin, String name, String pass) {
    String result = "";
    try {
        // 新建连接地址
        URL url = new URL(urllogin);
        // 获得链接
        HttpURLConnection httpConnection = (HttpURLConnection) url
                .openConnection();
        // 设置链接超时时间
        httpConnection.setConnectTimeout(5 * 1000);
        httpConnection.setReadTimeout(5 * 1000);
        // 拼接参数
        String params = "username=" + name + "&password=" + pass;
        // 获取数据输出流
        DataOutputStream os = new DataOutputStream(
                httpConnection.getOutputStream());
        // 写入数据输出流
        os.write(params.getBytes());
        // 刷新提交
        os.flush();
        // 关闭输出流
        os.close();
        // 判断是否返回正确
        if (httpConnection.getResponseCode() == 200) {
            // 实例化缓存读取
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    httpConnection.getInputStream()));
            String reline = "";
            while ((reline = br.readLine()) != null) {
                result += reline;
            }
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return result;
}

public Bitmap getimage(String imgurl) {
    Bitmap bitmap = null;
    try {
        // 建立连接
        URL url = new URL(imgurl);
        // 获得链接
        HttpURLConnection http = (HttpURLConnection) url.openConnection();
        http.setConnectTimeout(5 * 1000);
        http.setReadTimeout(5 * 1000);
        InputStream stream = http.getInputStream();
        bitmap = BitmapFactory.decodeStream(stream);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return bitmap;
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值