0
我是新來的android和翻新。當我點擊登錄時,如果回調返回「成功登錄」消息,那麼我應該重定向到主頁,但似乎我不允許在回調函數內部調用intent。不能調用意圖內部翻新成功回調函數
public void loginUser() {
//Here we will handle the http request to insert user to mysql db
//Creating a RestAdapter
RestAdapter adapter = new RestAdapter.Builder()
.setEndpoint(ROOT_URL) //Setting the Root URL
.build(); //Finally building the adapter
//Creating object for our interface
LoginAPI api = adapter.create(LoginAPI.class);
api.loginUser(
//Passing the values by getting it from editTexts
editTextUsername.getText().toString(),
editTextPassword.getText().toString(),
//Creating an anonymous callback
new Callback<Response>() {
@Override
public void success(Response result, Response response) {
//On success we will read the server's output using bufferedreader
//Creating a bufferedreader object
BufferedReader reader = null;
//An string to store output from the server
String output = "";
try {
//Initializing buffered reader
reader = new BufferedReader(new InputStreamReader(result.getBody().in()));
//Reading the output in the string
output = reader.readLine();
if(output == "Successful login") {
Intent i = new Intent(this, Home.class);
startActivity(i);
}
//Log.d(TAG, output);
} catch (IOException e) {
e.printStackTrace();
}
//Displaying the output as a toast
Toast.makeText(Login.this, output, Toast.LENGTH_LONG).show();
/*SharedPreferences mSettings = getSharedPreferences("data", 0);
SharedPreferences.Editor editor = mSettings.edit();
editor.putString("callback", output);
editor.commit();*/
}
@Override
public void failure(RetrofitError error) {
//If any error occured displaying the error as toast
Toast.makeText(Login.this, error.toString(), Toast.LENGTH_LONG).show();
}
}
);
}
此代碼位於何處?它是在一個活動中還是在一個單獨的類中? –
它在登錄活動中。 – rendell