package com.example.sdafds;


import java.io.ByteArrayOutputStream;
import java.io.InputStream;






public class StreamTools {


//把一个inputStream 转换成一个String
public static String readStream(InputStream in) throws Exception{


//定义一个央存输出流
ByteArrayOutputStream baos = new ByteArrayOutputStream();


int len = -1;
byte[] butter = new byte[1024];
while((len = in.read(butter)) != -1){
baos.write(butter,0,len);
}
in.close();
String content = new String(baos.toByteArray());


return content;
}



}







package com.example.sdafds;


import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;


import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
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.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends Activity {


private EditText et_password;
private EditText et_username;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

et_username = (EditText) findViewById(R.id.et_username);
et_password = (EditText) findViewById(R.id.et_password);




}


//get请求
public void click1(View v){

new Thread(){
public void run(){
try {
String name = et_username.getText().toString().trim();
String pwd = et_password.getText().toString().trim();
//2.1定义get方式提交的路经
String path = "http://192.168.11.73:8080/login/Login?LoginServlet?username="+URLEncoder.encode(name,"utf-8")+URLEncoder.encode(pwd,"utf-8")+"";

//3 获取httpclient实例
DefaultHttpClient client = new DefaultHttpClient();
//3.1准备get请求定义一个httpget实现
HttpGet get = new HttpGet(path);
//3.2 执行一个get请求
HttpResponse response = client.execute(get);
//4 获取服务器的返回状态码
int code = response.getStatusLine().getStatusCode();
if(code == 200){
//5获取服务器返回数据 以流的形式返回
InputStream inputStream = response.getEntity().getContent();
//6 把流转换成字符串
String content = StreamTools.readStream(inputStream);

showToast(content);
}



} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}.start();



}


//post 提交数据
public void click2(View v){

new Thread(){public void run(){
try {
//1 拿到用户名和用户密码
String name = et_username.getText().toString().trim();
String pwd = et_password.getText().toString().trim();
String path = "http://192.168.11.73:8080/login/LoginServlet";

//2 用 httpClient方式提交灵数据
DefaultHttpClient client = new DefaultHttpClient();

//3.1 准备post 请求
HttpPost post = new HttpPost(path);
//3.1.0 NameValuePair
List lists = new ArrayList();
//3.1.1准备NameValuePair实际上就是我们要提交的用户名和密码   key是服务器key:username
BasicNameValuePair nameValuePair = new BasicNameValuePair("username", name);
BasicNameValuePair pwdValuePair = new BasicNameValuePair("password", pwd);
//3.1.2 把nameValuePair 和  pwdValuePair 加入到集合中
lists.add(nameValuePair);
lists.add(pwdValuePair);
//3.1.3 准备entity
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(lists);
//3.2 准备post方式提交正文 以实体形式准备(建值对形式)
post.setEntity(entity);
//3.3 执行一个get请求
HttpResponse response = client.execute(post);
//4 获取服务器的返回状态码
int code = response.getStatusLine().getStatusCode();
if(code == 200){
//5获取服务器返回数据 以流的形式返回
InputStream inputStream = response.getEntity().getContent();
//6 把流转换成字符串
String content = StreamTools.readStream(inputStream);

showToast(content);
}


} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();




}}.start();

}

//封装的提交更新UI函数(方法)
public void showToast(final String content){

runOnUiThread(new Runnable() {

@Override
public void run() {
// //该方法一定是执行主线程
Toast.makeText(getApplicationContext(), content, 1).show();

}
});


}




}





android 6.0 以后不能用httpclient方法要手动加载包

更多相关文章

  1. android事件分发机制一
  2. Android四大组件Service之bindService启动
  3. Android按返回键退出程序
  4. android EditText 只能输入无法删除的解决方法
  5. android 按两次返回键退出
  6. android.database.CursorIndexOutOfBoundsException:Index -1 re
  7. Android(安卓)编程下 Touch 事件的分发和消费机制
  8. android之有返回结果跳转intent
  9. Android中文API(128) —— HandlerThread

随机推荐

  1. 【Android】Navigation初试-官方demo分析
  2. csr8811蓝牙芯片porting总结
  3. activity启动模式
  4. Android 2.2兼容性移植
  5. 怎么在android的XML文件里加入凝视
  6. 关于android中px,dip,dp,sp等单位
  7. MPAndroidChart项目实战(八)——自定义分段
  8. Eclipse中导入Android工程
  9. 网格布局(计算器界面设计)
  10. Android(安卓)自定义消息右上角的数字提