volley unable to send data to server - android

I am using this code
StringRequest stringRequest = new StringRequest(Request.Method.POST,
uploadUrl,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d("sfhyoutubeghhj",response);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
//loading.dismiss();
}
}) {
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> prams = new HashMap<>();
prams.put("aaaa", "1111");
prams.put("bbbb", "2222");
return prams;
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
headers.put("User-agent", "My useragent");
return headers;
}
};
RequestQueue requestQueuea = Volley.newRequestQueue(this);
requestQueuea.add(stringRequest);
Request is going to server and response is also coming but its not sending any variable from android either in GET or POST method
and i m just using
print_r($_REQUEST);
at PHP end

I think there was a problem with Request queue only
working fine with this code # Ali Azhar's Answer
StringRequest sr = new StringRequest(Request.Method.POST, url , new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d(TAG, response.toString());
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
Log.d(TAG, ""+error.getMessage()+","+error.toString());
}
}){
#Override
protected Map<String,String> getParams(){
Map<String, String> params = new HashMap<String, String>();
params.put("id", "28");
params.put("value", "1");
return params;
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> headers = new HashMap<String, String>();
headers.put("Content-Type","application/x-www-form-urlencoded");
headers.put("abc", "value");
return headers;
}
};
AppController.getInstance().addToRequestQueue(sr);

Related

Url working in Retrofit but not working in Volley

I have Url for Login. Its working and give response in Postman and Retrofit but not working with volley.
In volley its give error: com.android.volley.RedirectError
Why i geeting RedirectError. url working and give response in retrofit as well in postman.
Here is my Code:
public void Login(){
final String REGISTER_URL = "Here_My_Url";
StringRequest stringRequest = new StringRequest(Request.Method.POST, REGISTER_URL,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d("onResponse", response);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("eee123",error.toString());
Toast.makeText(getApplicationContext(), error.getMessage(),
Toast.LENGTH_SHORT).show();
}
}) {
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("user_email", st_email);
params.put("password", st_pass);
Log.e("params", " " + params);
}
return params;
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
return headers;
}
};
stringRequest.setRetryPolicy(new DefaultRetryPolicy(0, -1,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
stringRequest.setShouldCache(false);
VolleySingleton.getInstance(this).addToRequestQueue(stringRequest);
}

How to pass params and add authorization header in GET Request in Volley

I am trying to retrieve a response from my server in my Android App but getting "com.android.volley.ServerError"
E/Volley: [224669] BasicNetwork.performRequest: Unexpected response code 400 for http://52.74.115.250:8080/api/products
When I try in Postman,I do get a response.
This is my code for GET request:
JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.GET, Constants.productListUrlStr, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Log.d("jsonsucces","success");
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d("jsonerror",error.toString());
}
}) {
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", Constants.getToken(getActivity()));
//headers=globalProvider.addHeaderToken(getActivity());
return headers;
}
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
if (contract!=null){
if(contract._supplier!=null){
Log.d("checksupplier",contract._supplier);
params.put("_supplier",contract._supplier);
}
}
if(!globalProvider.ShangpingHeaderLoadCategory .equals("")){
params.put("category",globalProvider.ShangpingHeaderLoadCategory);
Log.d("checkcategoryhe",globalProvider.ShangpingHeaderLoadCategory);
}
return params;
}
};
globalProvider.addRequest(jsonObjectRequest);
what am I doing wrong?
There are few posts about getParams() not working anymore for JsonObjectRequest volley request.
Alternatively either you can use StringRequest or do a little tweak in your request as below
JSONObject jOb = new JSONObject();
try {
jOb.put("_supplier", contract._supplier);
jOb.put("category", globalProvider.ShangpingHeaderLoadCategory);
} catch (JSONException e) {
e.printStackTrace();
}
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, Constants.productListUrlStr, jOb, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Log.d("jsonsucces","success");
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d("jsonerror",error.toString());
}
}) {
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", Constants.getToken(getActivity()));
//headers=globalProvider.addHeaderToken(getActivity());
return headers;
}
};
I figured out,Since I am passing query parameter I was constructing my url incorrectly.
String uri= String.format(Constants.productListUrlStr+"?_supplier=%1$s",contract._supplier);
StringRequest stringRequest=new StringRequest(Request.Method.GET, uri, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d("checksuc",response);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d("checkstrerr",error.networkResponse.headers.toString());
}
})
{
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", Constants.getToken(getActivity()));
//headers=globalProvider.addHeaderToken(getActivity());
return headers;
}
}
;
globalProvider.addRequest(stringRequest);

