lundi 6 juillet 2015

Insert query on Google App Engine from Android Client

I'm making a login/register form from an Android Client to Google App Engine.

I'm pretty confused about this since I'm very new to GAE so far...

I had this form running fine on SQLite queries, now I need to make it work with Google App Engine.

This is my Entities file on GAE module:

package com.kkoci.shairlook.backend;

import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;
import com.googlecode.objectify.annotation.Index;



/**
* Created by kristian on 01/07/2015.
*/

@Entity
public class User {
@Id
Long id;
String who;
String what;
@Index String email;
String password;
String school;

public User() {}

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getWho() {
    return who;
}

public void setWho(String who) {
    this.who = who;
}
public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}
public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getWhat() {
    return what;
}

public void setWhat(String what) {
    this.what = what;
}
}

Now I have this EndpointLogic to insert a User into DB:

@ApiMethod(name = "insertUser")
public User insertUser(User user) throws ConflictException {
//If if is not null, then check if it exists. If yes, throw an Exception
//that it is already present
    if (user.getId() != null) {
        if (findRecord(user.getId()) != null) {
            throw new ConflictException("Object already exists");
        }
    }
//Since our @Id field is a Long, Objectify will generate a unique value for us
//when we use put
    ofy().save().entity(user).now();
    return user;
}

I'm trying to make it work from this AsyncTask class:

public class EndpointsAsyncTaskInsert extends AsyncTask<String, Void, User> implements GoogleClientRequestInitializer {
private static UserEndpoint myApiService = null;
private Context context;

EndpointsAsyncTaskInsert(Context context) {
    this.context = context;
}

@Override
public void initialize(AbstractGoogleClientRequest<?> abstractGoogleClientRequest) throws IOException {
    // put it here no in MyClass
    abstractGoogleClientRequest.setDisableGZipContent(true);
}

// class MyClass{} // you don't need it


@Override
protected User doInBackground(String... params) {
    User response = null;
    if (myApiService == null) { // Only do this once
        UserEndpoint.Builder builder = new UserEndpoint.Builder(AndroidHttp.newCompatibleTransport(),
                new AndroidJsonFactory(), null)
// options for running against local devappserver
// - 10.0.2.2 is localhost's IP address in Android emulator
// - turn off compression when running against local devappserver
                .setRootUrl("http://ift.tt/1q2eoqB")
                .setGoogleClientRequestInitializer(this);
 // end options for devappserver

        myApiService = builder.build();
    }

    try {
        User users = new User();
        users.setEmail(params[0]);
        users.setPassword(params[1]);
        users.setWho(params[2]);
        response = myApiService.insertUser(users).execute();
    } catch (Exception e) {
        Log.d("Could not Add User", e.getMessage(), e);
    }
    return response;
}

}

Now, to call this from my Activity, I'm trying this:

    public void insertUser(View v) {
    new EndpointsAsyncTaskInsert(this).execute();
}

My problem or my confusion arises with text fields on Login layout, because I should send that data to GoogleAppEngine, this is my code:

public class LoginMember extends Activity {
private static
//DbAdapter dbAdapter = null;
    //EditText txtUserName;
    EditText txtPassword;
    EditText txtEmail;
//EditText txtSchool;
    Button btnLogin;
    TextView Forgot_text;
    Button twitter;
    Button facebook;
    //Button btnRegister;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        txtPassword = (EditText) findViewById(R.id.et_pw);
        txtEmail = (EditText) findViewById(R.id.et_email);
        btnLogin = (Button) findViewById(R.id.btn_login);
        twitter = (Button) findViewById(R.id.twitter);
        facebook = (Button) findViewById(R.id.facebook);
        Forgot_text = (TextView) findViewById(R.id.Forgot_text);
        //String txtAuthorName = editAuthorName.getText().toString().trim();
        //String txtMessage = editMessage.getText().toString().trim();

        /**dbAdapter = new DbAdapter(this);
        dbAdapter.open();**/

        btnLogin.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(txtEmail.getWindowToken(), 0);
                imm.hideSoftInputFromWindow(txtPassword.getWindowToken(), 0);
                String password = txtPassword.getText().toString();
                String email = txtEmail.getText().toString();
                if (email.length() > 0 && password.length() > 0) {
                    try {

                        //if (dbAdapter.Login(email, password)) {
                            /**Toast.makeText(LoginMember.this,
                            "Successfully Logged In", Toast.LENGTH_LONG)
                            .show();
                            Intent i = new Intent(LoginMember.this, WelcomeScreen.class);
                            startActivity(i);**/

                        } else {
                            Toast.makeText(LoginMember.this,
                                    "Invalid email or password",
                                    Toast.LENGTH_LONG).show();
                        }

                    } catch (Exception e) {
                        Toast.makeText(LoginMember.this, "Some problem occurred",
                                Toast.LENGTH_LONG).show();

                    }
                } else {
                    Toast.makeText(LoginMember.this,
                            "Email or Password is empty", Toast.LENGTH_LONG).show();
                }
            }

        });

As You can see I commented DbAdapter object, which was from my previous SQLite version, I need to pass somehow the Google App Engine parameters to it...

How can I do that? I've searched for examples but I can't find one specific to this situation, or maybe I don't understand them, I think the backend module code works, I just need to pass that parameter to texts in orther to send them into my backend.

Any ideas?

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire