Monday, 29 May 2017

Volley in Android

Login webservice volley demo :


private void login() {
    nm = uname.getText().toString().trim();
    pass = password.getText().toString().trim();
    pdialog = new ProgressDialog(Login.this);
    pdialog.setMessage("Please wait...");
    if (nm.equals("")) {
        uname.setError("email required");
    } else if (pass.equals("")) {
        password.setError("password required");
    } else {
        isInternet = ie.isInternetAvailable();
        if (isInternet) {
            pdialog.show();
            JSONObject jsonObject = new JSONObject();
            try {
                JSONObject login_info = new JSONObject();
                login_info.put("user_name", nm);
                login_info.put("password", pass);
                login_info.put("device_token", regid);
                login_info.put("device_type", "0");
                jsonObject.accumulate("method", "sign_in");
                jsonObject.accumulate("params", login_info);

            } catch (Exception e) {
                Log.d("InputStream", e.getLocalizedMessage());
            }
            JsonObjectRequest stringRequest = new JsonObjectRequest(Request.Method.POST, Config.URL, jsonObject,
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {
                            try {
                                String loginresult = response.optString("success").toString();
                                if (loginresult.equals("true")) {
                                    JSONObject businessObject = response.getJSONObject("params");
                                    JSONObject businessObject1 = businessObject.getJSONObject("user_details");
                                    String salesmanid = businessObject1.getString("sales_id");
                                    // Toast.makeText(Login.this, "sales man id :- "+salesmanid , Toast.LENGTH_SHORT).show();
                                    //Toast.makeText(Login.this, "login success", Toast.LENGTH_SHORT).show();
                                    pref = new preference(getApplicationContext());
                                    pref.setLoginStatus(true);
                                    pref.setUserName(nm);
                                    pref.setSalesId(salesmanid);
                                    pdialog.dismiss();
                                    startActivity(new Intent(getApplicationContext(), Home.class));
                                    finish();
                                } else if (loginresult.equals("false")){
                                    Toast.makeText(getApplicationContext(), "Invalid Username or Password", Toast.LENGTH_SHORT).show();
                                    pdialog.dismiss();
                                }

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            pdialog.dismiss();
                            Toast.makeText(Login.this, "Response error", Toast.LENGTH_SHORT).show();
                            error.printStackTrace();
                        }
                    }) {
                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    HashMap<String, String> headers = new HashMap<String, String>();
                    headers.put("Content-Type", "application/json; charset=utf-8");
                    return headers;
                }
            };
            RequestQueue requestQueue = Volley.newRequestQueue(this);
            requestQueue.add(stringRequest);
        } else {
            Toast.makeText(this, "Internet not available.", Toast.LENGTH_SHORT).show();
        }
    }
}