My app performs uploading operation to backend php server.My problem is i cannot dismiss the progressdialog inside volley Onresponse() method.Onresponse() method gets executed and toast is displayed in background of the Progressdialog so that means Onresponse() gets called currectly.
I declared ProgressDialog variable in public
onCreate() method look like this
ProgressDialog dialog;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.signup);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog=new ProgressDialog(context,ProgressDialog.STYLE_SPINNER);
dialog.setIndeterminate(true);
dialog.show(context,"Signing up","Please wait...");
login_call();
}
Login_call() performs volley request and inside the onResponse() i need to dismiss the dialog but its not working.
public void login_call()
{
RequestQueue queue= Volley.newRequestQueue(getApplicationContext());
StringRequest request = new StringRequest(Request.Method.POST, "http://192.168.56.1/sign.php", new Response.Listener<String>() {
#Override
public void onResponse(String response) {
if (response.equals("success"))
{
dialog.dismiss();
getApplicationContext().startActivity(new Intent(getApplicationContext(), MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
}
else
{
dialog.dismiss()
Toast.makeText(getApplicationContext(), response, Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
dialog.dismiss();
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
}
}
) {
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> param = new HashMap<>();
param.put("user",user.getText().toString());
param.put("pass",pass.getText().toString());
param.put("name",name.getText().toString());
param.put("mob",mob.getText().toString());
param.put("email",email.getText().toString());
param.put("photo",photo);
return param;
}
};
queue.add(request);
}
Toast message is displaying correctly but progressdialog is not gettting dismissed.
Any ideas to solve the problem??
Just change this :
RequestQueue queue= Volley.newRequestQueue(getApplicationContext());
StringRequest request = new StringRequest(Request.Method.POST, "http://192.168.56.1/sign.php", new Response.Listener<String>() {
#Override
public void onResponse(String response) {
dialog.dismiss(); // write it there
if (response.equals("success")) {
// dialog.dismiss(); remove this from here..!!
getApplicationContext().startActivity(new Intent(getApplicationContext(), MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
);
} else {
Toast.makeText(getApplicationContext(), response, Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
dialog.dismiss();
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
}
}
Edit : set this
dialog.setIndeterminate(false);
first in first you didn't check all conditions
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
if (response.equals("success")) {
dialog.dismiss();
getApplicationContext().startActivity(new Intent(getApplicationContext(), MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
**} else {
Toast.makeText(getApplicationContext(), response, Toast.LENGTH_LONG).show();
}**
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
dialog.dismiss();
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
}
The else block doesn't hide the dialog so you should change it in this way
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
dialog.dismiss();
if (response.equals("success")) {
getApplicationContext().startActivity(new Intent(getApplicationContext(), MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
} else {
Toast.makeText(getApplicationContext(), response, Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
dialog.dismiss();
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
}
I think that the mistake is this, of course assuming that your dialog is accessible from the callback
Change your Dialog.show method to use this version: show(Context context, CharSequence title, CharSequence message, boolean indeterminate, boolean cancelable) like this:
final ProgressDialog dialog = ProgressDialog.show(context, "Signing up","Please wait...", false, true);
So your onClick code should change to something like this:
public void onClick(View view) {
final ProgressDialog dialog = ProgressDialog.show(context, "Signing up","Please wait...", false, true);
login_call();
}
Please give this a try and let me know if it helps.
Related
I'm creating a registration page.
I'm getting an error on if (!(password.equals(user_conform)))as "'equals()' between objects of in-convertible types 'Edit Text' and 'String'". I've spent too much time on it to resolve but can't overcome this problem.
Here is my code
EditText name,email,password,conPassword;
Button register;
AlertDialog.Builder builder;
String URL="http://192.168.1.8/Register/user.php" ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
name=findViewById(R.id.name);
email=findViewById(R.id.email);
password=findViewById(R.id.password);
conPassword=findViewById(R.id.c_password);
builder=new AlertDialog.Builder(Register.this);
register=findViewById(R.id.register);
register.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final String user_name=name.getText().toString();
final String user_email=email.getText().toString();
final String user_password=password.getText().toString();
String user_conform=conPassword.getText().toString();
if (user_name.equals("")||user_email.equals("")||user_password.equals("")||user_conform.equals("")){
builder.setTitle("Something Went Wrong...");
builder.setMessage("Please fill all the fields...");
displayAlert("input_error");
}
else {
if (!(password.equals(user_conform))){
builder.setTitle("Something Went Wrong...");
builder.setMessage("Your Passwords are not matching...");
displayAlert("input_error");
}
else {
StringRequest stringRequest=new StringRequest(Request.Method.POST, URL,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
JSONArray jsonArray=new JSONArray(response);
JSONObject jsonObject=jsonArray.getJSONObject(0);
String code=jsonObject.getString("code");
String message=jsonObject.getString("message");
builder.setTitle("Server Response...");
builder.setMessage(message);
displayAlert(code);
} 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("name",user_name);
params.put("email",user_email);
params.put("password",user_password);
return params;
}
};
MySingleton.getInstance(Register.this).addToRequestQueue(stringRequest);
}
}
}
});
}
public void displayAlert(final String code){
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
if (code.equals("input_error")){
password.setText("");
conPassword.setText("");
}
else {
if (code.equals("reg_success")){
finish();
}
else {
if (code.equals("reg_failed")){
name.setText("");
email.setText("");
password.setText("");
conPassword.setText("");
}
}
}
}
});
AlertDialog alertDialog=builder.create();
alertDialog.show();
}
}
EditText is a view object. You'd want to change if (!(password.equals(user_conform))) to password.getText().toString() to turn it into a string. Essentially, your if statement should look like this: if (!(password.getText().toString().equals(user_conform))).
However, you already have a variable for this, so you can just do this: if (!(user_password.equals(user_conform)))
You've mistaken the variables. Replacing if(!(password.equals(user_conform))) with if(!(user_password.equals(user_conform))) should solve your problem.
how can i create notification
when the date current equals date in the base data(mysql)
then the notification created with information.
i dont know how can i create the notification with this setting
please help me it's for project
btn.setOnClickListener(new View.OnClickListener() {
#override
public void onClick(final View view) {
StringRequest request = new StringRequest(Request.Method.POST, insert, new Response.Listener<String>() {
#override
public void onResponse(String response) {
}
}, new Response.ErrorListener() {
#override
public void onErrorResponse(VolleyError error) {
progressDialog.dismiss();
Toast.makeText(partietache.this,error.toString(),Toast.LENGTH_LONG).show();
}
}){
#override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> para = new HashMap<String , String>();
para.put("taskname", ed1.getText().toString().trim());
para.put("totalwork",ed4.getText().toString().trim());
para.put("datetask",ed2.getText().toString().trim());
para.put("starttime",ed3.getText().toString().trim());
para.put("description",ed5.getText().toString().trim());
return para;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(partietache.this);
requestQueue.add(request);
Snackbar snackbar = Snackbar.make(view,"Add with Succesful" , Snackbar.LENGTH_LONG);
snackbar.show();
new Timer().schedule(new TimerTask(){
public void run() {
partietache.this.runOnUiThread(new Runnable() {
#override
public void run() {
finish();
}
});
}
} , 4000);
}
});
#override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (not.isChecked()) {
Toast.makeText(getApplicationContext() , "Notification " + not.getTextOn().toString() , Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext() , "Notification " + not.getTextOff().toString() , Toast.LENGTH_SHORT).show();
}
}
In order to use push notifications you will need to do both server-side and client-side coding. Describing the whole process in detail would be too much for an SO answer, you should take a look at one of the many tutorials available online. Here's one to get you started.
I want to POST data to server but i get
Volley: [1726] BasicNetwork.performRequest: Unexpected response code 415 for http://192.158.20.43:8080/Api/employee/create" error.
public class RegisterTextActivity extends AppCompatActivity {
EditText emailBox, passwordBox,firstName,lastName;
Button registerButton;
TextView loginLink;
String URL = "http://192.158.20.43:8080/Api/employee/create";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register_text);
firstName=(EditText)findViewById(R.id.firstname);
lastName=(EditText)findViewById(R.id.lastname);
emailBox = (EditText)findViewById(R.id.emailBox);
passwordBox = (EditText)findViewById(R.id.passwordBox);
registerButton = (Button)findViewById(R.id.registerButton);
loginLink = (TextView)findViewById(R.id.loginLink);
registerButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
StringRequest request = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>(){
#Override
public void onResponse(String s) {
if(s.equals("true")){
Toast.makeText(RegisterTextActivity.this, "Registration Successful", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(RegisterTextActivity.this, "Can't Register", Toast.LENGTH_LONG).show();
}
}
},new Response.ErrorListener(){
#Override
public void onErrorResponse(VolleyError volleyError) {
volleyError.printStackTrace();
Toast.makeText(RegisterTextActivity.this, "Some error occurred -> "+volleyError, Toast.LENGTH_LONG).show();
}
}) {
#Override
public String getBodyContentType() {
return "application/json;charset=utf-8";
}
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("firstname",firstName.getText().toString().trim());
parameters.put("lastname",lastName.getText().toString().trim());
parameters.put("email", emailBox.getText().toString().trim());
parameters.put("phone", passwordBox.getText().toString().trim());
return parameters;
}
};
RequestQueue rQueue = Volley.newRequestQueue(RegisterTextActivity.this);
/
rQueue.add(request);
}
});
loginLink.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(RegisterTextActivity.this, LoginTestActivity.class));
}
});
}
}
API work fine in POSTMAN but not work properly here.
try changing your Content-Type
application/json;charset=utf-8
to
application/json
I'm creating an IoT project through which I will be able to control home devices and utilities over internet. In the below code, it'll open a curtain when the switch is enabled. What I want to do here is to disable the button until the curtain is fully open which takes 5 seconds of time, so the user won't be able to press the button to close the curtain until it is fully open (because of limitations in servo motor). I've wrote the below code to accomplish this. It uses the Timer class. But it is not disabling the switch at all. Any help would be appreciated. Thanks!
curtain.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
if (response.equals("open")) {
Toast.makeText(getApplicationContext(), "Curtain is opening..", Toast.LENGTH_LONG).show();
final long period = 5000;
new Timer().schedule(new TimerTask() {
#Override
public void run() {
curtain.setEnabled(false);
}
}, System.currentTimeMillis(), period);
curtain.setEnabled(true);
} else {
Toast.makeText(getApplicationContext(), "Failed to open", Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), "Connection failed. Please check your internet connection.", Toast.LENGTH_SHORT).show();
error.printStackTrace();
}
}) {
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> getData = new HashMap<>();
getData.put("device", "curtain");
getData.put("action", "open");
return getData;
}
};
IotSingleton.getInstance(MainActivity.this).addToRequestQue(stringRequest);
}
Simply use Handler as below:
...
#Override
public void onResponse(String response) {
if (response.equals("open")) {
buttonView.setEnabled(false);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
buttonView.setEnabled(true);
}
}, 5000);
} else {
Toast.makeText(getApplicationContext(), "Failed to open", Toast.LENGTH_SHORT).show();
}
}
...
See if it works for you...!!
I am Creating an application which perform an OTP verification method..
I implemented the code as described in this link http://www.androidhive.info/2015/08/android-adding-sms-verification-like-whatsapp-part-2/
But when i click the NEXT button in app app is force closing
The function i get error is :
private void requestForSMS(final String name, final String email, final String mobile) {
StringRequest strReq = new StringRequest(Request.Method.POST,
Config.URL_REQUEST_SMS, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d(TAG, response.toString());
try {
JSONObject responseObj = new JSONObject(response);
// Parsing json object response
// response will be a json object
boolean error = responseObj.getBoolean("error");
String message = responseObj.getString("message");
// checking for error, if not error SMS is initiated
// device should receive it shortly
if (!error) {
// boolean flag saying device is waiting for sms
pref.setIsWaitingForSms(true);
// moving the screen to next pager item i.e otp screen
viewPager.setCurrentItem(1);
txtEditMobile.setText(pref.getMobileNumber());
layoutEditMobile.setVisibility(View.VISIBLE);
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(),
"Error: " + message,
Toast.LENGTH_LONG).show();
}
// hiding the progress bar
progressBar.setVisibility(View.GONE);
} catch (JSONException e) {
//Toast.makeText(getApplicationContext(),"Error: " + e.getMessage(),Toast.LENGTH_LONG).show();
progressBar.setVisibility(View.GONE);
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
//Log.e(TAG, "Error: " + error.getMessage());
//Toast.makeText(getApplicationContext(),error.getMessage(), Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
}
}) {
/**
* Passing user parameters to our server
* #return
*/
#Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("name", name);
params.put("email", email);
params.put("mobile", mobile);
//Log.e(TAG, "Posting params: " + params.toString());
return params;
}
};
// Adding request to request queue
MyApplication.getInstance().addToRequestQueue(strReq);
}
Log is :
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at com.my.application.activity.SmsActivity.requestForSMS(SmsActivity.java:245)
at com.my.application.activity.SmsActivity.validateForm(SmsActivity.java:157)
at com.my.application.activity.SmsActivity.onClick(SmsActivity.java:117)
at android.view.View.performClick(View.java:4211)
at android.view.View$PerformClick.run(View.java:17446)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5336)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Volley library is used..
Any help??
public void confirmOTP() throws JSONException {
LayoutInflater lf = LayoutInflater.from(SignUp.this);
View view = lf.inflate(R.layout.confirm_dialog, null);
confirmButton = (Button) view.findViewById(R.id.buttonConfirm);
inputOTP = (EditText) view.findViewById(R.id.editTextOtp);
AlertDialog.Builder dialog = new AlertDialog.Builder(SignUp.this);
dialog.setView(view);
final AlertDialog alertDialog = dialog.create();
alertDialog.show();
alertDialog.setCanceledOnTouchOutside(false);
confirmButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
alertDialog.dismiss();
final ProgressDialog load = ProgressDialog.show(SignUp.this, "Authenticating", "Please Wait ...", false, false);
load.setCanceledOnTouchOutside(false);
final String otp = inputOTP.getText().toString().trim();
StringRequest stringRequest = new StringRequest(Request.Method.POST, ConfingDetalsActivity.CONFIRM_URL, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d("Confirm Response Check>",response.toString());
String mesage;
try {
JSONObject jsonObject=new JSONObject(response);
mesage=jsonObject.getString("msg");
if(mesage.equals("success")){
Toast.makeText(SignUp.this, "Registerd sucessfully", Toast.LENGTH_LONG).show();
load.dismiss();
Intent in = new Intent(SignUp.this, SignupSucess.class);
startActivity(in);
} else {
load.dismiss();
Toast.makeText(SignUp.this, "Wrong OTP Please try again", Toast.LENGTH_SHORT).show();
confirmOTP();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
load.dismiss();
Toast.makeText(SignUp.this, error.getMessage(), Toast.LENGTH_SHORT).show();
}
}
) {
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map params = new HashMap();
//Adding the parameters otp and username
params.put(ConfingDetalsActivity.KEY_OTP, otp);
if (businessFrag) {
} else {
params.put(ConfingDetalsActivity.CONSUMER_EMAIL, constant.EMAIl);
}
return params;
}
};
requestQueue.add(stringRequest);
}
});
}
public void register() {
Log.d("RegisterCalled>>>>>>>>>", "register");
final ProgressDialog load = ProgressDialog.show(SignUp.this, "Loading", "Pleas wait...", false, false);
load.setCanceledOnTouchOutside(false);
StringRequest request = new StringRequest(Request.Method.POST, ConfingDetalsActivity.REGISTER_URL, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d("Response Check>>>",response.toString());
load.dismiss();
try {
JSONObject jsonObject = new JSONObject(response);
String message=jsonObject.getString("msg");
if (message.equals("success")) {
confirmOTP();
} else {
Toast.makeText(SignUp.this, "User Name or phone already register", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
load.dismiss();
Toast.makeText(SignUp.this, error.getMessage(), Toast.LENGTH_SHORT).show();
}
}) {
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> parms = new HashMap<String,String>();
if (businessFrag) {
parms.put(ConfingDetalsActivity.BUSINESS_ZIP_CODE, constant.B_ZIP_CODE);
parms.put(ConfingDetalsActivity.BUSINESS_INDUSTRY, constant.B_INDUSTRY);
parms.put(ConfingDetalsActivity.BUSINESS_PHONE, constant.B_MOBILE);
parms.put(ConfingDetalsActivity.BUSINESS_EMAIL, constant.B_EMAIl);
parms.put(ConfingDetalsActivity.BUSINESS_PASSWORD, constant.B_PASSSOWRD);
parms.put(ConfingDetalsActivity.SIGNUP_TYPE,"1");
} else {
parms.put(ConfingDetalsActivity.CONSUMER_FNAME, constant.F_NAME);
parms.put(ConfingDetalsActivity.CONSUMER_LNAME, constant.L_NAME);
parms.put(ConfingDetalsActivity.CONSUMER_PHONE, constant.MOBILE);
constant.EMAIl);
parms.put(ConfingDetalsActivity.CONSUMER_PASSWORD, constant.PASSSOWRD);
parms.put(ConfingDetalsActivity.SIGNUP_TYPE,"0");
}
return parms;
}
};
requestQueue.add(request);
}