【Android学习】第七章 · 通过GET和POST方式提交表单

通过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.csdn.net/woshizhangliang999/article/details/50943268

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值