package com.itheima.httpclient; 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();//子线程是不能执行Toast.makeText, } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } 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() {//URLEncoder.encode(name)是仿浏览器的中文编码, String path = "http://192.168.13.13/Web/servlet/CheckLogin?name=" + URLEncoder.encode(name) + "&pass=" + pass; //使用httpClient框架做get方式提交 //1.创建HttpClient对象,用于执行get请求 HttpClient hc = new DefaultHttpClient(); //2.创建httpGet对象,构造方法的参数就是网址 HttpGet hg = new HttpGet(path);//一个get请求 //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) { e.printStackTrace(); } } }; t.start(); } public void post(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"; //1.创建客户端对象 HttpClient hc = new DefaultHttpClient(); //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); try { //要提交的数据都已经在集合中了,把集合传给实体对象 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(); String text = Utils.getTextFromStream(is); //发送消息,让主线程刷新ui显示text Message msg = handler.obtainMessage(); msg.obj = text; handler.sendMessage(msg); } } catch (Exception e) { e.printStackTrace(); } } }; t.start(); } }
package com.itheima.httpclient.utils; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; public class Utils { public static String getTextFromStream(InputStream is){ byte[] b = new byte[1024]; int len = 0;//每次read的长度 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://www.cnblogs.com/yaowen/p/4934923.html,如需转载请自行联系原作者