httpclient的使用模式:
1. 创建一个httpclent
2.实例化新的http方法,比如postmethod 或 getmethod
3.设置http参数名称/值
4.使用httpclent执行http调用
5.处理http响应
import java.io.ioexception;
import java.io.inputstream;
import java.net.urlencoder;
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.statusline;
import org.apache.http.client.clientprotocolexception;
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 com.itheima.httpclient.utils.utils;
import android.os.bundle;
import android.os.handler;
import android.os.message;
import android.app.activity;
import android.view.menu;
import android.view.view;
import android.widget.edittext;
import android.widget.toast;
public class mainactivity extends activity {
handler handler = new handler(){
@override
public void handlemessage(android.os.message msg) {
toast.maketext(mainactivity.this, (string)msg.obj, 0).show();
}
};
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);
}
//get方法
public void get(view v){
edittext et_name = (edittext) findviewbyid(r.id.et_name);
edittext et_pass = (edittext) findviewbyid(r.id.et_pass);
final string name = et_name.gettext().tostring();
final string pass = et_pass.gettext().tostring();
thread t = new thread(){
@override
public void run() {
string path = "http://192.168.13.13/web/servlet/checklogin?name=" + urlencoder.encode(name) + "&pass=" + pass;
//使用httpclient框架做get方式提交
//1.创建httpclient对象
httpclient hc = new defaulthttpclient();
//2.创建httpget对象,构造方法的参数就是网址
httpget hg = new httpget(path);
//3.使用客户端对象,把get请求对象发送出去
try {
httpresponse hr = hc.execute(hg);
//拿到响应头中的状态行
statusline sl = hr.getstatusline();
if(sl.getstatuscode() == 200){
//拿到响应头的实体
httpentity he = hr.getentity();
//拿到实体中的内容,其实就是服务器返回的输入流
inputstream is = he.getcontent();
string text = utils.gettextfromstream(is);
//发送消息,让主线程刷新ui显示text
message msg = handler.obtainmessage();
msg.obj = text;
handler.sendmessage(msg);
}
} catch (exception e) {
// todo auto-generated catch block
e.printstacktrace();
}
}
};
t.start();
//post方法
public void post(view v){
string path = "http://192.168.13.13/web/servlet/checklogin";
//1.创建客户端对象
//2.创建post请求对象
httppost hp = new httppost(path);
//封装form表单提交的数据
basicnamevaluepair bnvp = new basicnamevaluepair("name", name);
basicnamevaluepair bnvp2 = new basicnamevaluepair("pass", pass);
list<basicnamevaluepair> parameters = new arraylist<basicnamevaluepair>();
//把basicnamevaluepair放入集合中
parameters.add(bnvp);
parameters.add(bnvp2);
//要提交的数据都已经在集合中了,把集合传给实体对象
urlencodedformentity entity = new urlencodedformentity(parameters, "utf-8");
//设置post请求对象的实体,其实就是把要提交的数据封装至post请求的输出流中
hp.setentity(entity);
//3.使用客户端发送post请求
httpresponse hr = hc.execute(hp);
if(hr.getstatusline().getstatuscode() == 200){
inputstream is = hr.getentity().getcontent();
}
utils方法:
import java.io.bytearrayoutputstream;
public class utils {
public static string gettextfromstream(inputstream is){
byte[] b = new byte[1024];
int len = 0;
bytearrayoutputstream bos = new bytearrayoutputstream();
try {
while((len = is.read(b)) != -1){
bos.write(b, 0, len);
string text = new string(bos.tobytearray());
bos.close();
return text;
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace();
return null;
}
转载:http://blog.csdn.net/chaoyu168/article/details/50964727