HttpClient请求

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

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值