Android中的改造错误

时间:2016-05-03 10:00:58

标签: java android django rest django-rest-framework

我正在尝试创建一个登录页面。单击按钮,我正在调用一种方法来验证登录凭据(如果已存在于后端django服务器中)。我可以获取用户名和密码的值。我目前面临的问题是,如果我提供后端服务器中已存在的登录凭据,则应用程序正常工作。但是,如果我提供不在后端数据库中的登录凭据,则应用程序崩溃。请帮助

Login.xml代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/login_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:background="@drawable/login_background"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

    <TextView
        android:id="@+id/appName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="50dp"
        android:text="Teacher Diary"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@color/white" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="By"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@color/white" />

    <ImageView
        android:id="@+id/logo"
        android:layout_width="100sp"
        android:layout_height="100sp"
        android:layout_gravity="center"
        android:contentDescription="@string/home_screen_description"
        android:src="@drawable/lo_logo" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="@string/app_name"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@color/white" />

    <EditText
        android:id="@+id/etUserName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="25dp"
        android:background="@drawable/apptheme_edit_text_holo_light"
        android:ems="10"
        android:hint="@string/edit_text_username"
        android:singleLine="true"
        android:textColor="@color/blue" />

    <EditText
        android:id="@+id/etPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:background="@drawable/apptheme_edit_text_holo_light"
        android:ems="10"
        android:hint="@string/edit_text_password"
        android:imeOptions="actionGo"
        android:inputType="textPassword"
        android:textColor="@color/blue" />

    <Button
        android:id="@+id/btLogin"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="20dp"
        android:background="@drawable/button_background"
        android:text="@string/login"
        android:textColor="@color/white"
        android:textSize="20dp" />

    <TextView
        android:id="@+id/tvDisplayMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="5dp"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@color/white" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="150dp"
        android:text="The easiest way to conduct assessments!"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@color/white" />
</LinearLayout>

login.java代码

public class Login extends Activity implements View.OnClickListener {

    private SQLiteDatabase m_database;
    private LODatabaseHelper m_databaseHelper;
    private Button m_btLogin;
    private Animation m_anim;

    private EditText m_etUsername, m_etPassword;
    private String m_username, m_password, m_role, m_schoolId;
    private TextView m_displayMessage;
    private String ACCESS_TOKEN = "f59Gh28E6@2h13Y";
    private static RequestToServiceCallback mGetRequestToServiceCallback;
    public static void setCallback(RequestToServiceCallback callback) {
        mGetRequestToServiceCallback = callback;
    }

    public interface RequestToServiceCallback {
        void statusReporting(int id);

        void stopService();
    }

    boolean login = true;
    ProgressDialog pDialog;

    //List of type books this list will store type Book which is our data model
    private static List<LoginModel> logindata;
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // getActionBar().hide();

        ActionBar bar = getActionBar();
        bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#CC25AAE2")));

        int titleId = getResources().getIdentifier("action_bar_title", "id",
                "android");

        TextView abTitle = (TextView) findViewById(titleId);
        abTitle.setTextSize(15.5f);
        abTitle.setTextColor(Color.WHITE);

        SharedPreferences prefs = this.getSharedPreferences("global_settings",
                Context.MODE_PRIVATE);
        m_username = prefs.getString("username", "");
        m_role = prefs.getString("role", "");

        if (m_username.contentEquals("")) {
            // do Nothing and continue further the user hasn't logged in yet.
            // mSync.adminSync("2");
        } else {
            Intent intent;
            if (m_role.contentEquals("admin"))
                intent = new Intent(getApplicationContext(), AdminSync.class);
            else
                intent = new Intent(getApplicationContext(), Home.class);
            finish();
            startActivity(intent);
        }
        setContentView(R.layout.login);

        /** Initialise m_database variables */
        m_databaseHelper = LODatabaseHelper.getHelper(getApplicationContext());
        m_database = m_databaseHelper.getWritableDatabase();
        LODatabaseUtility.getInstance().setDatabase(m_database);
        m_btLogin = (Button) findViewById(R.id.btLogin);
        m_btLogin.setOnClickListener(this);

        m_displayMessage = (TextView) findViewById(R.id.tvDisplayMessage);
        m_etUsername = (EditText) findViewById(R.id.etUserName);

        m_etPassword = (EditText) findViewById(R.id.etPassword);

        /* Put the last login credentials into the edit texts */
        String lastUsername = prefs.getString("username_last", "");
        String lastPassword = prefs.getString("password_last", "");

        if (lastUsername.contentEquals("")) {
            /* Do Nothing */
        } else {
            m_etUsername.setText(lastUsername);
            m_etPassword.setText(lastPassword);
        }

