android 使用HttpURLConnection方式提交get/post请求
package com.zhangbz.submitdata.Utils; import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder; import android.util.Log; public class NetUtils { private static final String TAG = "NetUtils";
/**
* 使用post的方式登录
* @param userName
* @param password
* @return
*/
public static String loginOfPost(String userName, String password){
HttpURLConnection conn = null;
try {
URL url = new URL("http://10.0.2.2:8080/serverzhangbz/servlet/LoginServlet?"); conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST");
conn.setReadTimeout(10000); //连接的超时时间
conn.setReadTimeout(5000); //读数据的超时时间
conn.setDoOutput(true);//必须设置此方法,允许输出
//conn.setRequestProperty("content-Length", 234); //设置请求头消息,可以设置多个 //post请求的参数
String data = "username=" + userName + "&password=" + password; //获得一个输出流,用于向服务器写数据,默认情况下,系统不予许向服务器输出内容
OutputStream out = conn.getOutputStream();
out.write(data.getBytes());
out.flush();
out.close(); int responseCode = conn.getResponseCode();
if(responseCode == 200) {
InputStream is = conn.getInputStream();
String state = getSringFromInputStream(is);
Log.i(TAG, state);
return state;
} else {
Log.i(TAG, "访问失败:" + responseCode);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(conn != null) {
conn.disconnect(); //关闭连接
}
} return null;
} /**
* 使用get的方式登录
* @param userName
* @param password
* @return
*/
public static String logOfPost(String userName, String password) {
HttpURLConnection conn = null; //局部变量在使用时必须进行初始化
try {
String data = "username=" + URLEncoder.encode(userName) + "&password=" + URLEncoder.encode(password);
URL url = new URL("http://10.0.2.2:8080/serverzhangbz/servlet/LoginServlet?" + data);
conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET");//get或者post必须得全大写
conn.setReadTimeout(10000);//连接的超时时间
conn.setReadTimeout(5000);//读数据的超时连接 int responseCode = conn.getResponseCode();
if(responseCode == 200) {
InputStream is = conn.getInputStream();
String state = getSringFromInputStream(is);
Log.i(TAG, state);
return state;
} else {
Log.i(TAG, "访问失败:" + responseCode);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if(conn != null) {
conn.disconnect(); //关闭连接
}
} return null; } /**
* 根据流返回一个字符串信息
* @param is
* @return
* @throws IOException
*/
private static String getSringFromInputStream(InputStream is) throws IOException{ ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1; while((len = is.read(buffer)) != -1){
baos.write(buffer, 0, len);
}
is.close();
String html = baos.toString(); //把流中的数据转换成字符串,采用的编码是:utf-8 //String html = new String(baos.toByteArray(), "GBK"); baos.close();
return html;
}
}
android 使用HttpURLConnection方式提交get/post请求的更多相关文章
- Android 采用post方式提交数据到服务器
接着上篇<Android 采用get方式提交数据到服务器>,本文来实现采用post方式提交数据到服务器 首先对比一下get方式和post方式: 修改布局: <LinearLayout ...
- Android 使用Post方式提交数据(登录)
在Android中,提供了标准Java接口HttpURLConnection和Apache接口HttpClient,为客户端HTTP编程提供了丰富的支持. 在HTTP通信中使用最多的就是GET和POS ...
- Android 使用Post方式提交数据
在Android中,提供了标准Java接口HttpURLConnection和Apache接口HttpClient,为客户端HTTP编程提供了丰富的支持. 在HTTP通信中使用最多的就是GET和POS ...
- Android 使用HttpClient方式提交POST请求
final String username = usernameEditText.getText().toString().trim(); final String password = passwr ...
- Android 使用HttpClient方式提交GET请求
public void httpClientGet(View view) { final String username = usernameEditText.getText().toString() ...
- Android 采用get方式提交数据到服务器
首先搭建模拟web 服务器,新建动态web项目,servlet代码如下: package com.wuyudong.web; import java.io.IOException; import ja ...
- android 通过post方式提交数据的最简便有效的方法
public boolean post(String username, String password) throws Exception { username = URLEncoder.encod ...
- IE11在使用get方式提交没有进行请求的bug问题
在做iemsc项目的时候,测试提交了一个bug问题,在发布新闻成功后,自动刷新列表的时候,不进行刷新,但是在谷歌上面又不会出现这种问题, 原因: 发现请求的时候用的get请求,因为不同的浏览器的请求机 ...
- android 之httpclient方式提交数据
HttpClient: 今天实战下httpclient请求网络json数据,解析json数据返回信息,显示在textview, 起因:学校查询饭卡余额,每次都要访问校园网(内网),才可以查询,然后才是 ...
随机推荐
- 二叉平衡查找树AvlTree(C实现)
二叉平衡查找树即是一棵树中所有节点的左右子树高度差不超过1的查找树 头文件—————————————————————————————— #ifndef _AVLTREE_H_ #define _AVL ...
- mongodb 监控分析命令
1. bin/mongostate 2.开启慢查询用于调试 正式要关闭 可以在客户端调用db.setProfilingLevel(级别) 命令来实时配置.可以通过db.getProfilingLeve ...
- DDD:一个朋友对领域驱动的小结
首先我在一家老板有点关系的小软件公司带领一帮工作一两年的程序员做项目,这里要特别强调的是做项目(差不多是外包,只不过客户群体比较固定),项目就是今天项目A是这个逻辑,明天项目B是那个逻辑,两者之间的业 ...
- 利用react native创建一个天气APP
我们将构建一个实列程序:天气App,(你可以在react native 中创建一个天气应用项目),我们将学习使用并结合可定义模板(stylesheets).盒式布局(flexbox).网络通信.用户输 ...
- Hadoop入门进阶课程8--Hive介绍和安装部署
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,博主为石山园,博客地址为 http://www.cnblogs.com/shishanyuan ...
- IEE数据库kill指定条件的进程
需求:IEE数据库临时需要添加一个监控,将command为sleep,time>1800,info为null的进程自动杀掉. 1.杀进程脚本ieekill.sh内容如下 #!/bin/bash ...
- Direct3D11学习:(零)常见问题及解决方法整理
转载请注明出处:http://www.cnblogs.com/Ray1024 一.概述 在D3D11学习的这个系列中,单独写一篇文章来记录自己学习过程中遇到的问题及最后的解决方法. 这篇文章的目的 ...
- Lambda表达式和匿名内部类(I)
本文git地址 前言 Java Labmda表达式的一个重要用法是简化某些匿名内部类(Anonymous Classes)的写法.实际上Lambda表达式并不仅仅是匿名内部类的语法糖,JVM内部是通过 ...
- sql server2008安装错误(无法处理异常)
我在安装sql server2008时出现安装错误,无法处理的异常,问题如下: 解决方法:在地址栏输入C:\Users\Administrator\AppData\Local ,找到Microsoft ...
- LeetCode130:Surrounded Regions
题目: Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is capt ...