0

I have this onClickListener method on my login activity in my android app:

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 ((txtEmail.length() == 0) || (txtPassword.length() == 0)) {
                    Toast.makeText(LoginMember.this, "You need to provide values for Email and Password", Toast.LENGTH_SHORT).show();
                    return;
                }

                //Go ahead and perform the transaction
                String[] params = {email,password};
                new EndpointsAsyncTaskInsert(LoginMember.this).execute(params);


            }
        });

It sends data to Google App Engine without any problems, already my EndpointsAsyncTask class defined, etc etc

Now, my problem is, I need to also go to another activity after this, I'm not realyl sure, but If I remember well I could do this automatically when logged in by using SQLite, don't know how to accomplish it here.

Already have the activities I need declared on manifest.

It should be something like this:

@Override
public void onClick(View v){
    Intent intent = new Intent(LoginMember.this, WelcomeScreen.class);
    startActivity(intent);
 }

My problem is that I don't know how to "add" or "append" this activity transaction into this logic, I'm fairly new to android and google app engine, Any ideas???

Thanks in advance!

EDIT

This is my EndpointsAsyncTaskInsert code:

package com.kkoci.shairlook;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

import com.appspot.shairlook1.userEndpoint.UserEndpoint;
import com.appspot.shairlook1.userEndpoint.model.User;
import com.google.api.client.extensions.android.http.AndroidHttp;
import com.google.api.client.extensions.android.json.AndroidJsonFactory;
import com.google.api.client.googleapis.services.AbstractGoogleClientRequest;
import com.google.api.client.googleapis.services.GoogleClientRequestInitializer;

import java.io.IOException;
import java.util.Collections;
import java.util.List;

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

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("https://shairlook1.appspot.com/_ah/api/")
                .setGoogleClientRequestInitializer(this);
// end options for devappserver

        myApiService = builder.build();
    }

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

}

SECOND EDIT

This is how it looks right now, it's giving me 'java.lang.NoClassDefFoundError' on this line: new EndpointsAsyncTaskInsert(LoginMember.this) { :

public class LoginMember extends Activity {
private static
//DbAdapter dbAdapter = null;
    //EditText txtUserName;
    EditText txtPassword;
    EditText txtEmail;
    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);

        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 ((txtEmail.length() == 0) || (txtPassword.length() == 0)) {
                    Toast.makeText(LoginMember.this, "You need to provide values for Email and Password", Toast.LENGTH_SHORT).show();
                    return;
                }

                //Go ahead and perform the transaction
                String[] params = {email,password};
                //new EndpointsAsyncTaskInsert(LoginMember.this).execute(params);
                /**try{  Intent k = new Intent(LoginMember.this, WelcomeScreen.class);
                    startActivity(k);
                }catch(Exception e){

                }**/
                new EndpointsAsyncTaskInsert(LoginMember.this) {
                    protected void onPostExecute(User result) {
                        super.onPostExecute(result);
                        // Do something with result
                        Intent intent = new Intent(LoginMember.this, WelcomeScreen.class);
                        startActivity(intent);
                    }
                }.execute(params);

            }
        });

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

}
NeoVe
  • 3,857
  • 8
  • 54
  • 134
  • Is `EndpointsAsyncTaskInsert` a class you made? There should be an `onPostExecute` as part of the AsyncTask. You could do an anonymous class in your code and override that to do your intent. It's hard to tell though without seeing the code of that class. – Dan Harms Jul 06 '15 at 21:06
  • Hi @dharms See my edit, please, thank you! – NeoVe Jul 06 '15 at 21:18
  • Made a second edit, thank you – NeoVe Jul 06 '15 at 22:09

4 Answers4

2

ok, i see, maybe you should do this, i haven't tryied this yet, but could help you:

Before onCreate method, declare a var of this way:

Activity currentActivity;

then inside onCreate method do this:

currentActivity=this;

so then, when you make you Asyctask, make this:

new EndpointsAsyncTaskInsert(currentActivity.getApplicationContext()).execute(params);

Hope that helps, let's me know if was helpFull, if not i try to help you in another way.

Regards.

Max Pinto
  • 1,463
  • 3
  • 16
  • 29
  • ok, lets me know if it works, if it doesnt show me your logcat when your app crash – Max Pinto Jul 07 '15 at 16:20
  • Seems like it's working but it doesn't sends anything into GAE – NeoVe Jul 07 '15 at 16:56
  • well i havent work with google app engine, so in that i cant help you. But, the original problem was solved, right?, so now the problem is with GAE call, could be another thread. So, the best is to accept/close some answer and open a new Thread with new circunstances. Regards. – Max Pinto Jul 07 '15 at 17:00
1

You can make an anonymous version of your AsyncTask class and override the onPostExecute to start the new activity after it is done.

