I need to get user
and save it to variable.
public class MainActivity extends ActionBarActivity
{
User user = new LoginTask2().execute("");
}
class LoginTask2 extends AsyncTask<String, Void, User> {
private Exception exception;
public String hash = "";
protected String doInBackground(String... t) {
RestClient restClient = new HttpRestClient();
restClient.setUserAgent("bot/1.0 by name");
// Connect the user
User user = new User(restClient, "User", "somepass");
try {
user.connect();
//hash = user.getModhash();
return user;
} catch (Exception e) {
e.printStackTrace();
this.exception = e;
return null;
}
}
protected void onPostExecute(String string) {
}
}
It looks like it work, but I don't know how to get user
. With this code I get error:
Error:(49, 47) error: incompatible types
required: String
found: AsyncTask<String,Void,String>
Could someone give me advice how to change code?
Your async task is declared:
In order to avoid the incompatible types error, you need to make
doInbackground
return aUser
object.See: http://developer.android.com/reference/android/os/AsyncTask.html
The code you have above:
Will also fail because you must execute the async task and then later use the return value. You could access the returned
User
object by setting it as a field in yourMainActivity
and then using that object later onceAsyncTask
completes.