Android提交數據到JavaWeb服務器實現登錄


之前學習Android提交數據到php服務器沒有成功,在看了兩三個星期的視頻之后,現在終於實現了與服務器的交互。雖然完成的不是PHP端的,但是在這個過程還是學到了不少東西的。現在我先來展示一下我的成果圖。

思想是:服務器端模擬數據庫中存在一條數據為:username=123,password=123的用戶。如果Android端提交的數據是用戶名和密碼都為123的數據。就返回成功,否則就返回失敗。

 

由於看的關於教程的視頻都是跟javaweb進行交互,所以自己就嘗試着搭建了javaweb服務器。在這個過程中,遇到了幾個問題:

一、tomcat與javaweb的位數不一致,32位的javaweb不能在64位的tomcat上面運行。

二、tomcat默認的端口號是8080.但是運行的時候此端口被占用。所以就在web.xml文件中更改了端口號。

解決了javaweb上面的問題就是寫一個接口給Android端。

先創建一個web工程,然后創建一個Servlet。

package com.itcast.Login;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;
	public Login(){
		super();
	}
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		System.out.println("QQ號:"+username);
		System.out.println("密碼:"+password);
		if ("123".equals(username)&&"123".equals(password)) {
			System.out.println("登錄成功");
			response.getOutputStream().write("登錄成功".getBytes("utf-8"));
			
		}else {
			System.out.println("登錄失敗");
			response.getOutputStream().write("登錄失敗".getBytes("utf-8"));
			
		}
		
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
		
	}

}

  然后利用get方式訪問服務器得到的結果是:

Android端的代碼為:

package com.itheima.login;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLEncoder;

public class LoginService {
    public static String loginByPost(String username,String password){    
        try {
            String path = "http://192.168.1.101:8090/Java/servlet/Login";
            URL url = new URL(path);
            HttpURLConnection conn =  (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(5000);
            conn.setRequestMethod("POST");
            String data = "username="+URLEncoder.encode(username)+"&password="
                    +URLEncoder.encode(password);
            System.out.println(data);
            conn.setRequestProperty("Content=Type", "application/x-wwww-form-urlencoded");
            conn.setRequestProperty("Content-length", data.length()+"");
            conn.setDoOutput(true);
            OutputStream os = conn.getOutputStream();
            os.write(data.getBytes());
            int code = conn.getResponseCode();
            System.out.println(code);
            if (code == 200) {
                InputStream is = conn.getInputStream();
                String text = StreamTools.readInputStream(is);
                return text;
            }else {
                return null;
            }
            
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("111111");
        } catch (ProtocolException e) {
            System.out.println("222222");
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("33333");
            e.printStackTrace();
        }
        return null;
    }
}

運行的結果為:

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM