I've got a problem with Volley request - I want to send GET (another similiar POST also) request with JSONObject param (user having password and login) to authorize user and send back full user data. Although I can see during debugging that mRequestBody is correct JSON but I cannot receive it by my controller -
private void processLogin() throws JSONException {
this.user.setLogin(this.loginText.getText().toString());
this.user.setPassword(this.passwordText.getText().toString());
final JSONObject jsonObject = new JSONObject(new Gson().toJson(this.user));
final JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, UrlHelper.loginUrl, jsonObject, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
user = new Gson().fromJson(response.toString(), User.class);
if (user.getUserId().equals(getResources().getString(R.string.ERROR))) {
onLoginFailed();
} else {
onLoginSuccess(user);
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, error.toString());
}
}) {
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json");
return headers;
}
};
VolleySingleton.getInstance(this).addToRequestQueue(request);
}
Controller method:
#RequestMapping(value = "/loginTest", method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
public User someMethod(#RequestBody User user) throws SQLException {
return userService.authenticateUser(user.getLogin(), user.getPassword());
}
Without annotation #RequestBody I it process but User in param is empty, so I process User with null password and login. Recently I did it by StringRequest Please, help me. :)
As a part of the HTTP specs, GET requests have no body. You can create and send GET requests with a body though, but that's meaningless since the body has no semantic meaning in the GET method specification. I don't know too much about Spring, but my guess is that Spring is probably ignoring the body of the request because it's a GET method. You should send and receive the request as POST if your intention is to put something inside the body.
Related
I am trying to register some data with an api and I have checked the json that I get in my application and the structure and data are fine, I put it in Postman and it works fine, the url is also fine, someone could help me to know what I am doing wrong ?
this is the method with which I try to make the call to the api
private void request2(final String json){
// Instantiate the RequestQueue.
RequestQueue mRequestQueue = SingletonRequestQueue.getInstance(getApplicationContext()).getRequestQueue();
// Request a string response from the provided URL.
StringRequest stringRequestPOSTJSON = new StringRequest(Request.Method.POST, BASE_URL + "/api/Asociado/RegistrarAsociado", new Response.Listener() {
#Override
public void onResponse(Object response) {
// response..
}
}, errorListener) {
#Override
public Priority getPriority() {
return Priority.HIGH;
}
#Override
public Map getHeaders() throws AuthFailureError {
HashMap headers = new HashMap();
headers.put("Authorization", "Bearer "+ bearertoken);
return headers;
}
#Override
public byte[] getBody() throws AuthFailureError {
String your_string_json = json; // put your json
return your_string_json.getBytes();
}
};
// Add the request to the RequestQueue.
mRequestQueue.add(stringRequestPOSTJSON);
}
This is the error I get: E/Volley: [4146] BasicNetwork.performRequest: Unexpected response code 400 for
try this code:
stringRequest.setRetryPolicy(new DefaultRetryPolicy(
30000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
I have Spring Boot project on backend and Android app on frontend. Communication between the two happens via Retrofti2. On one of the endpoits, actually /login endpoint, is retruning a HashMap<String, Object>. That endpoint looks like this:
#RequestMapping(value = "/login", method = RequestMethod.POST)
public ResponseEntity<?> login(#RequestBody AuthenticationRequest authenticationRequest) throws Exception {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("user", getUserObject());
map.put("token", getJwtToken());
return ResponseEntity.ok(map);
}
Interface for method call looks like this:
public interface LoginService {
#POST("login")
Call<HashMap<String, Object>> testLogin(#Body Login login); //login contains username and password strings
}
The reposnse in frontend look like this:
Call<HashMap<String, Object>> call = mLoginService.testLogin(new Login(username, password));
call.enqueue(new Callback<HashMap<String, Object>>() {
#Override
public void onResponse(Call<HashMap<String, Object>> call, Response<HashMap<String, Object>> response) {
if (!response.isSuccessful()){
Toast.makeText(getApplicationContext(), "Wrong credentials!", Toast.LENGTH_SHORT).show();
return;
}
//extract user & token
HashMap<String, Object> map = response.body();
Log.i("Test result", map.get("token").toString());
Log.i("Test result", map.get("user").toString());
User u = (User) map.get("user");
//Toast.makeText(getApplicationContext(), "Welcome " + u.getUsername() + "!", Toast.LENGTH_SHORT).show();
startActivity(goToEmailsIntent);
}
#Override
public void onFailure(Call<HashMap<String, Object>> call, Throwable t) {
Toast.makeText(getApplicationContext(), "Cannot login, look at the console", Toast.LENGTH_SHORT).show();
Log.i("ERRROOOOOOR during login", t.toString());
return;
}
});
When I look at the log statements in the console everything is okey, I get the data which I want. But when trying to cast to User object I get exception.
java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to com.example.email.model.User
How to get around this? Is there any better way to send User object and a String from backend?
First method
When you can change your backend service then modify the backend service return DTO not HashMap.
#RequestMapping(value = "/login", method = RequestMethod.POST)
public ResponseEntity<Token> login(#RequestBody AuthenticationRequest authenticationRequest) throws Exception {
// create your response
Token token = new Token();
return ResponseEntity.ok(token);
}
Second method
Use json format to exchange data,in your onResponse method you can do like below:
HashMap<String, Object> map = response.body();
// map.get("user) may return a null value
String jsonString = new Gson().toJson(map.get("user));
User user = new Gson().fromJson(jsonString,User.class);
// do other thing as you want
I am trying to make a web-service call using volley and printing the response in the logcat. But I don't know why I am not getting response. Not even any error message.
Below is my code. I know I am missing something. Please correct me.
private void syncData() {
mProgressDialog.showProgressDialog("Initializing Please Wait...");
RequestQueue requestQueue = Volley.newRequestQueue(SalesDashboard.this);
StringRequest stringRequest = new StringRequest(Request.Method.POST, SYNC_DATA_SALES, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
mProgressDialog.dismissProgressDialog();
Log.e("TAG", response);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
mProgressDialog.dismissProgressDialog();
Log.e("TAG", error.toString());
}
}){
#Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String , String> params = new HashMap<>();
String userrole = mSessionManagement.getLoggedInUser().get(SESSION_USER_ROLE);
params.put("branch", mSessionManagement.getSelectedBranch());
params.put("staff_id", mSessionManagement.getLoggedInUser().get(SESSION_EMP_ID));
params.put("user_role", mSessionManagement.getLoggedInUser().get(SESSION_USER_ROLE));
params.put("user_dept", mSessionManagement.getLoggedInUser().get(SESSION_DEPT_IDS));
params.put("principal", mSessionManagement.getLoggedInUser().get(SESSION_PRINC_IDS));
if (userrole.equals(ADMIN) || userrole.equals(COUNTRY_MANAGER)) {
params.put("user_div", mSessionManagement.getSelectedDivision());
} else {
params.put("user_div", mSessionManagement.getLoggedInUser().get(SESSION_DIV_IDS));
}
return super.getParams();
}
};
requestQueue.add(stringRequest);
}
I am not receiving any error message also or any kind of exception. I have checked the webservice by hitting it on browser the response is displayed correctly on the browser but not able to fetch it in android.
Why you call super.getParams() on return instead params? Maybe it turn wrong on your request. Then try as follow
#Override
protected Map<String, String> getParams() throws AuthFailureError {
...
return params;
}
Bonus: use getMessage instead do toString() your error object
Print the stringRequest and check the url and cross verify with ur url
Before that check have u added Internet permissions in manifest file.
I was trying Post method with using Volley and I got: com.android.google.volley server error.
I tested my api in postman and it works.
Here is my code:
StringRequest request=new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d("Response", response);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.e("Error :",error.getMessage());
}
}) {
#Override
public Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("name", film.toString());
params.put("email", spin.toString());
params.put("password", spin_iki.toString());
params.put("address", spin_uc.toString());
params.put("brand", name.toString());
return params;
}
};
RequestQueue queue= Volley.newRequestQueue(film.this);
queue.add(request);
}
});
Normally I use my api like http://myurl/api/values/Post?film=...&email=...
How can I fix this? Thank you.This my API's Post method code
[HttpPost]
public int Post_film(string name, string email, string password, string address, string brand)
{
using (var context = new Entities())
{
db_table nesne = new db_table();
nesne.name =name;
nesne.email = email;
nesne.password = password;
nesne.address = address;
nesne.brand = brand;
context.db_table.Add(nesne);
int i = context.SaveChanges();
return i;
}
}
this my Postman SS
enter image description here
OK, you're sending a POST request but still adding parameters via URL. If your server is taking these parameters from the URL, then your Volley request will never work because overriding getParams() only works for parameters inside the request body, not parameters attached to the URL. You should either change the way your server is taking the parameters or, in your Android code, generate a string URL with all the parameters attached to it like "http:://myUrl.com/?email=something&name=something". To me, the most logical option is to change your server side code, because it doesn't make too much sense to expect a POST request with parameters in the URL. Parameters in POST requests should be inside the request body,
I have a method that does a GET request using Volley Library
public void get(String url) {
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
System.out.println("Successfull response is: " + response);
}
}, createMyReqErrorListener()) {
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<>();
headers.put(ACCEPT, ACCEPT_JSON);
headers.put(AUTH_ID, AUTH_ID_VALUE);
headers.put(PLATFORM, PLATFORM_VALUE);
headers.put(CID, "");
System.out.println(headers.toString());
return headers;
}
};
// Add the request to the RequestQueue.
queue.add(stringRequest);
}
In the onResponse Override method i would like to get the response object passed onto my parser which is in a data class, which will then pass the parsed info (images/name etc) to the activity so it can display it?
How could i pass the object up the chain?