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.
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 want to change my StringRequest to a JsonArrayRequest in order to separate the return into different variables.
As I run this code below:
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d("res", response);
if (response.equals("failure")) {
Toast.makeText(MainActivity.this, "Ungültige Anmeldedaten", Toast.LENGTH_SHORT).show();
} else {
TV.setText(response);
//ID = Integer.parseInt(response);
//switchToMain();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, error.toString().trim(), Toast.LENGTH_SHORT).show();
}
}){
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> data = new HashMap<>();
data.put("email", email);
data.put("password", password);
return data;
}
};
requestQueue.add(stringRequest);
I return the response:
[{"id":"6","Username":"a","Email":"a","Password":"0cc175b9c0f1b6a831c399e269772661","Status":"Online"}]
, which is a JsonArray, isn´t it?
When I try to make a JsonArrayRequest I receive errors:
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
JsonArrayRequest queue = new JsonArrayRequest(Request.Method.POST,URL,null,new Response.Listener<JSONArray>(){
#Override
public void onResponse(JSONArray response) {
if (response.equals("null")) {
Toast.makeText(MainActivity.this, "Ungültige Anmeldedaten", Toast.LENGTH_SHORT).show();
} else {
//ID = Integer.parseInt(response);
//switchToMain();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, error.toString().trim(), Toast.LENGTH_SHORT).show();
}
}){
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> data = new HashMap<>();
data.put("email", email);
data.put("password", password);
return data;
}
};
requestQueue.add(queue);
I receive the following error:
com.android.volley.ParseError: org.json.JSONException: End of input at character 0 of
Where is my mistake?
I create project in android studio and use Volley for signup and login
I can login in my android app with email and password that i insert in database with phpMyadmin But signup in android doesn't insert email and password to database!
this is my LoginDialog.java code :
public class LoginDialog extends DialogFragment {
EditText edtEmail, edtPass;
Button btnSignup, btnLogin;
OnSignupClicked onSignupClicked;
View view;
#NonNull
#Override
public Dialog onCreateDialog(#Nullable Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
view = LayoutInflater.from(getContext()).inflate(R.layout.login_dialog, null);
setupViews();
builder.setView(view);
return builder.create();
}
private void setupViews() {
btnSignup = (Button) view.findViewById(R.id.btn_loginDialog_signup);
edtEmail = (EditText) view.findViewById(R.id.edt_loginDialog_email);
edtPass = (EditText) view.findViewById(R.id.edt_loginDialog_pass);
btnLogin = (Button) view.findViewById(R.id.btn_loginDialog_login);
btnSignup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String email = edtEmail.getText().toString();
String pass = edtPass.getText().toString();
userSignup(email, pass);
}
});
btnLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
login(edtEmail.getText().toString(), edtPass.getText().toString());
}
});
}
private void login(final String myEmail, final String pass) {
String url = "http://192.168.1.101/login.php";
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
if (response.equals("not found")) {
Toast.makeText(getContext(), "پست الکترونیک یا رمز عبور اشتباه است", Toast.LENGTH_SHORT).show();
} else {
onSignupClicked.onClicked(response);
dismiss();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.i("LOG", "onErrorResponse: " + error.toString());
}
}) {
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("email", myEmail);
params.put("pass", pass);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(getContext());
requestQueue.add(stringRequest);
}
public void setOnSignupClicked(OnSignupClicked onSignupClicked) {
this.onSignupClicked = onSignupClicked;
}
private void userSignup(final String email, final String pass) {
String url = "http://192.168.1.101/signup.php";
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.i("LOG", "onResponse: "+response);
onSignupClicked.onClicked(response);
dismiss();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.i("LOG", "onErrorResponse: " + error.toString());
}
}) {
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("email", email);
params.put("pass", pass);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(getContext());
requestQueue.add(stringRequest);
}
public interface OnSignupClicked {
void onClicked(String email);
}
}
And this my signup.php code :
<?php
include "connect.php";
$email = $_POST["email"];
$pass = $_POST["pass"];
$query = "INSERT INTO user(email,pass)VALUES (:email,:pass)";
$res=$connect->prepare($query);
$res->bindParam(":email",$email);
$res->bindParam(":pass",$pass);
$res->execute();
if($res){
echo $email;
}else{
echo "error";
}
Please guide me how can i fix signup to connect with database :X
Your server side code had bugs. I fixed it
<?php
$connection = mysqli_connect('localhost','root','password','user') or die('connecition err');
$email = $_POST['email'];
$pass = $_POST['pass'];
$sql = "INSERT INTO `user` (`email`,`pass`) VALUES ('$email','$pass')";
$query = mysqli_query($connection,$sql);
if($query){
echo $email;
}else{
echo "no";
}
?>
I am using volley library to send data on server as a json. My server response successfully. Data is saving in database. But Volley execute the ErrorResponse, here is my code
private void upload(final Context context) {
String url = BASE_URL + "upload.php";
RequestQueue requestQueue = Volley.newRequestQueue(context);
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Intent intent=new Intent(ProfilePictureActivity.this,HomeActivity.class);
startActivity(intent);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// Log.i("Error", "" + error);
Toast.makeText(context, "Cannot save", Toast.LENGTH_SHORT).show();
}
}) {
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> stringMap = new HashMap<>();
stringMap.put("name", names);
stringMap.put("date_of_birth",dobs);
stringMap.put("address", presentAddress);
stringMap.put("phone",phone);
stringMap.put("email", email);
stringMap.put("image",phone+".jpg");
stringMap.put("photo",imageToString(bitmap));
return stringMap;
}
};
requestQueue.add(stringRequest);
}
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);