1

I'm new to Android and making the Login Page for the SudentApplication.

Have 3 activities in application,FrontPage LoginActivity and PageLogActivity. In FrongPage just gave a login symbol when user clicks on that symbol it will navigate to next activity call "LoginActvity", Here User giving UserName and Password and that connecting to the Php server there if both the username and password matches the response will come as "true".and also given remember to the user. That Code is Like this: LoginActivity

public class LoginActivity extends AppCompatActivity implements TextWatcher, 
CompoundButton.OnCheckedChangeListener {

final String TAG = LoginActivity.class.getSimpleName();

private CheckBox rem_userpass;
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;

private EditText etUserNmae,etPassword;
private Button btLogin;

String userName;
String password;

private static final String PREF_NAME = "prefs";
private static  final String KEY_REMEMBER = "remember";
private static final  String KEY_USERNAME = "username";
private  static final  String KEY_PASS = "password";

String url = "https://domainname.com/folderName/login";

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    sharedPreferences = getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
    editor = sharedPreferences.edit();

    etUserNmae = (EditText) findViewById(R.id.etusername);
    etPassword = (EditText) findViewById(R.id.etpassword);
    rem_userpass = (CheckBox) findViewById(R.id.checkBox);
    btLogin = (Button) findViewById(R.id.btlogin);

    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    if(sharedPreferences.getBoolean(KEY_REMEMBER,false)){
        rem_userpass.setChecked(true);
    }
    else {
        rem_userpass.setChecked(false);
    }


    etUserNmae.setText(sharedPreferences.getString(KEY_USERNAME,""));
    etPassword.setText(sharedPreferences.getString(KEY_PASS,""));

    etUserNmae.addTextChangedListener(LoginActivity.this);
    etPassword.addTextChangedListener(LoginActivity.this);
    rem_userpass.setOnCheckedChangeListener(LoginActivity.this);

    btLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int idi = view.getId();
            switch (idi) {
                case R.id.btlogin:
                    doing();
                    break;

            }
        }

        private void doing() {
             userName = etUserNmae.getText().toString();
             password = etPassword.getText().toString();

            if (TextUtils.isEmpty(userName)) {
                etUserNmae.setError("Please enter UserName");
                etPassword.setError("Please enter password");
                return;
            } else if (TextUtils.isEmpty(password)) {
                etPassword.setError("Please enter password");
                return;
            }else {
                StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.d(TAG, response);
                        if (response.contains("true")) {
                            Intent intent = new Intent(LoginActivity.this, PageLogActivity.class);
                            startActivity(intent);
                        } else {
                            Toast.makeText(getApplicationContext(), "Wrong Username and Password", Toast.LENGTH_SHORT).show();
                        }

                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                        Toast.makeText(getApplicationContext(), "Error while reading data", Toast.LENGTH_SHORT).show();

                    }
                }) {
                    @Override
                    protected Map<String, String> getParams() throws AuthFailureError {
                        Map<String, String> params = new HashMap<>();
                        params.put("username", etUserNmae.getText().toString());
                        params.put("password", etPassword.getText().toString());
                        return params;
                    }
                };
           MySingleton.getmInstance(getApplicationContext()).addToRequestQueue(stringRequest);
            }
        }
    });

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if(id == R.id.home){
        NavUtils.navigateUpFromSameTask(this);
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    managePrefs();

}

@Override
public void afterTextChanged(Editable editable) {
}

@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
    managePrefs();
}

 private void managePrefs(){
     if(rem_userpass.isChecked()) {
         editor.putString(KEY_USERNAME, etUserNmae.getText().toString().trim());
         editor.putString(KEY_PASS, etPassword.getText().toString().trim());
         editor.putBoolean(KEY_REMEMBER, true);
         editor.apply();
     }else {
        editor.putBoolean(KEY_REMEMBER, false);
         editor.remove(KEY_USERNAME);
         editor.remove(KEY_PASS);
         editor.apply();
     }
     }
 }

Geeting the response but when click Login button ut's getting the msg as a Unfrochunatly app as closed. It's not going to the NextACtivity.

And MYSingleTon class is like this:

