Volley Error on "preferred Data SIM" change in dual sim

196 views Asked by At

I have an application that is using a Volley request. It works well when SIM 1 is my preferred data SIM card. On switching the preferred data sim cards on the dual sim, I get a volley 404 error on requests.

My Volley method looks like this (working perfectly with one SIM card):

 private void login() {
        loading = ProgressDialog.show(context, null, "Authenticating.Please wait...",true,true);
        StringRequest strReq = new StringRequest(Request.Method.POST,
                MyApplication.Online_Login, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                Log.e(TAG, "response: " + response);

                try {
                    JSONObject obj = new JSONObject(response);

                    // check for error flag
                    if (obj.getString("error").equals("false")) {
                        // user successfully logged in
                        loading.dismiss();
                        JSONObject userObj = obj.getJSONObject("user");
                        String id=userObj.getString("requestor_id");
                        String nam= userObj.getString("name");
                        String ema=userObj.getString("email");
                        User user = new User(id,nam,ema);
                        // storing user in shared preferences
                        if(myPreferenceManager.isFirstLaunch()){
                            reRegisterGCM(nam,ema);
                            myPreferenceManager.setIsFirstLaunch(false);

                        }
                        MyApplication.getInstance().getPrefManager().storeUser(user);
                        startActivity(new Intent(getApplicationContext(), MapsActivity.class));
                        finish();

                    } else {
                        // login error - simply toast the message
                        loading.dismiss();
                        AlertDialog.Builder builder = new AlertDialog.Builder(context);
                        builder.setTitle("Invalid credentials").setCancelable(false)
                                .setMessage("Please try again")
                                .setNegativeButton("Retry", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i) {
                                        //retry
                                        dialogInterface.dismiss();
                                    }
                                });
                        builder.show();

                        //Toast.makeText(getApplicationContext(), "" + obj.getJSONObject("error").getString("message"), Toast.LENGTH_LONG).show();
                    }

                } catch (JSONException e) {
                    Log.e(TAG, "json parsing error: " + e.getMessage());
                    Toast.makeText(getApplicationContext(), "Json parse error: " + e.getMessage(), Toast.LENGTH_SHORT).show();
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                NetworkResponse networkResponse = error.networkResponse;
                loading.dismiss();
                Log.e(TAG, "Volley error: " + error.getMessage() + ", code: " + networkResponse+" and "+error.getMessage());
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setTitle("Error").setCancelable(false)
                        .setMessage("Please check your internet settings and try again")
                        .setNeutralButton("Open settings", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Intent myIntent = new Intent(Settings.ACTION_SETTINGS);
                               startActivity(myIntent);
                            }
                        })
                        .setNegativeButton("Retry", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                //retry
                             login();
                            }
                        });
                builder.show();
               //Toast.makeText(getApplicationContext(), "Volley error: " + error.getMessage(), Toast.LENGTH_SHORT).show();
            }
        }) {

            @Override

            public Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                params.put("email",email);
                params.put("password",password);

                Log.e(TAG, "params: " + params.toString());
                return params;
            }
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String,String> params = new HashMap<String, String>();
                params.put("Content-Type","application/x-www-form-urlencoded");
                return params;
            }
        };
        //Adding request to request queue
        MyApplication.getInstance().addToRequestQueue(strReq);
    }

The below error is encountered after switching to the other simcard on my dualsim(The logcat)

01-08 07:59:36.143 1305-1342/mushirih.up I/[MALI][Gralloc]: [+]r_hnd(0x7fa19abe60), client(35), share_fd(63)
01-08 07:59:36.427 1305-1428/mushirih.up I/System.out: close [socket][/196.103.96.143:44803]
01-08 07:59:36.428 1305-1428/mushirih.up E/Volley: [3020] BasicNetwork.performRequest: Unexpected response code 404 for http://xxx/v1/user/login
01-08 07:59:36.450 1305-1342/mushirih.up I/[MALI][Gralloc]: [-]r_hnd(0x7fa19abd20), client(35), share_fd(55)
01-08 07:59:36.451 1305-1342/mushirih.up I/[MALI][Gralloc]: [-]r_hnd(0x7f8fc20640), client(35), share_fd(61)
01-08 07:59:36.452 1305-1342/mushirih.up I/[MALI][Gralloc]: [-]r_hnd(0x7fa19abe60), client(35), share_fd(63)
01-08 07:59:36.452 1305-1342/mushirih.up I/[MALI][Gralloc]: [-]r_hnd(0x7fa19ac2c0), client(35), share_fd(56)
01-08 07:59:36.457 1305-1305/mushirih.up E/MainActivity.class: Volley error: null, code: com.android.volley.NetworkResponse@e26bc80 and null
01-08 07:59:36.504 1305-1342/mushirih.up W/libEGL: [ANDROID_RECORDABLE] format: 1
0

There are 0 answers