mercredi 6 mai 2015

Cant validate Login on Android app using an api request

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();
                }
            });
        }

    });

}

}

XML:

<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
            xmlns:tools="http://ift.tt/LrGmb4"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin"
            android:paddingBottom="@dimen/activity_vertical_margin"
            tools:context=".LoginActivity">

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Logga in"
    android:id="@+id/buttonLogin"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"/>

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textEmailAddress"
    android:ems="10"
    android:id="@+id/emailField"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="email"/>

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textPassword"
    android:ems="10"
    android:id="@+id/passwordField"
    android:layout_below="@+id/emailField"
    android:layout_alignLeft="@+id/emailField"
    android:layout_alignStart="@+id/emailField"
    android:text="lösenord"/>

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