I want to make a progress dialog appear for 2 or 3 seconds.
It won't actually do anything other than say progress.
I want to add a delay in progress bar. How can succeed this?
It appears at first but for a very short time.
My code is below:
private void registerNewAccount(String username,String email, String password, String mobile, String gender){
final ProgressDialog progressDialog = new ProgressDialog( RegisterActivity.this);
progressDialog.setCancelable(false);
progressDialog.setIndeterminate(false);
progressDialog.setTitle("Registering New Account");
progressDialog.show();
String uRl = "http://192.168.1.6/Projects/LoginRegisterAndroid/register.php";
StringRequest request = new StringRequest(Request.Method.POST, uRl, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
if (response.equals("Successfull")){
progressDialog.dismiss();
Toast.makeText(RegisterActivity.this, response, Toast.LENGTH_SHORT).show();
startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
finish();
}
else{
progressDialog.dismiss();
Toast.makeText(RegisterActivity.this, response, Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
progressDialog.dismiss();
Toast.makeText(RegisterActivity.this, error.toString(), Toast.LENGTH_SHORT).show();
}
}){
#Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String,String> param = new HashMap<>();
param.put("username", username);
param.put("email", email);
param.put("psw", password);
param.put("mobile", mobile);
param.put("gender", gender);
return param;
}
};
request.setRetryPolicy(new DefaultRetryPolicy(30000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
MySingleton.getmInstance(RegisterActivity.this).addToRequestQueue(request);
}
}
I'd suggest using a Handler perhaps:
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
#Override
public void run() {
// dismiss progress dialog here
}
}, 3000)
This can be done inside the onRepsonse method in your Volley request
Related
am trying to save to save a fingerprint image which is a byte into mysql database from my android app.
public void save(View view) {
String id = efid.getText().toString().trim();
String left_finger_image = new String(Left_Finger_Image);
String right_finger_image = new String(Right_Finger_Image);
String left_finger_iso = new String(Left_Finger_ISOTemplate);
String right_finger_iso = new String(Right_Finger_ISOTemplate);
if(!id.equals("")){
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_SAVE_FINGERPRINT, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d("res", response);
if (response.equals("error")==false) {
Toast.makeText(Enrollment.this, "Fingerprint Enrolled Successfully..", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Enrollment.this, MainActivity.class);
startActivity(intent);
finish();
} else if (response.equals("failure")==true) {
Toast.makeText(Enrollment.this, "Fingerprint Enrolled Failed..", Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(Enrollment.this, error.toString().trim(), Toast.LENGTH_SHORT).show();
}
}){
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> data = new HashMap<>();
data.put("fid", id);
data.put("l_finger",left_finger_image);
data.put("r_finger",right_finger_image);
data.put("l_finger_iso",left_finger_iso);
data.put("r_finger_iso",right_finger_iso);
return data;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
requestQueue.add(stringRequest);
}else{
Toast.makeText(this, "Fields can not be empty!", Toast.LENGTH_SHORT).show();
}
}
I've given permissions for Internet access to my application and the URL I'm using is right (I can do POST requests through postman correctly). But in my application the requests fail:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final String email = Email.getText().toString();
final String password = Password.getText().toString();
StringRequest stringRequest = new StringRequest(Request.Method.POST, server_url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
if (response.equalsIgnoreCase("exitoso")) {
Intent intent = new Intent(MainActivity.this, Home.class);
startActivity(intent);
finish();
} else {
Toast.makeText(MainActivity.this, "Todo mal", Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, "Hubo un error", Toast.LENGTH_SHORT).show();
error.printStackTrace();
}
}) {
#Override
protected Map<String, String> getParams() {
Map<String, String> Params = new HashMap<String, String>();
Params.put("email", email);
Params.put("password", password);
return Params;
}
#Override
public Map<String, String> getHeaders() {
HashMap<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/x-www-form-urlencoded; charset=\"UTF-8");
return headers;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
requestQueue.add(stringRequest);
}
});
My emulator does not give me any responses, just stays on login activity (which is the main one).
Just rebuild the app, honestly it was that now it works.
I tried MYSQL, PHP JSON and VOlley request to log in to the user but when I do without toasting the final response user is not get logged in but when I use toast user gets logged in? why is it so
StringRequest stringRequest = new StringRequest(Request.Method.POST,
URL_LOGIN,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
/////////////////////THIS TOAST I AM TALKING ABOUT
Toast.makeText(getApplicationContext(), jsonObject+"", Toast.LENGTH_LONG).show();
if (!jsonObject.getBoolean("error")) {
startActivity(new Intent(LoginActivity.this, MainActivity.class);
}
if(jsonObject.getBoolean("error")){
if(jsonObject.getString("message").equals("ERROR")){
startActivity(new Intent(LoginActivity.this, Verify.class);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
}) {
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("email", username);
params.put("password", password);
return params;
}
};
stringRequest.setShouldCache(false);
RequestHandler.getInstance(this).addToRequestQueue(stringRequest);
I am trying to login by using volley library. I have used SharedPrefrences to store User name,email,mobile. When I am using correct mobile no. and password. Toast is generating to login successful but not able to move Login Fragment to Dashboard Activity.
Here is the Code of login method
private void login(String login_url, final String getLoginMob, final String getLoginPwd) {
//Progress Dialog code
final Dialog dialog =new Dialog(getActivity());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.loading_dialog);
dialog.setCancelable(false);
dialog.show();
RequestQueue queue = Volley.newRequestQueue(getActivity());
StringRequest postrequest = new StringRequest(Request.Method.POST, login_url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
dialog.dismiss();
try {
JSONObject jsonObject = new JSONObject(response);
if (jsonObject.getBoolean("success") == true ) {
Toast.makeText(getActivity(),jsonObject.getString("message"),Toast.LENGTH_LONG).show();
JSONObject jsonObjectInfo=jsonObject.getJSONObject("User");
sharedPrefrence_main.setName(jsonObjectInfo.getString("name"));
sharedPrefrence_main.setEmail(jsonObjectInfo.getString("email"));
sharedPrefrence_main.setMobile_no(jsonObjectInfo.getString("mobile"));
Intent intent=new Intent(getActivity(), Dashboard.class);
startActivity(intent);
} else if (jsonObject.getBoolean("success") == false) {
Toast.makeText(getActivity(), jsonObject.getString("message"), Toast.LENGTH_LONG).show();
}
else
Toast.makeText(getActivity(),"Entries are wrong",Toast.LENGTH_LONG).show();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
dialog.dismiss();
}
}){
#Override
protected Map<String, String> getParams(){
Map<String,String> param=new HashMap<String, String>();
param.put("mobile_email", getLoginMob);
param.put("password", getLoginPwd);
return param;
}
};
int socketTimeout = 30000;
RetryPolicy policy = new DefaultRetryPolicy(socketTimeout,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
postrequest.setRetryPolicy(policy);
queue.add(postrequest);
}
JSON Response
{"success":"true","message":"Login Sucessfully","User":[{"name":"satishkanaujiya ","email":"*****#gmail.com","mobile":"901589****"}]}
runOnUiThread(new Runnable() {
#Override
public void run() {
//Type your Intent code here
}
});
OR
Use LocalBraodcastManager:
Use this link: how to use LocalBroadcastManager?
This is an array and I was treating it as objects. Finally I corrected my mistakes by following codes.
JSONArray jsonArrays=jsonObject.getJSONArray("User");
for(int i=0;i<jsonArrays.length();i++){
JSONObject jsonObject1=jsonArrays.getJSONObject(i);
sharedPrefrence_main.setName(jsonObject1.getString("name"));
sharedPrefrence_main.setEmail(jsonObject1.getString("email"));
sharedPrefrence_main.setMobile_no(jsonObject1.getString("mobile"));
}
I am creating a button which when clicked calls the checkUserName method which searches the database and tells whether the username is available or not. The URL_CHECK_USERNAME responds "Username Available" or "Username not available".
public void checkUserName(View v){
String username = inputUserName.getText().toString().trim();
new CheckUsername().execute(username);
}
class CheckUsername extends AsyncTask<String, String, String>{
String r = null;
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(RegisterActivity.this);
pDialog.setMessage("Checking Availability of this Username...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
#Override
protected String doInBackground(String... params) {
//String r = null;
final String username = params[0];
StringRequest strReq = new StringRequest(Method.POST,
AppConfig.URL_CHECK_USERNAME, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
r = response;
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError volleyError) {
Log.e(TAG, "Error in checking username." + volleyError.getMessage());
Toast.makeText(getApplicationContext(), volleyError.getMessage(), Toast.LENGTH_SHORT).show();
hideDialog();
}
}){
#Override
protected Map<String, String> getParams() {
Map<String, String> param = new HashMap<>();
//param.put("username", params[0]);
param.put("username", username);
return param;
}
};
AppController.getInstance().addToRequestQueue(strReq);
return r;
}
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
pDialog.dismiss();
Toast.makeText(RegisterActivity.this, s, Toast.LENGTH_SHORT).show();
}
}
This program displays an empty Toast everytime. What can be the problem?
From the code you posted I suppose you are using Volley as your network library.
If that's the case you should be aware that there's no need for that Asynctask of yours because Volley takes care of the threading for you.
The request will be executed in the background and the callback will be called asynchronously. That 's why your toasts are empty.
Create your StringRequest in your checkUsername(View) method and have the toast shown in the onResponse callback.