如何向Android客户端发送请求并传递(POST数据用于注册和登录)数据到服务器(应用程序引擎)?

时间:2014-06-16 23:27:00

标签: android google-app-engine

1.我已经使用Google app engine和android studio创建了这个项目。我的服务器设置包含用户帐户,注册信息,登录信息。和其他用户详细信息/数据。我不想使用Google凭据登录。如何从Android向服务器发出请求并传递(用于注册和登录的数据)数据?我是否会定义一个仅处理HTTP传输的类并使用它来创建服务对象?我见过使用这种服务对象的代码示例。我不知道该怎么做HTTPRequestInitializer?

Tictactoe.Builder builder = new Tictactoe.Builder(
AndroidHttp.newCompatibleTransport(), new GsonFactory(), null);
service = builder.build();”
//TotoClass.java
public Account account() {
return new Account();
}
  1. 这是我的API类
  2. / **     *“帐户”方法集合。     * /

        public class Account {
        /**
        * Create a request for the method “account.adduser”.
        *
        * This request holds the parameters needed by the Toto server. After setting any optional
        * parameters, call the {@link Adduser#execute()} method to invoke the remote operation.
        *
        * @return the request
        */
        public Adduser adduser() throws java.io.IOException {
        Adduser result = new Adduser();
        initialize(result);
        return result;
        }
        public class Adduser extends TotoRequest {
        private static final String REST_PATH = “adduser”;
        /**
        * Create a request for the method “account.adduser”.
        *
        * This request holds the parameters needed by the the Toto server. After setting any optional
        * parameters, call the {@link Adduser#execute()} method to invoke the remote operation.
        * {@link
        * Adduser#initialize(com.google.api.client.googleapis.services.AbstractGoogleClientRequest)} must
        * be called to initialize this instance immediately after invoking the constructor.
        *
        * @since 1.13
        */
        protected Adduser() {
        super(Toto.this, “POST”, REST_PATH, null, TotoBoolResponse.class);
        }
        @Override
        public Adduser setAlt(String alt) {
        return (Adduser) super.setAlt(alt);
        }
        @Override
        public Adduser setFields(String fields) {
        return (Adduser) super.setFields(fields);
        }
        @Override
        public Adduser setKey(String key) {
        return (Adduser) super.setKey(key);
        }
        @Override
        public Adduser setOauthToken(String oauthToken) {
        return (Adduser) super.setOauthToken(oauthToken);
        }
        .....
        @com.google.api.client.util.Key
        private String lastname;
        /**
        */
        public String getLastname() {
        return lastname;
        }
        public Adduser setLastname(String lastname) {
        this.lastname = lastname;
        return this;
        }
        @com.google.api.client.util.Key
        private String password;
        /**
        */
        public String getPassword() {
        return password;
        }
        public Adduser setPassword(String password) {
        this.password = password;
        return this;
        }
        @com.google.api.client.util.Key
        private String email;
        /**
        */
        public String getEmail() {
        return email;
        }
        public Adduser setEmail(String email) {
        this.email = email;
        return this;
        }
        @com.google.api.client.util.Key
        private String firstname;
        /**
        */
        public String getFirstname() {
        return firstname;
        }
        public Adduser setFirstname(String firstname) {
        this.firstname = firstname;
        return this;
        }
        @Override
        public Adduser set(String parameterName, Object value) {
        return (Adduser) super.set(parameterName, value);
        }
        }
        /**
        * Create a request for the method “account.login”.
        *
        * This request holds the parameters needed by the Toto server. After setting any optional
        * parameters, call the {@link Login#execute()} method to invoke the remote operation.
        *
        * @return the request
        */
        public Login login() throws java.io.IOException {
        Login result = new Login();
        initialize(result);
        return result;
        }
        public class Login extends TotoRequest {
        private static final String REST_PATH = “login”;
        /**
        * Create a request for the method “account.login”.
        *
        * This request holds the parameters needed by the the Toto server. After setting any optional
        * parameters, call the {@link Login#execute()} method to invoke the remote operation. {@link
        * Login#initialize(com.google.api.client.googleapis.services.AbstractGoogleClientRequest)} must
        * be called to initialize this instance immediately after invoking the constructor.
        *
        * @since 1.13
        */
        protected Login() {
        super(Toto.this, “POST”, REST_PATH, null, TotoBoolResponse.class);
        }
        @Override
        public Login setAlt(String alt) {
        return (Login) super.setAlt(alt);
        }
        @Override
        public Login setFields(String fields) {
        return (Login) super.setFields(fields);
        }
        @Override
        public Login setKey(String key) {
        return (Login) super.setKey(key);
        }
        @Override
        public Login setOauthToken(String oauthToken) {
        return (Login) super.setOauthToken(oauthToken);
        }
        ….
    
        @com.google.api.client.util.Key
        private String password;
        /**
        */
        public String getPassword() {
        return password;
        }
        public Login setPassword(String password) {
        this.password = password;
        return this;
        }
        @com.google.api.client.util.Key
        private String email;
        /**
        */
        public String getEmail() {
        return email;
        }
        public Login setEmail(String email) {
        this.email = email;
        return this;
        }
        @Override
        public Login set(String parameterName, Object value) {
        return (Login) super.set(parameterName, value);
        }
        }
        }
    

    3。这是我的帐户类   // AccountClass

      /**
        * Model definition for Account.
        * This is the Java data model class that specifies how to parse/serialize into the JSON that is
        * transmitted over HTTP when working with the Toto. For a detailed explanation see:
        * http://code.google.com/p/google-http-java-client/wiki/JSON
        * */
        @SuppressWarnings(“javadoc”)
        public final class Account extends com.google.api.client.json.GenericJson {
        /**
        * The value may be {@code null}.
        */
        @com.google.api.client.util.Key
        private String email;
        /**
        * The value may be {@code null}.
        */
        @com.google.api.client.util.Key
        private String firstname;
        /**
        * The value may be {@code null}.
        */
        @com.google.api.client.util.Key
        private String friendlist;
        /**
        * The value may be {@code null}.
        */
        @com.google.api.client.util.Key @com.google.api.client.json.JsonString
        private Long id;
        /**
        * The value may be {@code null}.
        */
        @com.google.api.client.util.Key
        private String lastname;
        /**
        * The value may be {@code null}.
        */
        @com.google.api.client.util.Key
        private String pwdhash;
        /**
        * @return value or {@code null} for none
        */
        public String getEmail() {
        return email;
        }
        /**
        * @return value or {@code null} for none
        */
        public String getFirstname() {
        return firstname;
        }
        /**
        * @return value or {@code null} for none
        */
        public String getFriendlist() {
        return friendlist;
        }
        /**
        * @return value or {@code null} for none
        */
        public Long getId() {
        return id;
        }
        /**
        * @param id id or {@code null} for none
        */
        public Account setId(Long id) {
        this.id = id;
        return this;
        }
        ………
        /**
        * @return value or {@code null} for none
        */
        public String getPwdhash() {
        return pwdhash;
        }
        /**
        * @param pwdhash pwdhash or {@code null} for none
        */
        public Account setPwdhash(String pwdhash) {
        this.pwdhash = pwdhash;
        return this;
        }
        @Override
        public Account set(String fieldName, Object value) {
        return (Account) super.set(fieldName, value);
        }
        @Override
        public Account clone() {
        return (Account) super.clone();
        }
        }
    
    1. 这是我的Android ##登录电话

      doInBackground(String… params){
      try {
      Account account = new Account();
      account.setEmail(params[0]);
      account.setPwdhash(params[1]);
      Toto apiServiceHandle = AppConstants.getApiServiceHandle();
      Log.e(LOG_TAG, “apiServiceHandle”);
      TotoBoolResponse response ;
      apiServiceHandle.account().login().setEmail(“test@yahoo.com”);
      apiServiceHandle.account().login().setPassword(“test”);
      response = apiServiceHandle.account().login().execute();
      }
      
    2. 这就是我调用api来发布数据的方法,但它总是返回空白{}。我非常感谢你的例子。

1 个答案:

答案 0 :(得分:0)

我有类似的问题。只需在登录(长参数)功能中添加参数即可。