package com.bwei.cllient;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
public class Httpclinentdata {
public String gethttpclinent(String loginurl, String username,
String password) {
String result = “”;
String loginUrl = loginurl + “?username=” + username + “&password=”
+ password;
try {
// 构建get请求实体类
HttpGet httpget = new HttpGet(loginurl);
// 实例化参数实体类
HttpParams httpparams = new BasicHttpParams();
// 设置链接超时
HttpConnectionParams.setConnectionTimeout(httpparams, 5000);
// 设置读取超时
HttpConnectionParams.setSoTimeout(httpparams, 5000);
// 实例化执行请求实体类httpclient
HttpClient httpclient = new DefaultHttpClient(httpparams);
// 发送请求
HttpResponse httpresponse = httpclient.execute(httpget);
if (httpresponse.getStatusLine().getStatusCode() == 200) {
HttpEntity htpentity = httpresponse.getEntity();
result = EntityUtils.toString(htpentity);
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public String postclientdata(String url,String username,String password) {
String result = "";
try{
//实例化POST请求实体类
HttpPost postc = new HttpPost(url);
//实例化参数实体类
HttpParams httpparams = new BasicHttpParams();
//设置链接超时
HttpConnectionParams.setConnectionTimeout(httpparams, 5000);
//设置读取超时
HttpConnectionParams.setSoTimeout(httpparams, 5000);
//实例化执行请求实体类Httpclient
HttpClient httpclient = new DefaultHttpClient(httpparams);
//用来添加参数
List<NameValuePair> list = new ArrayList<NameValuePair>();
NameValuePair user_name = new BasicNameValuePair("username", username);
NameValuePair user_pass = new BasicNameValuePair("password", password);
list.add(user_pass);
list.add(user_name);
//实例化请求实体类
HttpEntity requestentitu = new UrlEncodedFormEntity(list);
//httppost携带请求实体参数
postc.setEntity(requestentitu);
//执行请求
HttpResponse httpResponse = httpclient.execute(postc);
if(httpResponse.getStatusLine().getStatusCode()==200){
HttpEntity resultentity = httpResponse.getEntity();
//返回数据实体类转化成String类型
result=EntityUtils.toString(resultentity);
}
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return result;
}
public Bitmap httpClientGetimg(String imgurl){
Bitmap bitmap =null;
try{
//构建get请求实体类
HttpGet httpget = new HttpGet(imgurl);
//实例化执行请求实体类
HttpClient httpclient = new DefaultHttpClient();
//执行请求
HttpResponse httpresponse = httpclient.execute(httpget);
//判断返回码
if(httpresponse.getStatusLine().getStatusCode()==200){
//获得数据范湖实体类
HttpEntity httpEntity =httpresponse.getEntity();
//获得输入流
InputStream is = httpEntity.getContent();
//从输入流中获取返回Bitmap数据
bitmap = BitmapFactory.decodeStream(is);
}
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return bitmap;
}
}