Android Volley Crashing

jampez77

I'm using Android Volley to login a user in but when addToRequestQueue is called the following crash report is shown.

Crash Report

The login request is carried out onclick in the Loginactivity as follows:

// Login button Click Event
    btnLogin.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            String username = inputUserName.getText().toString();
            String pin = inputPIN.getText().toString();

            // Check for empty data in the form
            if (username.trim().length() > 0 && pin.trim().length() > 0) {
                // login user

                SharedPreferences prefs = getGCMPreferences(context);
                String storeRegId = prefs.getString(PROPERTY_REG_ID, "");
                String devid = Secure.getString(getBaseContext().getContentResolver(),Secure.ANDROID_ID); 
                String vars = "/?tag=login&username=" + username + "&pin=" + pin + "&regid=" + storeRegId + "&devid=" + devid;

                WebRequest wr = new WebRequest(context);
                wr.Request(AppConfig.URL_LOGIN, vars, true, "Please Wait", "Logging in...", "req_login");


            } else {
                // Prompt user to enter credentials
                Toast.makeText(getApplicationContext(), "Please enter the credentials!", Toast.LENGTH_LONG).show();
            }
        }

    });

WebRequest, which uses volley looks like this:

package app;

import helper.SQLiteHandler;
import helper.SessionManager;
import helper.dbTables;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import main.MainActivity;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.WindowManager;
import android.widget.Toast;

import com.android.volley.Response; 
import com.android.volley.VolleyError;
import com.android.volley.Request.Method;
import com.android.volley.toolbox.StringRequest;
import com.google.android.gms.gcm.GoogleCloudMessaging;

public class WebRequest extends Activity {

private static final String TAG = "LoginActivity";

AtomicInteger msgId = new AtomicInteger();
GoogleCloudMessaging gcm;
SharedPreferences prefs;
public SQLiteHandler db;
SessionManager session;

protected Context context;

public WebRequest(Context context){
    this.context = context.getApplicationContext();
} 



public void Request(final String url, final String vars, final Boolean show, final String title, final String msg, final String requestName){


    final SessionManager session = new SessionManager(context);
    final ProgressDialog theProgressDialog = new ProgressDialog(context);
    db = new SQLiteHandler(context);

    if(show == true){
        theProgressDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        theProgressDialog.setTitle(title);
        theProgressDialog.setMessage(msg);
        theProgressDialog.setIndeterminate(true);
        theProgressDialog.setCancelable(false);
        theProgressDialog.show();
    }

    StringRequest strreq = new StringRequest(Method.GET, url + vars, 
            new Response.Listener<String>() {

                @Override
                public void onResponse(String response) {
                    Log.d(TAG, "WEB Response: " + response.toString());


                    try {
                        JSONObject jObj = new JSONObject(response);
                        boolean error = jObj.getBoolean("error");

                        // Check for error node in json
                        if (!error) {

                            switch(requestName){
                                case "req_login":
                                    //process user login
                                    // Fetching user details from sqlite

                                    HashMap<String, String> user = db.fetchResults(dbTables.TABLE_LOGIN, null);

                                    String storedUid = user.get("uid");

                                    JSONObject login = jObj.getJSONObject("login"); 


                                    if(storedUid != login.getString("uid")){
                                        //new user for device

                                        String[] emptyTables = {dbTables.TABLE_LOGIN};

                                            for(int i=0; i<emptyTables.length; i++){
                                             db.emptyTable(emptyTables[i]);
                                             Log.d(TAG, "empty table : " + emptyTables[i]);
                                           }

                                    }

                                    //store user in database
                                    db.addUser(login.getString("uid"), 
                                               login.getString("companyid"), 
                                               login.getString("resourceid"), 
                                               login.getString("groupid"), 
                                               login.getString("title"), 
                                               login.getString("firstname"), 
                                               login.getString("middleinitial"), 
                                               login.getString("lastname"), 
                                               login.getString("jobtitle"), 
                                               login.getString("managerid"), 
                                               login.getString("email"), 
                                               login.getString("photo_url"), 
                                               login.getString("signature_url"), 
                                               login.getString("language"), 
                                               login.getString("skin"), 
                                               login.getString("defaultprojectid"), 
                                               login.getString("cnNotifications"), 
                                               login.getString("crNotifications"), 
                                               login.getString("coNotifications"), 
                                               login.getString("addedby"), 
                                               login.getString("dateadded"), 
                                               login.getString("editedby"), 
                                               login.getString("dateedited"));









                                    // Create login session
                                    session.setLogin(true);

                                    // Launch main activity
                                    Intent intent = new Intent(context, MainActivity.class);
                                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                    context.startActivity(intent);
                                    //finish();
                                break;
                            }

                            if(show == true){
                                theProgressDialog.dismiss();
                            }
                        }else{
                            String errorMsg = jObj.getString("error_msg");
                            Toast.makeText(context, errorMsg, Toast.LENGTH_LONG).show();

                            if(show == true){
                                theProgressDialog.dismiss();
                            }
                        }
                    }catch(JSONException e){
                        // JSON error
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e(TAG, "Web Error: " + error.getMessage());

                    Toast.makeText(context, error.getMessage(), Toast.LENGTH_LONG).show();

                    if(show == true){
                        theProgressDialog.dismiss();
                    }

                }
            });



    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(strreq, requestName);


}

}

I've also included the AppController which I haven't personally changed

package app;

import android.app.Application;
import android.text.TextUtils;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;

public class AppController extends Application {

public static final String TAG = AppController.class.getSimpleName();

private RequestQueue mRequestQueue;

private static AppController mInstance;

@Override
public void onCreate() {
    super.onCreate();
    mInstance = this;
}

public static synchronized AppController getInstance() {
    return mInstance;
}

public RequestQueue getRequestQueue() {
    if (mRequestQueue == null) {
        mRequestQueue = Volley.newRequestQueue(getApplicationContext());
    }

    return mRequestQueue;
}

public <T> void addToRequestQueue(Request<T> req, String tag) {
    req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
    getRequestQueue().add(req);
}

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

public void cancelPendingRequests(Object tag) {
    if (mRequestQueue != null) {
        mRequestQueue.cancelAll(tag);
    }
}
}
EE66
  • This is no way to create a Activity object. A lot get go wrong like this.
    • Activity is a context therefore he doesnt need to hold a context as a member. That is a common memory leak.
    • The activity doesnt go trough the proper life cycle and that is why everything blows up.

I would suggest to you to start from the basic because when you will do this everything will be clearer.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related