public class MySingleton {

private static MySingleton mInstance;
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static Context mCtx;


private MySingleton(Context context){
    mCtx = context;
    mRequestQueue = getRequestQueue();
    //for loadig Image....

    mImageLoader  = new ImageLoader(mRequestQueue, new ImageLoader.ImageCache() {

        private final LruCache<String,Bitmap>
           cache = new LruCache<String, Bitmap>(20);

        @Override
        public Bitmap getBitmap(String url) {
            return cache.get(url);
        }

        @Override
        public void putBitmap(String url, Bitmap bitmap) {

          cache.put(url,bitmap);
        }
    });
}

public  static synchronized MySingleton getmInstance(Context context){
    if(mInstance == null){

        mInstance = new MySingleton(context);

    }
    return mInstance;
}

public RequestQueue getRequestQueue(){

    if (mRequestQueue == null){

        mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
    }
    return mRequestQueue;
}

public <T> void addToRequestQueue(Request<T> req){
    getRequestQueue().add(req);
}

public ImageLoader getmImageLoader(){
    return mImageLoader;
}}
MRX
  • 1,400
  • 3
  • 12
  • 32
Anusha B
  • 91
  • 1
  • 10
  • 1
    what does the logcat say – mehulmpt Jun 27 '17 at 06:26
  • Can you please post error log cat as well – MRX Jun 27 '17 at 06:32
  • @mehulmpt .OutOfMemoryError: Failed to allocate a 11833468 - byte allocation with 4194208 free bytes and 8MB until OOM – Anusha B Jun 27 '17 at 06:32
  • in that case did you try adding this android:hardwareAccelerated="false" , android:largeHeap="true" to your manifest file ? – MRX Jun 27 '17 at 06:33
  • @MRX in which activity i need to add those 2 lines – Anusha B Jun 27 '17 at 06:36
  • Are you loading an image that is of large size. Check it out and if thats true scale your image before using it in imageview – MRX Jun 27 '17 at 06:36
  • @AnushaB : No in your application tag in manifest file add these 2 line – MRX Jun 27 '17 at 06:37
  • @MRX no just am using 2TextView for username and password, and given logic to remember. – Anusha B Jun 27 '17 at 06:38
  • What about your mImageLoader in Mysingleton class ? isn't that loading some image – MRX Jun 27 '17 at 06:39
  • @mehulmpt still it's not working. – Anusha B Jun 27 '17 at 06:41
  • @MRX ya and I just used that code for requesting Volley. – Anusha B Jun 27 '17 at 06:43
  • remove the image loader code and try running it and see if that remove your error – MRX Jun 27 '17 at 06:44
  • 1
    @MRX if I remove that line no action will taking place.MySingleton.getmInstance(getApplicationContext()).addToRequestQueue(stringRequest); replace of this line which line i need to add. – Anusha B Jun 27 '17 at 06:50
  • Try this by add line `RequestQueue queue = MySingleton.getInstance(getApplicationContext()).getRequestQueue();` above `StringRequest stringRequest = new StringRequest....`. – Nitin Patel Jun 27 '17 at 06:56
  • @Nitin Patel getting like this Verbose in Logcat 06-27 12:29:58.496 19443-19443/com.frocerie.demoalqalam D/FeatureProxyBase: FeatureProxyBase class constructor 06-27 12:29:58.496 19443-19443/com.frocerie.demoalqalam D/MultiWindow: MultiWindowProxy constructor. – Anusha B Jun 27 '17 at 07:01
  • One more thing instead of getApplicationContext write(use) `LoginActivity.this` in LoginActivity for api code. – Nitin Patel Jun 27 '17 at 07:04
  • Change `RequestQueue queue = MySingleton.getInstance(LoginActivity.this).getRequestQ‌​ueue();` and – Nitin Patel Jun 27 '17 at 07:05
  • MySingleton.getmInstance(LoginActivity.this).addToRequestQueue(stringRequest); – Nitin Patel Jun 27 '17 at 07:06
  • @Nithin Patel When click button no action is going. 06-27 12:37:00.733 23499-23499/com.frocerie.demoalqalam D/skia: jpeg_decoder finish successfully, L:1843!!! 06-27 12:37:00.760 23499-23515/com.frocerie.demoalqalam I/art: Background sticky concurrent mark sweep GC freed 55(2112B) AllocSpace objects, 0(0B) LOS objects, 0% free, 84MB/84MB, paused 7.925ms total 23.614ms – Anusha B Jun 27 '17 at 07:07
  • Please remove imageLoader coding if you have. – Nitin Patel Jun 27 '17 at 07:09
  • @Nithin Patel If I remove MySingleton class it's showing error. – Anusha B Jun 27 '17 at 07:17
  • You don' t have to delete whole MySingleton Class. I will give you whole class by link. just after you got , just replace whole coding except first line which is your package. Give you link – Nitin Patel Jun 27 '17 at 07:20
  • @Nithin Patel Thank You, I will check it and let you know. – Anusha B Jun 27 '17 at 07:25
  • @AnushaB ok. if you still find issue let me know. – Nitin Patel Jun 27 '17 at 07:27
  • 1
    @Nithi Patel Still it's not working when I add MySingleton.getInstance(LoginActivty.this).getRequestQueue(); this line no action is taking place, But, when i add MySingleton.getInstance(getApplicationContext()).addToRequestQueue(stringRequest); Getting the Volley error "Error while reading data". – Anusha B Jun 27 '17 at 07:45
  • @Nithin Patel onResponse() is not at all working my application, am not getting the response as "true". – Anusha B Jun 27 '17 at 07:46
  • You have to change getApplicationContext to `LoginActivty.this` even for addToRequestQueue(). – Nitin Patel Jun 27 '17 at 07:47
  • And for volley error make sure and check URL is proper, and parameters key like username etc. are exact and same as using at server side. – Nitin Patel Jun 27 '17 at 07:51
  • @Nithin Patel MySingleton.getInstance(LoginActivty.this).addToRequestQueue(stringRequest); i wrie that line like this, when i click loginButton im getting LogCat as:06-27 13:20:17.203 24605-24700/com.frocerie.audioapplication D/OpenGLRenderer: Flushing caches (mode 0) 06-27 13:20:17.443 24605-24700/com.frocerie.audioapplication D/Surface: Surface::setBuffersDimensions(this=0x7fb0a1d600,w=720,h=1280) 06-27 13:20:17.450 24605-24700/com.frocerie.audioapplication W/MALI: glDrawArrays:714: [MALI] glDrawArrays takes more than 5ms here. Total elapse time(us): 6942 – Anusha B Jun 27 '17 at 07:52
  • Is this error ? or select error in android monitor and paste here. – Nitin Patel Jun 27 '17 at 07:59
  • going for lunch, so ping if you still face issue. – Nitin Patel Jun 27 '17 at 08:00
  • @Nithin Patel yes it's same as the name used in server side but still getting "Error while reading data". – Anusha B Jun 27 '17 at 08:02
  • @Nithin Patel No error in logCat, Just when press login button getting Tost as "Error while reading data". – Anusha B Jun 27 '17 at 08:05
  • Did you give **Internet permission** in AndroidManifest.xml? – Nitin Patel Jun 27 '17 at 08:51
  • @Nithin Patel ya I given Internet permission also. – Anusha B Jun 27 '17 at 09:17
  • Good then I will give you volley error code and change and we will find what is error. – Nitin Patel Jun 27 '17 at 09:20
  • @Nithin Patel 06-27 15:01:42.379 23880-23900/com.frocerie.audioapplication E/GED: Failed to get GED Log Buf, err(0) – Anusha B Jun 27 '17 at 09:32
  • @Nithin Patel 06-27 15:03:45.040 23880-23880/com.frocerie.audioapplication D/FeatureProxyBase: getService(), serviceName = multiwindow_service_v1 getting Sever Error – Anusha B Jun 27 '17 at 09:35
  • So, its server error. right? – Nitin Patel Jun 27 '17 at 09:36
  • @Nithin Patel yes, how to resolve it?? – Anusha B Jun 27 '17 at 09:37
  • can any one please reply....I am also facing the same issue – noushad mohammed Jun 27 '17 at 09:43
  • @AnushaB Comment the what response code just I have given and add this: `Log.e("Volley Error....", error.getMessage()); Toast.makeText(LoginActivity.this, ""+error.getMessage(), Toast.LENGTH_SHORT).show();` means replace it. So, we can find it. – Nitin Patel Jun 27 '17 at 09:46
  • @Nithin Patel how I get log cat, for your code. – Anusha B Jun 27 '17 at 09:54
  • In androidMonitor Verbose there will be line like ::Volley Error.... find it. – Nitin Patel Jun 27 '17 at 09:57
  • @Nithin Patel when I press Login button I'm not getting any Toast message. – Anusha B Jun 27 '17 at 09:58
  • Do u know how to debug – Nitin Patel Jun 27 '17 at 10:00
  • @Nithin Patel 06-27 15:34:03.832 27284-27284/com.frocerie.audioapplication W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable 06-27 15:34:04.041 27284-27360/com.frocerie.audioapplication E/GED: Failed to get GED Log Buf, err(0) – Anusha B Jun 27 '17 at 10:04
  • @Nithin Patel, I don't know can you please guide me. – Anusha B Jun 27 '17 at 10:05
  • Try this..https://stackoverflow.com/a/41410489/7806873 – Nitin Patel Jun 27 '17 at 10:09
  • Now its better to move chat in another. – Nitin Patel Jun 27 '17 at 10:11
  • Good. You have solved that? – Nitin Patel Jun 27 '17 at 10:27
  • @Nithin Patel, there I didn't the solution for my problem. – Anusha B Jun 27 '17 at 10:28
  • Now what is error? – Nitin Patel Jun 27 '17 at 10:29
  • 1
    @Nithin Patel In that link I didn't get the solution,06-27 16:03:32.097 27284-27284/com.frocerie.audioapplication D/FeatureProxyBase: FeatureProxyBase class constructor 06-27 16:03:32.101 27284-27284/com.frocerie.audioapplication D/MultiWindow: MultiWindowProxy constructor. 06-27 16:03:32.101 27284-27284/com.frocerie.audioapplication D/FeatureProxyBase: getService(), serviceName = multiwindow_service_v1 06-27 16:03:32.244 27284-27360/com.frocerie.audioapplication D/Surface: Surface::setBuffersDimensions(this=0x7fb0a1d600,w=720,h=1280) – Anusha B Jun 27 '17 at 10:34
  • Do you add that line AndroidManifest? – Nitin Patel Jun 27 '17 at 10:35
  • @Nithin Patel Yes I add but still, it's not going to next activity. – Anusha B Jun 27 '17 at 10:38
  • Now error is toast or not? – Nitin Patel Jun 27 '17 at 10:39
  • @Nithin Patel, No Nothing is happening. – Anusha B Jun 27 '17 at 10:42
  • Now you have to put debug points at first line of VolleyError and onResponse and have to debug and evaluate(check) value. – Nitin Patel Jun 27 '17 at 10:49
  • @Nithin Patel Ya I gave breakpoint and started debug process In Verbose I get this 3 lines. 06-27 16:24:28.666 19347-19347/com.frocerie.audioapplication D/FeatureProxyBase: FeatureProxyBase class constructor 06-27 16:24:28.666 19347-19347/com.frocerie.audioapplication D/MultiWindow: MultiWindowProxy constructor. 06-27 16:24:28.666 19347-19347/com.frocerie.audioapplication D/FeatureProxyBase: getService(), serviceName = multiwindow_service_v1 – Anusha B Jun 27 '17 at 10:56
  • @Nithin Patel and when I check Debug, Connected to the target VM, address: 'localhost:8704', transport: 'socket' Disconnected from the target VM, address: 'localhost:8704', transport: 'socket' Like this lines i'm getting. – Anusha B Jun 27 '17 at 10:58
  • Try this https://stackoverflow.com/a/31315769/7806873 and let me know. – Nitin Patel Jun 27 '17 at 11:01
  • Did you solve that? – Nitin Patel Jun 27 '17 at 11:22
  • Just last suggestion to delete this question so, you will not get negative rating any more. Enjoy coding... – Nitin Patel Jun 27 '17 at 11:30
  • @Nithin Patel, I changed added this line MySingleton.getInstance(LoginActivty.this).addToRequestQueue(stringRequest); – Anusha B Jun 27 '17 at 12:01
  • @Nithin Patel it's going to next Activity. – Anusha B Jun 27 '17 at 12:03
  • @Nithin Patel, but the thing is I removed else part where I'm going to check username and password is empty or not.if (TextUtils.isEmpty(userName)) { etUserNmae.setError("Please enter UserName"); etPassword.setError("Please enter password");return; } else if (TextUtils.isEmpty(password)) { etPassword.setError("Please enter password"); return;}//else// { Directly i written StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new { this code ... it's is gud coding or what?? – Anusha B Jun 27 '17 at 12:07
  • Better make one function (in that add all else part's coding) and just only call that function from else. I can this is good. – Nitin Patel Jun 27 '17 at 12:47
  • Please delete this question as professional member of stackoverflow! – Nitin Patel Jun 27 '17 at 13:12
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/148108/discussion-between-anusha-b-and-nitin-patel). – Anusha B Jul 01 '17 at 07:05

0 Answers0