Without toast JSON response is not working why? - java

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);

Related

How to save a byte into mysql database in java android

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();
}
}

How to change a StringRequest into a JsonArrayRequest?

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?

Apps need to restarted to refresh profile image

recently i've made a program that can upload image to mysql database via php, and now i'm gonna put that image to my circleImageView, but i've encountered some issue, if i do image update, the image won't refresh until i close the apps and run it again.
here is my code for displaying the image and others from mysql database
public void putAllUserData(final String username){
StringRequest stringRequest = new StringRequest(Request.Method.POST, databaseURL.viewUserData, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("data");
for(int i = 0; i < jsonArray.length();i++){
JSONObject object = jsonArray.getJSONObject(i);
setUsername.setText(username);
setFullName.setText(object.getString("nama"));
setCountry.setText(object.getString("asal_negara"));
setGender.setText(object.getString("jenis_kelamin"));
prfImage = object.getString("image_path");
if(prfImage.isEmpty()){
Toast.makeText(profileActivity.this, "", Toast.LENGTH_SHORT).show();
}
else {
Picasso.get().load(prfImage).into(profileImage);
}
}
} catch (JSONException e) {
Toast.makeText(profileActivity.this, "Error " + e, Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(profileActivity.this, "Error " + error, Toast.LENGTH_SHORT).show();
}
}){
#Nullable
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("username", username);
return params;
}
};
Volley.newRequestQueue(this).add(stringRequest);
}

Volley POST StringRequest not giving any response

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.

Volley execute wrong method

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);
}

Categories

Resources