android后端java web_Android与web后台进行数据交互的三种方式(网络摘抄)

无论是用哪种方式使Android与web后台进行数据交互,都需要在eclipes中写一个完整的操作流程,在这里,我是写了一个登录。(没有从数据库进行判断,而是直接把用户名和密码定死了),下面就是我的后台web端。你可以根据你的不同需求在jsp界面用你所需要的提交方式来写

login,jsp:

pageEncoding="UTF-8"%>

Insert title here

用户名:

密码:

LoginServlet:

package com.lxj.servlet;

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class Login extends HttpServlet{

//@Override

//protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

//String uname=req.getParameter("uname");

//String upass=req.getParameter("upass");

//

//String a=null;

//if("admin".equals(uname) && "123".equals(upass)){

//a="succes";

//System.out.println(a);

//}else{

//a="fail";

//System.out.println(a);

//}

//PrintWriter pw=resp.getWriter();

//pw.write(a);

//pw.close();

//}

@Override

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

String uname=req.getParameter("uname");

String upass=req.getParameter("upass");

String a=null;

if("admin".equals(uname) && "123".equals(upass)){

a="succes";

System.out.println(a);

}else{

a="fail";

System.out.println(a);

}

PrintWriter pw=resp.getWriter();

pw.write(a);

pw.close();

}

}

最后,还要记得在web.xml里面配置跳转路径

login

com.lxj.servlet.Login

login

/login.do

第一:GET提交方式:

activity_main.xml:

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context="com.example.android_getandpost.MainActivity">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="请输入用户名"

android:id="@+id/uname"

/>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="请输入密码"

android:id="@+id/upass"

/>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="登陆(Get)"

android:onClick="LoginGet"

/>

MainActivity:

package com.example

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值