I've created an API in NodeJS that contains headers (which are the tokens).
The volley library (ANDROID) post goes like this:
RequestQueue queue = MyVolley.getRequestQueue();
JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST,REGISTERUSERURL,null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
System.out.println(response);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
hideProgressDialog();
}
}) {
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("email","myname#gmail.com");
params.put("password", "myname");
return params;
};
};
queue.add(jsObjRequest);
But this doesnt register from the server-side. Whats the issue?
Related
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);
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);
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 !).
I have tried every single method available on stackoverflow but result remains the same.
I want to send params and also api key in the Authorization field of header using volley.
Here is the code snippet.
holder.comment_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String POSTCOMMENT_URL = Utility.getIPADDRESS()+"comments";
volleySingleton = VolleySingleton.getInstance();
requestQueue = volleySingleton.getRequestQueue();
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, POSTCOMMENT_URL, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Log.e("Response: " , response.toString());
Toast.makeText(myApplication.getApplicationContext(),response.toString(),Toast.LENGTH_LONG).show();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(myApplication.getApplicationContext(),"Error", Toast.LENGTH_LONG).show();
}
}){
#Override
public String getBodyContentType() {
return "application/x-www-form-urlencoded; charset=UTF-8";
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("Authorization", getApiKey() );
return params;
}
#Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
params.put("pid", "39");
params.put("comment", "mobile testing");
return params;
}
};
requestQueue.add(jsonObjectRequest);
}
});
I'm getting this error:
[5333] BasicNetwork.performRequest: Unexpected response code 400 for http://www.XXXXXXXXXXX.com/comments
I have already tested the endpoint using Advanced Rest client, it works and returns the JsonObject with previously added comment.
Here are the results:
{
"error": false
"message": "comment added successfully added!"
"comment_id": "12"
"user_id": "12"
"pid": "39"
"comment": "testing for stackoverflow"
"date of comment": "2016-07-30 13:49:08"
}
Ok, after a long research and stupid rather valuable changes, it finally worked for me.
Here is the working code:
holder.comment_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view) {
final String api_key = getApiKey();
String POSTCOMMENT_URL = Utility.getIPADDRESS()+"comments";
volleySingleton = VolleySingleton.getInstance();
requestQueue = volleySingleton.getRequestQueue();
StringRequest stringRequest = new StringRequest(Request.Method.POST, POSTCOMMENT_URL, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Toast.makeText(myApplication.getApplicationContext(),response,Toast.LENGTH_LONG).show();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(myApplication.getApplicationContext(),error.toString(),Toast.LENGTH_LONG).show();
}
}) {
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("Authorization", api_key);
return params;
}
#Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
params.put("pid", "40");
params.put("comment", "testing");
return params;
}
};
requestQueue.add(stringRequest);
}
});
Note: It worked for me, I'm not sure about others.
Changes are: Change JsonObjectRequest to StringRequest and removing any Content-Type doesn't make any difference, so I removed it.
Also, make sure all the params have some values i.e test it by logging.
i should to send image in request body, how can I make it?
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
String url = getString(R.string.url) + "auth/avatar?token="+ Utils.getToken(getActivity());
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject jsonObject) {
Log.d("myLogs", jsonObject.toString());
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError volleyError) {
}
}) {
#Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("avatar", encodedImage);
return params;
}
};
requestQueue.add(request);
I have read this article, but it doesn't help me.