Android游戏开发之服务器端

首先我不知道这篇文章发到Android开发板块是否适合的,因为只部分内容大多数是有关Google appEngine的,但因为这是AppEngine和Android的配合开发,故对Android开发者也有一定的参考意义,故发到这里来了。

其次,在写文章前先谢谢UCWeb Android的产品经理huang,谢谢他的赏识,虽然最后因为一些个人原因我没留在UC,但从笔试和面试的过程中,我发觉UC真是一家严谨、惜才的公司,搞Android的朋友如果有兴趣,可以考虑应聘一下UC的社招。


OK,进入正题!

在做游戏开发的时候,很多游戏都会提供一个全球分数排名的的功能,这就需要使用服务器了,当然,你可以选择搭建自己的服务器,但从成本,稳定性和开发周期上看,我认为Google AppEngine是个很不错的选择,现在Google AppEngine关于Python的参考资料比较详尽,Java的就比较少,所以我把我写的小Demo放上来,做抛砖引玉之用,因为我也是初学者,所以demo一切从简,大侠请不要见笑哦!

 

Demo1:对象的存储:

 

步骤1.定义JavaBean:

 

package com.ray.test;

import java.util.Date;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

//标记对象可以持久化
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Employee {

	//标记主键,作以后查询之用
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private String firstName;
	
	@Persistent
	private String lastName;
	
	@Persistent
	private Date birthDate;
	
	public Employee(String firstName, String lastName, Date birthDate) {
		this.firstName = firstName;
		this.lastName = lastName;
		this.birthDate = birthDate;
	}

	public String getFirstName() {
		return firstName;
	}   
}

 

 步骤2.存取Bean(可以写在Servlet的doGet()或者doPost()里面):

 

package com.ray.test;

import java.io.IOException;
import java.util.Date;
import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class HelloServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, 
			HttpServletResponse resp)throws IOException {
		
		resp.setContentType("text/plain");
		
		Employee e = new Employee("Alfred", "Smith", new Date());
		//获取用以存取对象的PersistenceManager
	    PersistenceManager pm = PMF.get().getPersistenceManager();
    
        try {
        	//存储对象
        	resp.getWriter().println("saving object!");
            pm.makePersistent(e);     
        }catch(Exception ex){
        	resp.getWriter().println("cannot persistent!");
        	ex.printStackTrace();	
        } 
        finally {
            pm.close();
        }
        
        try{
        	//再次获取用以存取对象的PersistenceManager
        	pm = PMF.get().getPersistenceManager();
        	//由主键检索对象,Alfred之前被定义为主键的
	        Employee newE = pm.getObjectById(Employee.class, "Alfred");
	        //读取新对象的信息检测对象检索是否成功
	        resp.getWriter().println("Employee:"+newE.getFirstName());
        }catch(Exception ex){
        	ex.printStackTrace();
        	resp.getWriter().println("cannot get data!");
        	pm.close();
        }
	}

	@Override
	protected void doPost(HttpServletRequest req, 
			HttpServletResponse resp)throws ServletException, IOException {
		this.doGet(req, resp);
	}
}

 

 其中PMF类的代码如下,没看错应该是个单例模式:

 

package com.ray.test;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManagerFactory;

public final class PMF {
    private static final PersistenceManagerFactory pmfInstance =
        JDOHelper.getPersistenceManagerFactory("transactions-optional");

    private PMF() {}

    public static PersistenceManagerFactory get() {
        return pmfInstance;
    }
}

 

 Demo2.使用Google 账户认证服务:

 

package com.ray.test;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

public class GoogleAccount extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//创建用户服务
		UserService userService = UserServiceFactory.getUserService();
		
      	String thisURL = req.getRequestURI();
      	//通过请求对象的 getUserPrincipal() 方法测试用户是否使用 Google 帐户登录
        if (req.getUserPrincipal() != null) {
        	//如果有登录,则创建Login地址
        	resp.getWriter().println("<p>Hello, " +
                                         req.getUserPrincipal().getName() +
                                         "!  You can <a href=\"" +
                                         userService.createLogoutURL(thisURL) +
                                         "\">sign out</a>.</p>");
        } else {
        	//如果没有登录,则创建Login地址
        	resp.getWriter().println("<p>Please <a href=\"" +
                                         userService.createLoginURL(thisURL) +
                                         "\">sign in</a>.</p>");
        }
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}	
}

 

 手机访问登录效果:

Android游戏开发之服务器端

 

需要特别指出的是,Google通过在web.xml里面配置权限来区分登录者的权限,可以用下面的代码来配置安全常量从而划定只能由admin访问的区域---admin路径下的所有页面:

 

	<security-constraint>
        <web-resource-collection>
            <url-pattern>/admin/*</url-pattern>
        </web-resource-collection>
        <auth-constraint>
            <role-name>admin</role-name>
        </auth-constraint>
    </security-constraint>

 参考资料:

http://code.google.com/intl/zh-CN/appengine/docs/java/overview.html

你可能感兴趣的:(游戏,应用服务器,android,Google,GAE)