package com.lixu.httpget_post;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
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 android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
EditText et1;
EditText et2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1 = (EditText) findViewById(R.id.et1);
et2 = (EditText) findViewById(R.id.et2);
Button bt1 = (Button) findViewById(R.id.btn1);
Button bt2 = (Button) findViewById(R.id.btn2);
bt1.setOnClickListener(this);
bt2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn1:
new Thread(new Runnable() {
@Override
public void run() {
// 1.定义一个客户端 你可以理解为是一个浏览器
HttpClient client = new DefaultHttpClient();
// 2. 定义一个get请求,并封装好他的参数
String data = "username" + et1.getText().toString() + "password" + et2.getText().toString();
HttpGet get = new HttpGet("http://10.0.2.2:8080/test/lianjie?" + data);
// 3. 用定义好的浏览器来执行一个地址
try {
HttpResponse response = client.execute(get);
// 4. 获取状态码,看返回回来的是否是200
final int statuscode = response.getStatusLine().getStatusCode();
if (statuscode == 200) {
InputStream in = response.getEntity().getContent();
final String content = getStringFromStream(in);
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(MainActivity.this, "状态为:" + statuscode + "内容是:" + content, 0).show();
}
});
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
break;
case R.id.btn2:
new Thread(new Runnable() {
public void run() {
HttpClient client = new DefaultHttpClient();
// 定义一个post请求的对象
HttpPost post = new HttpPost("http://10.0.2.2:8080/test/lianjie");
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
NameValuePair name = new BasicNameValuePair("username", et1.getText().toString());
NameValuePair pwd = new BasicNameValuePair("pasword", et2.getText().toString());
parameters.add(name);
parameters.add(pwd);
UrlEncodedFormEntity entity;
try {
entity = new UrlEncodedFormEntity(parameters, "UTF-8");
post.setEntity(entity);
HttpResponse response = client.execute(post);
final int statuscode = response.getStatusLine().getStatusCode();// 获取状态码
if (statuscode == 200) {
InputStream in = response.getEntity().getContent();
final String content=getStringFromStream(in);
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(MainActivity.this, "状态为:"+statuscode+"内容是:"+content, 0).show();
}
});
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
break;
default:
break;
}
}
public String getStringFromStream(InputStream in) {
byte[] buffer = new byte[1024];
ByteArrayOutputStream bytearray = new ByteArrayOutputStream();
int len = 0;
try {
if ((len = in.read(buffer, 0, 1024)) != -1) {
bytearray.write(buffer);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String content = bytearray.toString();
return content;
}
}