Can any one suggest in Android volley library only post request.
I don't want to wait for response.
StringRequest strReq = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
common.toast(getApplicationContext().getApplicationContext(), error.getMessage());
loader(false);
}
}) {
#Override
protected Map<String, String> getParams() {
return params;
}
};
AppController.getInstance().addToRequestQueue(strReq, tagStringReq);
Related
For example, I have Volley code for StringRequest here.
What do we use to achieve the same result for Retrofit and how does interface look like in this case?
String url = getString(R.string.API_URL) + "/social/revoke-token";
StringRequest postRequest = new StringRequest
(Request.Method.POST, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d("RESPONSE FROM SERVER", response.toString());
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// TODO: Handle error
}
}){
I am trying to establish connection with wit.AI using Volley . However, all i am getting is a 400 error.
public void makeRequest(String url) {
RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
StringRequest postRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Toast.makeText(MainActivity.this, "YOU'RE TOAST", Toast.LENGTH_SHORT).show();
Log.d("accessToken:", response);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError volleyError) {
Log.d("error:", volleyError.toString());
}
}) {
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<String, String>();
headers.put("Authorization:", "Bearer SAMPLETOKENHERE");
return headers;
}
};
queue.add(postRequest);
}
I found my issue . It was because I put a ":" in my authorization . I didn't know I did not needed it. It works now
Someone to help me about this error
image of my screen
thanks!
You must diklaration/adding a ErrorListener
public class MyRequest {
private Context context;
private RequestQueue queue;
public MyRequest(Context context, RequestQueue queue) {
this.context = context;
this.queue = queue;
}
public void register(String pseudo, String password, String password2) {
String url = "http://192.168.56.1/don/connexion.php";
StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
}
}
Your Question about Post Request but Your Code is Get Request.......
try this for POST,
StringRequest stringRequest = new StringRequest(Request.Method.POST, REGISTER_URL,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
//Success
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
//Failure
}
}) {
#Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put(KEY_USERNAME, user);
params.put(KEY_PASSWORD, pass);
return params;
}
};
RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
queue.add(stringRequest);
and you have to pass some params if you need to use Post method
I'm trying to send JSON parameter but server is receiving them as nulls values,
i tried to request it from Postman and it works perfect, i don't know what the problem is with volley i followed the instructions here but it didn't make sense
here is my code
String url = "http://10.10.10.166:8080/SystemManagement/api/Profile/Login";
JSONObject jsonObject=new JSONObject();
try {
jsonObject.put("user_id","Test user name");
jsonObject.put("user_password","test password");
} catch (JSONException e) {
e.printStackTrace();
}
System.out.println(jsonObject.toString());
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest( Request.Method.POST, url, jsonObject,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Toast.makeText(Login.this, response.toString(),Toast.LENGTH_SHORT).show();
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
System.out.println(error.toString());
}
});
//add request to queue
queue.add(jsonObjectRequest);
I faced same problem, solved by overriding the getParams() method
Here is my login request using Volley.
private void loginRequeset() {
showpDialog();
StringRequest jsonObjReq = new StringRequest(Request.Method.POST, Constants.LOGIN_URL, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Toast.makeText(Login.this, response.toString(),Toast.LENGTH_SHORT).show();
hidepDialog();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_SHORT).show();
// hide the progress dialog
hidepDialog();
}
})
{
#Override
protected Map<String, String> getParams() {
Map<String, String> signup_param = new HashMap<String, String>();
signup_param.put(Constants.USERNAME, _emailText.getText().toString().trim());
signup_param.put(Constants.PASSWORD, _passwordText.getText().toString().trim());
return signup_param;
}
};
// Adding request to request queue
queue.getInstance().addToRequestQueue(jsonObjReq);
}
I'm trying to post data to website. It is login operation. I check form data hidden tag etc. In form data there is a hidden input _RequestVerificationToken. That's why first, I made get request to parse _RequestVerificationToken and for headers.
StringRequest _StringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
//Parsing Process
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
}
)
{
#Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
headers=response.headers;
return super.parseNetworkResponse(response);
}
};
After all i made post request with parameters and headers:
#Override
public Map<String, String> getParams()
{
Map<String, String> params = new HashMap<>();
// the POST parameters:
params.put("Password", password);
params.put("RememberMe", "false");
params.put("ReturnUrl", url_post);
params.put("UserName",username);
params.put(name,_RequestVerificationToken);
return params;
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
return headers;
}
I check form data by using fiddler extension in chrome. I checked parameters again and again but it returned code 400. Can you show me what is the problem?
Change Request.Method.GET to Request.Method.POST
StringRequest _StringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
//Parsing Process
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
}
)
{
#Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
headers=response.headers;
return super.parseNetworkResponse(response);
}
};
Or You can use this code:-
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
returnValue = response;
try {
parseFromUrl(response);
System.out.println(returnValue);
} catch (JSONException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(app,error.toString(),Toast.LENGTH_SHORT).show();
}
}) {
#Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("Password", password);
params.put("RememberMe", "false");
params.put("ReturnUrl", url_post);
params.put("UserName",username);
params.put(name,_RequestVerificationToken);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(app);
requestQueue.add(stringRequest);
I solve the problem with this class and I added this:
CookieManager cookieManager = new CookieManager(new PersistentCookieStore(this), CookiePolicy.ACCEPT_ORIGINAL_SERVER);
CookieHandler.setDefault(cookieManager);
before add request to queue