Android实现服务器登录

Android实现服务器登录

一、使用MyEclipse创建一个servlet service project服务;(

在创建服务器时一定要创建servlet service project,不能创建servlet project)代码如下:

package login;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.HashMap;

import java.util.Map;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

public class AndroidLogin extends HttpServlet {

public AndroidLogin() {

super();

}

public void destroy() {

super.destroy();

}

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doPost(request, response);

}

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html");  

        PrintWriter out = response.getWriter();  

        Boolean flag =false;    

        String userName = request.getParameter("phone");  

        String password = request.getParameter("password");   

        if(userName.equals("123123")&&password.equals("123123"))   {

            flag =true;  }

        else {flag = false;}

        System.out.println("userName:"+userName+"password:"+password);

        out.print(flag);  

        out.flush();  

        out.close();

}

}

二、修改WebRoot/WEB-INF/web.xml文件中的代码,加上如下代码:

<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>AndroidLogin</servlet-name>

    <servlet-class>login.AndroidLogin</servlet-class>

  </servlet>

  <servlet-mapping>

    <servlet-name>AndroidLogin</servlet-name>

    <url-pattern>/login/AndroidLogin</url-pattern>

</servlet-mapping>

三、修改Android的Layout界面

<?xml version="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"android:layout_width="match_parent"
    android:layout_height="match_parent">
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="phone"
        android:ems="10"
        android:id="@+id/editText3"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:ems="10"
        android:id="@+id/editText"/>

    <Button
        android:text="登录"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button"/>
</LinearLayout>

四、修改LoginDemo代码(我自己建的Login Demo,默认为MainActivity)

packagecom.example.jsondemo;

importandroid.os.Bundle;
importandroid.os.Handler;
importandroid.os.Looper;
importandroid.os.Message;
importandroid.support.annotation.Nullable;
importandroid.support.v7.app.AppCompatActivity;
importandroid.view.View;
importandroid.widget.Button;
importandroid.widget.EditText;
importandroid.widget.TextView;
importandroid.widget.Toast;
importorg.apache.http.HttpResponse;
importorg.apache.http.client.HttpClient;
importorg.apache.http.client.methods.HttpPost;
importorg.apache.http.impl.client.DefaultHttpClient;
importorg.apache.http.util.EntityUtils;
importjava.io.ByteArrayOutputStream;
importjava.io.InputStream;
importjava.net.HttpURLConnection;
importjava.net.URL;

public classLoginDemoextendsAppCompatActivity {
    privateEditTextphone;
    privateEditTextpassword;
    privateButtonlogin;
    Handlerhandler=newHandler(){
        @Override
        public void handleMessage(Message msg) {
            switch(msg.what){
                case1:
                    Toast.makeText(LoginDemo.this,"成功",Toast.LENGTH_SHORT).show();
                    break;
                default:
                    Toast.makeText(LoginDemo.this,"失败",Toast.LENGTH_SHORT).show();
                    break;
            }    }  };
    @Override
    protected void onCreate(@NullableBundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_demo);
        password= (EditText) findViewById(R.id.editText);
        login= (Button) findViewById(R.id.button);
        phone= (EditText) findViewById(R.id.editText3);
        login.setOnClickListener(newView.OnClickListener() {
            @Override
            public void onClick(View v) {
                xxx();   }    });   }
    private void xxx() {
        newThread(newRunnable() {
            @Override
            public void run() {
                String msg ="";
                try{
                    String phone1 =phone.getText().toString().trim();
                    String pw =password.getText().toString().trim();
                    String url ="http://10.0.2.2:8888/AndroidLogin/login/"+
                            "AndroidLogin?phone="+phone1+"&password="+pw+"";
                    HttpURLConnection conn = (HttpURLConnection)newURL(url).openConnection();
                    //设置请求方式,请求超时信息
                    conn.setRequestMethod("POST");
                    conn.setReadTimeout(5000);
                    conn.setConnectTimeout(5000);
                    //设置运行输入,输出:
                    conn.setDoOutput(true);
                    conn.setDoInput(true);
                    //Post方式不能缓存,需手动设置为false
                    conn.setUseCaches(false);
                    if(conn.getResponseCode() ==200) {
                        //使用httpClient发送post请求
                        HttpClient httpClient = newDefaultHttpClient();
                        HttpPost post =newHttpPost(url);
                        HttpResponse httpResponse = httpClient.execute(post);
                        String result = EntityUtils.toString(httpResponse.getEntity());
                        if(result.equals("true")){
                            Message message1 =newMessage();
                            message1.what=1;
                            handler.sendMessage(message1);
                        }else{
                            Message message1 =newMessage();
                            message1.what=0;
                            handler.sendMessage(message1);
                        }
                    }
                }catch(Exception e) {
                    e.printStackTrace();   }    }    }).start();   }   }

五、启动模拟器,分别输入123123/123123,点击“登录”按钮,效果,如图。

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值