Sending post parameters with JsonObjectRequest volley to active campaign api

I am adding contacts to active campaign api but the request is not sending the post parameters.The parameters are being sent from postman but with volley its not working. I have tried sending params from the constructor also but no progress. Here is the code.
Map<String, String> params = new HashMap();
params.put("email", "wff#dd.com");
params.put("p[1]", "1");
//JSONObject parameters = new JSONObject(params);
RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
url="https://brumano.api-us1.com/admin/api.php?api_key=key&api_action=contact_add&api_output=json";
Log.d("url",url);
JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST,url,null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Log.d("url",response.toString());
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
Log.d("url",error.toString());
error.printStackTrace();
}
}){
#Override
public byte[] getBody() {
HashMap<String, String> params2 = new HashMap<String, String>();
params2.put("email", "w#sss.com");
params2.put("p[1]", "1");
return new JSONObject(params2).toString().getBytes();
}
#Override
public String getBodyContentType() {
return "application/x-www-form-urlencoded;";
}
#Override
protected Map<String, String> getParams() {
Map<String, String> params2 = new HashMap<String, String>();
params2.put("email", "w#sss.com");
params2.put("p[1]", "1");
return params2;
}
}
};
queue.add(jsObjRequest);
Try this using StringRequest
StringRequest jsonObjRequest = new StringRequest(Request.Method.POST,
"https://brumano.api-us1.com/admin/api.php?api_key=key&api_action=contact_add&api_output=json",
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d("url",response.toString());
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("volley", "Error: " + error.getMessage());
}
}) {
#Override
public String getBodyContentType() {
return "application/x-www-form-urlencoded; charset=UTF-8";
}
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("email", "w#sss.com");
params.put("p[1]", "1");
return params;
}
};
queue.add(jsonObjRequest);
check this, with stringRequest like this
//Tested on PostMan
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("userName", "cyborg91mv#gmail.com");
params.put("password", "qwerty");
System.out.println(params);
return params;
}
Now if your server expects json, then make jsonRequest
JSONObject jsonObjectBody = new JSONObject();
jsonObjectBody.put("userName", "cyborg91mv#gmail.com");
jsonObjectBody.put("password", "qwerty");

Volley request showing com.android.volley.ServerError

Volley request showing com.android.volley.ServerError also it is neccessary to implement getHeaders() method? What is exact use of this method?
send.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(Request.Method.POST, URL, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Toast.makeText(getApplicationContext(),response.toString(),Toast.LENGTH_LONG).show();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
}){
#Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put("username","Admin");
params.put("password", "123456789");
return params;
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/x-www-form-urlencoded");
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
requestQueue.add(jsonObjectRequest);
}
});
Generally this type of error occurs when you are using poor internet connection or else your server goes down. There is no need to implement getHeaders() method. Try to just check your connection or server connection.
Use the android volley library,
compile 'com.android.volley:volley:1.0.0'
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
url, null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
Toast.makeText(getApplicationContext(),response.toString(),Toast.LENGTH_LONG).show();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, "Error: " + error.getMessage());
}
}) {
#Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("username","Admin");
params.put("password", "123456789");
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
requestQueue.add(jsonObjectRequest);
Hope this will help you (Happy Coding !).

posting params as json in android volley

I am using volley library for making request. I need to post param as json array because I am receiving it on the other side as json array. How can I convert my params to Json array?
here is my code
public void SendData() {{
final StringRequest strReq = new StringRequest(Request.Method.POST, GET_STUDENTS_BY_ID, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.v("failedd",response);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("Error", "Registration Error: " + error.getMessage());
}
})
{
#Override
protected Map<String, String> getParams() {
// Posting params to register url
Map<String, String> params = new HashMap<String, String>();
params.put("user_id", user_id);
params.put("student_id", studId);
params.put("to", tomail);
params.put("subject", subjects);
params.put("description", descriptions);
return params;
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
return headers;
}
};
AppController.getInstance().addToRequestQueue(strReq, tag_json_obj);
}
please help..
public void SendData() {
Map<String, String> params = new HashMap<String, String>();
params.put("user_id", user_id);
params.put("student_id", studId);
params.put("to", tomail);
params.put("subject", subjects);
params.put("description", descriptions);
JSONObject parameters = new JSONObject(params);
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,GET_STUDENTS_BY_ID,parameters,new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
}
}) {
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
return headers;
}
};
AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj);
}

Categories

Resources