I am using volley to send web service request and volley response is SERVER ERROR (this site requires java script enabled). I tried POST and GET methods.
After some search on this issue I found these two questions..
Volley Server error (Requires javascript?) Android Development but there is no answer to solve this issue in 2 months
No more questions and solution found. Can anyone tell the exact problem and solution with this issue.
Here is my code
public void postRequest(final JSONObject jsonParams,final Handler handler){
StringRequest stringRequest = new StringRequest(Request.Method.POST, AppConstants.ADMIN_URL,
new Response.Listener() {
@Override
public void onResponse(String response) {
Message message = handler.obtainMessage();
message.obj = response;
handler.sendMessage(message);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Message message = handler.obtainMessage();
message.obj = error.toString();
Log.d("error",error.toString());
handler.sendMessage(message);
}
}){
@Override
protected Map getParams(){
Map params = new HashMap();
Iterator iter = jsonParams.keys();
while (iter.hasNext()) {
try {
String key = iter.next();
String value = jsonParams.get(key)+"";
params.put(key,value);
} catch (JSONException e) {
// Something went wrong!
Message message = handler.obtainMessage();
message.obj = e.getMessage();
handler.sendMessage(message);
}
}
return params;
}
@Override
public Map getHeaders() throws AuthFailureError {
Map headers = new HashMap();
return headers;
}
};
MyVolley.getInstance(context).getRequestQueue().add(stringRequest);
}
and web service code is
解决方案
There is a nice workaround suggested in this answer.
In Volley, you can override getHeaders() method to send the cookie with your HTTP request.
Code :
@Override
public Map getHeaders() throws AuthFailureError {
Map map = new HashMap<>();
map.put("Cookie", "__test=YOUR COOKIE HERE; expires=Friday, January 1, 2038 at 5:25:55 AM; path=/");
return map;
}