i'm trying to create a login form that connects to an API and authorizes the username and password but whenever i press the onClickListener which is my LogIn button the app crashes. The username and password is hardcoded in my code.
Code:
public class LoginActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(layout.activity_login);
Button buttonLogin = (Button)findViewById(id.buttonLogin);
EditText uEmail = (EditText) findViewById(id.emailField);
EditText uPassword = (EditText) findViewById(id.passwordField);
buttonLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final OkHttpClient client = new OkHttpClient();
client.setAuthenticator(new Authenticator() {
@Override
public Request authenticate(Proxy proxy, Response response) throws IOException {
String credential = Credentials.basic("username", "password");
return response.request().newBuilder()
.header("Authorization", credential)
.build();
}
@Override
public Request authenticateProxy(Proxy proxy, Response response) throws IOException {
return null;
}
});
Request request = new Request.Builder().url("MyUrlThatIdontWannaShow").build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
Context context = getApplicationContext();
CharSequence text = "Error";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
@Override
public void onResponse(Response response) throws IOException {
Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
});
}
});
}
}
The error i get:
C:\Users\Dan\AndroidStudioProjects\LogsterAndroid\app\src\main\java\com\example\danial\logsterandroid\LoginActivity.java:23: error: cannot find symbol uEmail = (EditText)findViewById(R.id.emailField); ^ symbol: variable uEmail location: class LoginActivity C:\Users\Dan\AndroidStudioProjects\LogsterAndroid\app\src\main\java\com\example\danial\logsterandroid\LoginActivity.java:24: error: cannot find symbol uPassword = (EditText)findViewById(R.id.passwordField); ^ symbol: variable uPassword location: class LoginActivity 2 errors
FAILED
FAILURE: Build failed with an exception.
-
What went wrong: Execution failed for task ':app:compileDebugJava'.
Compilation failed; see the compiler error output for details.
-
Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
Thanks in advance!
Aucun commentaire:
Enregistrer un commentaire