new EndpointsAsyncTaskInsert(LoginMember.this) {
    protected void onPostExecute(User result) {
        super.onPostExecute(result);
        // Do something with result
        Intent intent = new Intent(LoginMember.this, WelcomeScreen.class);
        startActivity(intent);
    }
}.execute(params);
Dan Harms
  • 4,725
  • 2
  • 18
  • 28
  • Should I need some empty 'onPostExecute' statement in my EndpointsAsyncTaskInsert for this to work? Says 'java.lang.NoClassDefFoundError' – NeoVe Jul 06 '15 at 21:42
  • No you should not need it. There must be something else wrong with your setup where it can't find that class. Were you getting that error before making those changes? What I did was just extend what you already had. – Dan Harms Jul 06 '15 at 21:49
  • No I wasn't getting that error :( Ok, I see, I'm rebuilding the project – NeoVe Jul 06 '15 at 21:53
  • Should I open a new question for that? – NeoVe Jul 06 '15 at 21:58
  • The issue is with the anonymous function, already happened to me before, don't know if I should make it abstract on EndpointsAsyncTaskInsert or something – NeoVe Jul 06 '15 at 22:03
  • You shouldn't have to. Perhaps I have the wrong parameter signature? Are you missing an import for `User`? – Dan Harms Jul 06 '15 at 22:04
  • I'll edit my question with the actual activity result – NeoVe Jul 06 '15 at 22:05
  • Did you check to make sure you are importing the `User` class now? You can also add `@Override` above the anonymous method and see if it complains about that. – Dan Harms Jul 06 '15 at 22:12
  • I'm importing in on EndpointsAsyncTaskInsert class 'import com.appspot.shairlook1.userEndpoint.model.User; ' – NeoVe Jul 06 '15 at 22:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/82542/discussion-between-dharms-and-neove). – Dan Harms Jul 06 '15 at 22:15
  • 'Annotations are not allowed here' says when I add @Override above that method – NeoVe Jul 06 '15 at 22:16
1

In EndpointsAsyncTask class there should be method named onPostExecute() which is executed when your async task is completed. This is the place where you should notify your activity to go to another activity.

There are numerous way to do that.

  1. You can create an Interface class for instance

    public interface OnTaskFinishListener{

     void onFinish();
    

    }

and then implement this interface in your caller class:

public class YourActivity extends Activity implements OnTaskFinishListener {

    void onFinish(){
         Intent intent = new Intent(LoginMember.this, WelcomeScreen.class);
         startActivity(intent);
    }
}

When you create asynctask you should pass this reference as a parameter in its constructor and keep it in task fields and when the task is done call the onFinish method.

public EndpointsAsyncTaskInsert extends AsyncTask...{
    private OnTaskFinishListener listener;

    public EndpointsAsyncTaskInsert(OnTaskFinishListener listener){
        this.listener = listener;
    }

    protected void onPostExecute(..){
         //notify the listener
         listener.onFinish();
    }
}
  1. Second and more loosely coupled way is to use an event bus library, for example, greenrobots EventBus https://github.com/greenrobot/EventBus, then you can post an event when your task is finished, and then you can receive that event in your activity without setting up any listeners.
Gunhan
  • 6,807
  • 3
  • 43
  • 37
0

Maybe this might work

if ((txtEmail.length() == 0) || (txtPassword.length() == 0)) {
                Toast.makeText(LoginMember.this, "You need to provide values for Email and Password", Toast.LENGTH_SHORT).show();
                return;
            }
else{
String[] params = {email,password};
            new EndpointsAsyncTaskInsert(LoginMember.this).execute(params);
}

In the class which extends the Asynctask override the onpostexecute method and add the following code

 Intent in=new Intent(Login.this,Welcome.class);
 in.putExtra("email",email);
 in.putExtra("password",password);
 startActivity(in);

You can use bundle to send data from one activity to another and retrieve from the bundle in the Welcome activity

Intent in=getIntent();
String email=in.getStringExtra("email");
mik dass
  • 381
  • 3
  • 16
  • This doesn't wait for the AsyncTask to execute. You are prematurely transitioning activities. – Dan Harms Jul 06 '15 at 21:13
  • OK.My mistake I didnot get the question completely.You can execute this intent code in the onpostexecute method of the AsyncTask class(edited above).Hope it works. – mik dass Jul 06 '15 at 21:17
  • Hi, @user3834761 thank you, but my AsyncTask class is in another file, a separate class, so I can't write activity.this, class.class – NeoVe Jul 06 '15 at 21:23
  • 1
    You can make your AsyncTask as a inner class .You may thing if this idea is good or not.You may check this link for help.http://stackoverflow.com/questions/14374682/whats-the-correct-way-to-implement-asynctask-static-or-non-static-nested-class. – mik dass Jul 06 '15 at 21:32