通过get方式提交表单(注意大写)
建立url连接添加name+pass(注意此时要将中文的name用URLEncoder.encode(name)解析
public class myThreadextends Thread{
@Override
public void run(){
TextView tv1 = (TextView)findViewById(R.id.name);
String name = tv1.getText().toString();
TextView tv2 = (TextView)findViewById(R.id.pass);
String pass = tv2.getText().toString();
try {
URL url = new URL("http://169.254.165.163:8080/Web2/servlet/Login?"+"name="+URLEncoder.encode(name,"utf-8")+"&pass="+pass);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
conn.setReadTimeout(8000);
conn.setConnectTimeout(8000);
if(conn.getResponseCode()==200){
InputStream is = conn.getInputStream();
byte[] b = new byte[1024];
int len;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while((len=is.read(b))!=-1){
bos.write(b,0,len);
}
String str = bos.toString("UTF-8");
Message msg = new Message();
msg.obj = str;
handler.sendMessage(msg);
}
}catch (Exception e) {
e.printStackTrace();
}
}
}
通过post方式提交表单
不需要在请求头中写name和pass
设置输入请求,添加post请求头中自动添加的属性
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
os.write(content.getBytes());
conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length",content.length()+"");
此时流里面还没有东西要将数据写入输出流中
需要获取输出流写入数据并打开输出流
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
os.write(content.getBytes());
例子:
public class myThreadextends Thread{
@Override
public void run(){
TextView tv1 = (TextView)findViewById(R.id.name);
String name = tv1.getText().toString();
TextView tv2 = (TextView)findViewById(R.id.pass);
String pass = tv2.getText().toString();
try {
URL url = new URL("http://169.254.165.163:8080/Web2/servlet/Login");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setReadTimeout(8000);
conn.setConnectTimeout(8000);
String content = "name="+URLEncoder.encode(name)+"&pass="+pass;
conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", content.length()+"");
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
os.write(content.getBytes());
if(conn.getResponseCode()==200){
InputStream is = conn.getInputStream();
byte[] b = new byte[1024];
int len;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while((len=is.read(b))!=-1){
bos.write(b,0,len);
}
String str = bos.toString("UTF-8");
Message msg = new Message();
msg.obj = str;
handler.sendMessage(msg);
}
}catch (Exception e) {
e.printStackTrace();
}
}
}
总结:注意在服务器端获取name中文问题需要转码
(get方式不需要,查了百度说是tomcat问题)
Android编码用的是iso8859-1需要转化才可以匹配
Stringname = request.getParameter("name");
String pass = request.getParameter("pass");
name =new String(name.getBytes("iso8859-1"),"utf-8");
System.out.println(name);
System.out.println(pass);
System.out.println("----------------------------------");
OutputStream os = response.getOutputStream();
if("三十".equals(name) && "123".equals(pass)){
os.write("登陆成功".getBytes("utf-8"));
}else {
os.write("登陆失败".getBytes("utf-8"));
}
再插一句tomcat部署
http://blog.youkuaiyun.com/woshizhangliang999/article/details/50943268