1、使用HttpClient框架发送get、post请求
google收集apache提供的一个发送Http请求框架
public class Tools { public static String getTextFromStream(InputStream is){ byte[] b = new byte[1024]; int len; ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { while((len = is.read(b)) != -1){ bos.write(b, 0, len); } //把字节数组输出流转换成字节数组,然后用字节数组构造一个字符串 String text = new String(bos.toByteArray()); return text; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } }
public class MainActivity extends Activity { Handler handler = new Handler(){ 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 click1(View v){ //获取用户输入的账号密码 EditText et_name = (EditText) findViewById(R.id.et_name); EditText et_pass = (EditText) findViewById(R.id.et_pass); String name = et_name.getText().toString(); String pass = et_pass.getText().toString(); final String path = "http://169.254.244.136/Web2/servlet/Login?name=" + URLEncoder.encode(name) + "&pass=" + pass; Thread t = new Thread(){ @Override public void run() { //使用httpClient框架提交 //1.创建client对象 HttpClient client = new DefaultHttpClient(); //2.创建get请求对象 HttpGet get = new HttpGet(path); try { //3.使用client发送get请求 HttpResponse response = client.execute(get); //获取状态行 StatusLine line = response.getStatusLine(); //获取状态码 int code = line.getStatusCode(); if(code == 200){ //获取实体 HttpEntity entity = response.getEntity(); InputStream is = entity.getContent(); String text = Tools.getTextFromStream(is); 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 click2(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(); final String path = "http://169.254.244.136/Web2/servlet/Login"; Thread t = new Thread(){ @Override public void run() { HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(path); //把要提交的数据封装至post中 List<NameValuePair> parameters = new ArrayList<NameValuePair>(); //arg0:表单的名字 //arg1:表单中的值 BasicNameValuePair bnvp1 = new BasicNameValuePair("name", name); BasicNameValuePair bnvp2 = new BasicNameValuePair("pass", pass); //把BasicNameValuePair对象放入集合 parameters.add(bnvp1); parameters.add(bnvp2); //创建实体对象 UrlEncodedFormEntity entity = null; try { //把集合对象封装到实体中 entity = new UrlEncodedFormEntity(parameters, "utf-8"); } catch (Exception e1) { e1.printStackTrace(); } //把实体对象封装至post对象中 post.setEntity(entity); try { HttpResponse response = client.execute(post); if(response.getStatusLine().getStatusCode() == 200){ InputStream is = response.getEntity().getContent(); String text = Tools.getTextFromStream(is); Message msg = handler.obtainMessage(); msg.obj = text; handler.sendMessage(msg); } } catch (Exception e) { e.printStackTrace(); } } }; t.start(); } }