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