Volley Post Method MultiParams Error - java

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,

Related

Android JSON request with Volley

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

How function execution sequence works in Java?

I have a fragment that sends a Get request to server multiple times but with different arguments expecting different results.
This function accepts arguments and sends request successfully but there is a problem:
First execution of this function returns null but second execution returns first execution's result! and the problem is in execution sequence:
I added some toast comments to clarify: "Toast A" is in get request function and "Toast B" is after the function call. I figured out that "Toast A" is being executed AFTER "Toast B".
As you know by now, I am new to Java and answering this question helps me understand it better. My real question is not about sending Get request. I just want to know How java handles function's execution sequence. Thanks for guiding me.
This is how I call request function:
Map<String, String> header = new HashMap<>();
header.put("user_id", "123");
String[] responseString = get_request(header);
//Toast B
Here is my request function:
public String[] get_request(final Map<String, String> header) {
final String[] responseString = new String[1];
StringRequest stringRequest = new StringRequest(Request.Method.POST, getString(R.string.server) + "query.php", new Response.Listener<String>()
{
#Override
public void onResponse(String response) {
responseString[0] = response;
//Toast A
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError e) {
responseString[0] = "";
}
}) {
#Override
protected Map<String, String> getParams() throws AuthFailureError {
return header;
}
};
Volley.newRequestQueue(getActivity().getApplicationContext()).add(stringRequest);
return responseString;
}

Android Volley unable to get data in response

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.

Volley GET JsonObjectRequest with JSONObjectParameter doesn't work at all

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.

Get request out of onResponse

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?

Categories

Resources