Android客户端与J2EE服务器的互联
16lz
2021-01-23
Android客户端
LoginActivity.java:
//给Servlet传值:package com.activity;import java.io.BufferedReader;import java.io.DataOutputStream;import java.io.InputStream;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URL;import java.net.URLEncoder;import org.apache.http.protocol.HttpContext;import android.app.Activity;import android.app.AlertDialog;import android.app.AlertDialog.Builder;import android.content.DialogInterface;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class LoginActivity extends Activity {//声明Button实例private Button btnCancel,btnLogin;private EditText userEditText,pwdEditText;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.http1);btnCancel=(Button)findViewById(R.id.cancelButton);btnLogin=(Button)findViewById(R.id.loginButton);userEditText=(EditText)findViewById(R.id.userEditText);pwdEditText=(EditText)findViewById(R.id.pwdEditText);//绑定监听事件btnLogin.setOnClickListener(new ButtonConfirm());btnCancel.setOnClickListener(new ButtonCancel());}/** * 用以显示对话框 */private void showDialog(String msg){AlertDialog.Builder builder=new Builder(LoginActivity.this);builder.setMessage(msg) .setCancelable(false) .setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int id) {}});AlertDialog alert=builder.create();alert.show();}/** * 取消 */class ButtonCancel implements OnClickListener{@Overridepublic void onClick(View arg0) {finish();}}/** * 登陆 */class ButtonConfirm implements OnClickListener{@Overridepublic void onClick(View v) {//获取用户名和密码String userName=userEditText.getText().toString();String pwd=pwdEditText.getText().toString();//登陆login(userName,pwd);}}//通过用户名密码查询,发送Post请求,获取结果protected void login(String username,String password){System.out.println("username="+username+" password="+password);//请求URLString urlStr="http://10.0.2.2:8080/Network/servlet/LoginServlet";//获得的数据String resultData="";try{//实例化uRlURL url=new URL(urlStr);//获得HttpConnection连接实例HttpURLConnection conn=(HttpURLConnection)url.openConnection();/************************此段代码去掉然后将urlStr后面加上?username=..**参数的话就可以直接成为Get传值***************///因为这个是post请求,需要设置为trueconn.setDoOutput(true);conn.setDoInput(true);//设置以Post方式conn.setRequestMethod("POST");//Post请求不能使用缓存conn.setUseCaches(false);conn.setInstanceFollowRedirects(true);//配置本次连接的Content-type,配置为application/x-www.form-urlencodedconn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");DataOutputStream out=new DataOutputStream(conn.getOutputStream());//上传的参数String content="username="+URLEncoder.encode(username,"gb2312")+"&password="+password;System.out.println("conent--->"+"username="+URLEncoder.encode(username,"gb2312")+"&password="+password);//写入流中out.writeBytes(content);//刷新,关闭out.flush();out.close();/*****************************************************************/ BufferedReader buffer=new BufferedReader(new InputStreamReader(conn.getInputStream()));String inputLine=null;while((inputLine=buffer.readLine())!=null){resultData+=inputLine+" ";}System.out.println("resultData--->"+resultData.toString().trim());Toast.makeText(LoginActivity.this, "登陆成功!!!"+resultData, 10000).show();showDialog(resultData);//断开连接conn.disconnect();}catch(Exception e){showDialog(e.getMessage());}}}
AndroidManifest.xml:
<uses-permissionandroid:name="android.permission.INTERNET"></uses-permission>
http1.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:stretchColumns="1" > <TableRow > <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户名称" /> <EditText android:id="@+id/userEditText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" /> </TableRow> <TableRow > <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密码" /> <EditText android:id="@+id/pwdEditText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:password="true" android:text="" /> </TableRow> <TableRow android:gravity="right" > <Button android:id="@+id/cancelButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="取消" /> <Button android:id="@+id/loginButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登陆" /> </TableRow> </TableLayout></LinearLayout>
J2EE网络端
LoginServlet.java
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 LoginServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {System.out.println("22222222222222");//获取用户名和密码String username=request.getParameter("username");String password=request.getParameter("password");System.out.println("username="+username+" password="+password);//设置类型response.setContentType("text/html");//设置字符编码集response.setCharacterEncoding("utf-8");//获取打印输出流 PrintWriter out=response.getWriter();String msg=null;if(username.equals("a")&&password.equals("1")){msg="登陆成功";}else{msg="登陆失败";}//返回给客户端out.print(msg);out.flush();out.close();}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request,response);}}
Web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"><servlet><description>This is the description of my J2EE component</description><display-name>This is the display name of my J2EE component</display-name><servlet-name>LoginServlet</servlet-name><servlet-class>LoginServlet</servlet-class></servlet><servlet-mapping><servlet-name>LoginServlet</servlet-name><url-pattern>/servlet/LoginServlet</url-pattern></servlet-mapping><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>
更多相关文章
- Android 浏览器的开发实例分享
- Android TextView Marquee的应用实例详解
- Android 应用指定浏览器开发实例
- Netty多语言(Java、Android 、C#、WebSocket)通信实例Demo (三)Andro
- Android实例收藏
- android开发之设置Edittext密码的方法
- Android 中级教程之------Android MediaPlayer播放mp3的实例!
- Netty多语言(Java、Android 、C#、WebSocket)通信实例Demo (一)概述
- android的init实例