android,HttpPost发送数据到服务器

来源:互联网 发布:淘宝联盟批量链接工具 编辑:程序博客网 时间:2024/06/05 15:44
/** * 采用httpclient post数据到服务器 */public void loginByClientPost() {String password = et_password.getText().toString().trim();String name = et_username.getText().toString().trim();if (TextUtils.isEmpty(name) || TextUtils.isEmpty(password)) {Toast.makeText(this, "用户名密码不能为空", 1).show();return;}try {// 1.创建一个浏览器HttpClient client = new DefaultHttpClient();// 2.准备一个连接HttpPost post = new HttpPost("http://192.168.1.100:8080/web/LoginServlet");// 要向服务器提交的数据实体List<NameValuePair> parameters = new ArrayList<NameValuePair>();parameters.add(new BasicNameValuePair("name", name));parameters.add(new BasicNameValuePair("password", password));UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters,"utf-8");post.setEntity(entity);// 3.敲回车HttpResponse response = client.execute(post);int code = response.getStatusLine().getStatusCode();if (code == 200) {InputStream is = response.getEntity().getContent();String result = StreamTools.readFromStream(is);Toast.makeText(this, result, 1).show();} else {Toast.makeText(this, "服务器异常", 1).show();}} catch (Exception e) {e.printStackTrace();Toast.makeText(this, "client post 失败", 1).show();}}


学会这个。就可以快速的访问某个需要传参数的网址了。参数名字,要用浏览器抓包工具,看数据包就可以晓得了。可以做些抢票,秒杀工具等。

0 0