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?
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 am trying to start an JSONArrayRequests after the StringRequest has finished. The StringRequest is POST requet and the JSONArrayRequest is GET request. I want the JSONArray to start when the params of the String is uploaded so as I will be able to retrieve this data again. Below is my code:
RequestQueue requestQueue = Volley.newRequestQueue(getContext());
StringRequest stringRequest = new StringRequest(Request.Method.POST, JSON_URL_UPLOAD_IMAGE, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
if (response.equals("1")) {
JsonArrayRequest arrayRequest = new JsonArrayRequest(Request.Method.GET, JSON_URL, null,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
for (int i=0;i<response.length();i++) {
try {
JSONObject object = response.getJSONObject(i);
String USER_ID = object.getString("user_id");
String POST_DATE = object.getString("post_date");
if (USER_ID.equalsIgnoreCase(CurrentUser)){
Toast.makeText(getContext(), "First if.", Toast.LENGTH_SHORT).show();
if (POST_DATE.equalsIgnoreCase(dateTime)) {
Toast.makeText(getContext(), "Second if.", Toast.LENGTH_SHORT).show();
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley Error", error.getMessage());
}
});
requestQueue.add(arrayRequest);
requestQueue.start();
}
}catch (Exception e) {
e.printStackTrace();
Log.d("ERROR", e.getMessage().toString());
Toast.makeText(getContext(), "ERROR:"+e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
}){
#Nullable
#Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
params.put("post_author", CurrentUser);
if (!details.getText().toString().equalsIgnoreCase("")){
params.put("post_content", details.getText().toString());
params.put("post_excerpt", details.getText().toString());
}else {
params.put("post_content", "");
params.put("post_excerpt", "");
}
params.put("post_date", dateTime);
params.put("post_date_gmt", dateTime);
params.put("post_title", CurrentUser+"-"+CurrentUser+"-"+timeStampStr);
params.put("post_status", "publish");
params.put("comment_status", "closed");
params.put("ping_status", "closed");
params.put("post_name", CurrentUser+"-"+CurrentUser+"-"+timeStampStr);
params.put("post_modified", dateTime);
params.put("post_modified_gmt", dateTime);
params.put("guid", "https://www.selfcial.com/peepso-post/"+CurrentUser+"-"+CurrentUser+"-"+timeStampStr+"/");
params.put("post_type", "peepso-post");
return params;
}
};
requestQueue.add(stringRequest);
requestQueue.start();
What I tried to do is to add a RequestQueue in order to make the requests wait until the other one is finished but it didn't work. The problem I have is that when the inside request starts it doesn't find the "if" statement(namely this:(USER_ID.equals(CurrentUser) && POST_DATE.equals(dateTime)). So, what I can understand is that the second starts before the first has finished with the params. What can I do? Any answer will be appreciated!
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 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 want to use from open street map API
when i send request to Reverse Geo coding Api with volley
This error is displayed
com.android.volley.authfailureerror
But this API don't need to any token
this is my Code:
String url = "https://nominatim.openstreetmap.org/reverse?format=json&lat="+lat+"&lon="+lng;
Response.Listener<JSONObject> listener = new Response.Listener<JSONObject>()
{
#Override
public void onResponse(JSONObject response)
{
try
{
JSONObject address = response.getJSONObject("address");
final String city = address.getString("town");
final String county = address.getString("county");
selectcity = true;
String temp = county.substring(county.indexOf(" "),county.length());
ed.putString("lat", lat+"");
ed.putString("lng", lng+"");
ed.putString("location", temp);
ed.apply();
progressDialog.dismiss();
RequestQueue mRequestQueue;
mRequestQueue =AppController.getInstance().getRequestQueue();
mRequestQueue.cancelAll("AppController");
locationManager.removeUpdates(LocationActivity.this);
}
catch (JSONException e)
{
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Erorr", Toast.LENGTH_LONG).show();
RequestQueue mRequestQueue;
mRequestQueue =AppController.getInstance().getRequestQueue();
mRequestQueue.cancelAll("AppController");
progressDialog.dismiss();
locationManager.removeUpdates(LocationActivity.this);
}
}
};
Response.ErrorListener errorListener = new Response.ErrorListener()
{
#Override
public void onErrorResponse(VolleyError error)
{
Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG).show();
RequestQueue mRequestQueue;
mRequestQueue =AppController.getInstance().getRequestQueue();
mRequestQueue.cancelAll("AppController");
progressDialog.dismiss();
locationManager.removeUpdates(LocationActivity.this);
}
};
final JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, listener, errorListener){
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
headers.put("accept-language", "fa");
return headers;
}
};
request.setTag("AppController");
AppController.getInstance().addToRequestQueue(request);
In addition when I use from this URL in postman It works correctly
try this way
RequestQueue requestQueue = Volley.newRequestQueue(mContext);
StringRequest stringRequest = new StringRequest(Request.Method.GET, Your_URL, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
//response
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d("===","onErrorResponse "+error)
}
}) {
#Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
#Override
public byte[] getBody() throws AuthFailureError {
try {
Log.d("===","AuthFailureError");
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", requestBody, "utf-8");
return null;
}
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/json");
return params;
}
#Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
String responseString = "";
if (response != null) {
responseString = String.valueOf(response.statusCode);
// can get more details such as response.headers
try {
} catch (Exception e) {
}
}
return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response));
}
};
stringRequest.setRetryPolicy(new RetryPolicy() {
#Override
public int getCurrentTimeout() {
return 50000;
}
#Override
public int getCurrentRetryCount() {
return 50000;
}
#Override
public void retry(VolleyError error) throws VolleyError {
}
});
requestQueue.add(stringRequest);