1 package com.lixu.httpget_post; 2 import java.io.ByteArrayOutputStream; 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.io.UnsupportedEncodingException; 6 import java.util.ArrayList; 7 import java.util.List; 8 import org.apache.http.HttpResponse; 9 import org.apache.http.NameValuePair; 10 import org.apache.http.client.ClientProtocolException; 11 import org.apache.http.client.HttpClient; 12 import org.apache.http.client.entity.UrlEncodedFormEntity; 13 import org.apache.http.client.methods.HttpGet; 14 import org.apache.http.client.methods.HttpPost; 15 import org.apache.http.impl.client.DefaultHttpClient; 16 import org.apache.http.message.BasicNameValuePair; 17 import android.app.Activity; 18 import android.os.Bundle; 19 import android.view.View; 20 import android.view.View.OnClickListener; 21 import android.widget.Button; 22 import android.widget.EditText; 23 import android.widget.Toast; 24 25 public class MainActivity extends Activity implements OnClickListener { 26 27 EditText et1; 28 EditText et2; 29 30 protected void onCreate(Bundle savedInstanceState) { 31 super.onCreate(savedInstanceState); 32 setContentView(R.layout.activity_main); 33 et1 = (EditText) findViewById(R.id.et1); 34 et2 = (EditText) findViewById(R.id.et2); 35 Button bt1 = (Button) findViewById(R.id.btn1); 36 Button bt2 = (Button) findViewById(R.id.btn2); 37 bt1.setOnClickListener(this); 38 bt2.setOnClickListener(this); 39 40 } 41 42 @Override 43 public void onClick(View v) { 44 switch (v.getId()) { 45 case R.id.btn1: 46 new Thread(new Runnable() { 47 48 @Override 49 public void run() { 50 // 1.定义一个客户端 你可以理解为是一个浏览器 51 HttpClient client = new DefaultHttpClient(); 52 // 2. 定义一个get请求,并封装好他的参数 53 String data = "username" + et1.getText().toString() + "password" + et2.getText().toString(); 54 HttpGet get = new HttpGet("http://10.0.2.2:8080/test/lianjie?" + data); 55 // 3. 用定义好的浏览器来执行一个地址 56 try { 57 HttpResponse response = client.execute(get); 58 // 4. 获取状态码,看返回回来的是否是200 59 final int statuscode = response.getStatusLine().getStatusCode(); 60 if (statuscode == 200) { 61 InputStream in = response.getEntity().getContent(); 62 final String content = getStringFromStream(in); 63 runOnUiThread(new Runnable() { 64 65 public void run() { 66 Toast.makeText(MainActivity.this, "状态为:" + statuscode + "内容是:" + content, 0).show(); 67 } 68 }); 69 } 70 71 } catch (ClientProtocolException e) { 72 // TODO Auto-generated catch block 73 e.printStackTrace(); 74 } catch (IOException e) { 75 // TODO Auto-generated catch block 76 e.printStackTrace(); 77 } 78 79 } 80 }).start(); 81 break; 82 case R.id.btn2: 83 new Thread(new Runnable() { 84 public void run() { 85 HttpClient client = new DefaultHttpClient(); 86 // 定义一个post请求的对象 87 HttpPost post = new HttpPost("http://10.0.2.2:8080/test/lianjie"); 88 List<NameValuePair> parameters = new ArrayList<NameValuePair>(); 89 NameValuePair name = new BasicNameValuePair("username", et1.getText().toString()); 90 NameValuePair pwd = new BasicNameValuePair("pasword", et2.getText().toString()); 91 parameters.add(name); 92 parameters.add(pwd); 93 94 UrlEncodedFormEntity entity; 95 try { 96 entity = new UrlEncodedFormEntity(parameters, "UTF-8"); 97 post.setEntity(entity); 98 HttpResponse response = client.execute(post); 99 final int statuscode = response.getStatusLine().getStatusCode();// 获取状态码 100 if (statuscode == 200) { 101 InputStream in = response.getEntity().getContent(); 102 final String content=getStringFromStream(in); 103 runOnUiThread(new Runnable() { 104 105 public void run() { 106 Toast.makeText(MainActivity.this, "状态为:"+statuscode+"内容是:"+content, 0).show(); 107 } 108 }); 109 } 110 111 } catch (UnsupportedEncodingException e) { 112 // TODO Auto-generated catch block 113 e.printStackTrace(); 114 } catch (ClientProtocolException e) { 115 // TODO Auto-generated catch block 116 e.printStackTrace(); 117 } catch (IOException e) { 118 // TODO Auto-generated catch block 119 e.printStackTrace(); 120 } 121 } 122 123 }).start(); 124 break; 125 126 default: 127 break; 128 } 129 130 } 131 132 public String getStringFromStream(InputStream in) { 133 byte[] buffer = new byte[1024]; 134 ByteArrayOutputStream bytearray = new ByteArrayOutputStream(); 135 int len = 0; 136 try { 137 if ((len = in.read(buffer, 0, 1024)) != -1) { 138 bytearray.write(buffer); 139 } 140 } catch (IOException e) { 141 // TODO Auto-generated catch block 142 e.printStackTrace(); 143 } 144 String content = bytearray.toString(); 145 return content; 146 } 147 148 }