码迷,mamicode.com
首页 > 移动开发 > 详细

android 通过post发送数据 完成客户端登陆模块

时间:2014-12-24 18:28:00      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:android post 郑传余

1.get是从服务器上获取数据,post是向服务器传送数据。
2.get是把参数数据队列加到提交表单的 ACTION属性所指的URL中,值和表单内各个字段一一对应,在URL中可以看到。post是通过HTTPpost机制,将表单内各个字段与其内容放置在HTML HEADER内一起传送到ACTION属性所指的URL地址。用户看不到这个过程。
3.对于get方式,服务器端用 Request.QueryString获取变量的值,对于post方式,服务器端用Request.Form获取提交的数据。
4.get 传送的数据量较小,不能大于2KB。post传送的数据量较大,一般被默认为不受限制。但理论上,IIS4中最大量为80KB,IIS5中为100KB。
5.get安全性非常低,post安全性较高。

优酷:http://v.youku.com/v_show/id_XODU1MzExMjUy.html

爱奇艺:http://www.iqiyi.com/w_19rsechp2t.html#vfrm=8-7-0-1

以下是通过post发送数据,完成在客户端登陆模块,当然这得搭建服务器,如果有对服务器不熟悉的同学,可以发送邮件到whsgzcy@foxmail.com,很乐意帮助大家,以下是MainActivity.java,布局文件不再给出;



public class MainActivity extends Activity {

private EditText ed_name;

private EditText ed_password;

private Button btn_login;


@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

ed_name = (EditText)findViewById(R.id.username);

ed_password = (EditText)findViewById(R.id.upassword);

btn_login = (Button)findViewById(R.id.btn_login);

btn_login.setOnClickListener(new MyButtonListener());

}

class MyButtonListener implements OnClickListener{


@Override

public void onClick(View v) {

String name = ed_name.getText().toString();

String password = ed_password.getText().toString();

PostThread pt = new PostThread(name, password);

pt.start();

}

}

/*登陆成功放回服务器字符

* 登陆失败则放回不了???

*

*/

//post 方法 发送数据到服务器

class PostThread extends Thread{


String name;

String password;

public PostThread(String name, String password) {

super();

this.name = name;

this.password = password;

}


@Override

public void run() {

super.run();

HttpClient httpClient = new DefaultHttpClient();

String url = "http://192.168.191.1:8080/2.5/LoginServlet";

HttpPost httpPost = new HttpPost(url);

//NameValuePair对象,代表了一个需要发往服务器的键值对

NameValuePair pair1 = new BasicNameValuePair("username", name);

NameValuePair pair2 = new BasicNameValuePair("upassword", password);

//使用集合类将其包装起来

ArrayList<NameValuePair> pairs = new ArrayList<NameValuePair>();

pairs.add(pair1);

pairs.add(pair2);

//请求体代表了了请求对象

try {

HttpEntity requestEntity = new UrlEncodedFormEntity(pairs);

//将请求体放置在请求对象中

httpPost.setEntity(requestEntity);

HttpResponse response = httpClient.execute(httpPost);

if(response.getStatusLine().getStatusCode() == 200){

HttpEntity entity = response.getEntity();

BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent()));

String reslut = reader.readLine();

System.out.println(reslut);

Log.d("HttpPost","result"+reslut);

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

}


本文出自 “7851921” 博客,请务必保留此出处http://7861921.blog.51cto.com/7851921/1595032

android 通过post发送数据 完成客户端登陆模块

标签:android post 郑传余

原文地址:http://7861921.blog.51cto.com/7851921/1595032

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!