        setAnimationValue();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }

    private void setAnimationValue() {
        m_anim = new AlphaAnimation(0.0f, 1.0f);
        m_anim.setDuration(100); // You can manage the time of the blink with
        // this parameter
        m_anim.setStartOffset(20);
        m_anim.setRepeatMode(Animation.REVERSE);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btLogin:
            /* check the username and password */
                if (m_etUsername.getText().length() == 0) {
                    m_displayMessage.setText("Enter User Name");
                    if (m_etPassword.getText().length() == 0) {
                        m_displayMessage.setText("Enter User Name and Password");
                    }
                    return;
                }
                if (m_etPassword.getText().length() == 0) {
                    m_displayMessage.setText("Enter User Password");
                    return;
                }
                m_username = m_etUsername.getText().toString();
                m_password = m_etPassword.getText().toString();
                //Calling the method that will fetch data
                getLoginDetails(m_username, m_password, ACCESS_TOKEN);
                break;
        }
    }

    private void getLoginDetails(final String m_username, final String m_password, final String ACCESS_TOKEN) {

        //While the app fetched data we are displaying a progress dialog
        final ProgressDialog loading = ProgressDialog.show(Login.this, "Please wait... ", "Login in progress", false, false);

        Log.e("m_username:", m_username);
        Log.e("m_password:", m_password);
        App.getRestClient()
                .getLoginService()
                .getLoginData(m_username, m_password, ACCESS_TOKEN,
                        new Callback<List<LoginAPIModel>>() {

                    @Override
                    public void failure(RetrofitError arg0) {
                        if(arg0.getKind() == RetrofitError.Kind.CONVERSION || arg0.getKind() == RetrofitError.Kind.NETWORK){
                            Log.e("Exception","");
                        }
                        /*Log.e( "TAG" + "ERROR", "Logindetails table: start = "
                                + "" + retrofitError.getMessage());
                        if (retrofitError.getKind() == RetrofitError.Kind.CONVERSION
                                || retrofitError.getKind() == RetrofitError.Kind.NETWORK) {
                            getLoginDetails(m_username, m_password, ACCESS_TOKEN);
                        } else {
                            mGetRequestToServiceCallback.stopService();
                        }*/
                    }

                    @Override
                    public void success(List<LoginAPIModel> arg0,
                                        Response response) {
                        //Dismissing the loading progressbar
                        loading.dismiss();

                        Log.e("schoolid", arg0.get(0).getSchoolId());
                        Log.e("username:", arg0.get(0).getUsername());
                        Log.e("role:", arg0.get(0).getRole());
                        SharedPreferences prefs = getSharedPreferences(
                                "global_settings",
                                Context.MODE_PRIVATE);
                        Editor editor = prefs.edit();
                        editor.putString("school_id",
                                arg0.get(0).getSchoolId());
                        editor.putString("username",
                                arg0.get(0).getUsername());
                        editor.putString("password",
                                arg0.get(0).getPassword());
                        editor.putString("role",
                                arg0.get(0).getRole());
                        editor.putString("email",
                                arg0.get(0).getEmail());
                        editor.putString("user",
                                arg0.get(0).getUser());
                        editor.putString("otp",
                                arg0.get(0).getOtp());
                        editor.putString("telephone_no",
                                arg0.get(0).getTelephoneNo());
                        editor.commit();
                        String temp1 = arg0.get(0).getRole();
                        Log.e("Temp1 values:", temp1);
                        String tempUsername=arg0.get(0).getUsername();
                        Log.e("username values:", tempUsername);
                        String tempPassword=arg0.get(0).getPassword();
                        Log.e("password values:", tempPassword);
                        Intent intent = null;
                        if (temp1.contentEquals("admin")) {

                            intent = new Intent(Login.this, AdminSync.class);
                            intent.putExtra("mSchoolId", m_schoolId);
                            finish();
                            startActivity(intent);
                            new loginUser().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
                        }else if (temp1.contentEquals("teacher")) {
                            loading.dismiss();
                            m_displayMessage.setText("Invalid username password !");
                        } else {
                            //intent = new Intent(Login.this, Home.class);
                        }
                        //


                    }

                });
    }

我的Logcat:

05-03 13:02:17.156 13719-13719/? E/m_username:: GChjvbj
05-03 13:02:17.156 13719-13719/? E/m_password:: chknmnvv
05-03 13:02:17.293 7409-7419/? E/Parcel: Reading a NULL string not supported here.
05-03 13:02:17.555 13719-13719/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                   Process: com.learningoutcomes, PID: 13719
                                                   java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
                                                       at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
                                                       at java.util.ArrayList.get(ArrayList.java:308)
                                                       at com.learningoutcomes.Login$1.success(Login.java:206)
                                                       at com.learningoutcomes.Login$1.success(Login.java:184)
                                                       at retrofit.CallbackRunnable$1.run(CallbackRunnable.java:45)
                                                       at android.os.Handler.handleCallback(Handler.java:808)
                                                       at android.os.Handler.dispatchMessage(Handler.java:103)
                                                       at android.os.Looper.loop(Looper.java:193)
                                                       at android.app.ActivityThread.main(ActivityThread.java:5341)
                                                       at java.lang.reflect.Method.invokeNative(Native Method)
                                                       at java.lang.reflect.Method.invoke(Method.java:515)
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
                                                       at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案