android - Volley Server Error with null Network response -
every time try use post method volley, sever error. null value in getcause, , default value in getnetworkresponse.tostring().
if use method, works fine (i response url).
can can do?
map<string, string> jsonparams = new hashmap<string, string>(); jsonparams.put("teststr", "abd"); requestqueue requestqueue = volleysingleton.getinstance().getrequestqueue(); jsonobjectrequest request = new jsonobjectrequest( request.method.post, url, new jsonobject(jsonparams), new response.listener<jsonobject>() { @override public void onresponse(jsonobject response) { try { toast.maketext(getapplicationcontext(), "success"+response.tostring(), toast.length_long).show(); }catch(exception e){ toast.maketext(getapplicationcontext(), "json error", toast.length_long).show(); } } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { log.d("abd", "error: " + error + ">>" + error.networkresponse.statuscode + ">>" + error.networkresponse.data + ">>" + error.getcause() + ">>" + error.getmessage()); } }) { @override protected map<string,string> getparams() { hashmap<string, string> params = new hashmap<string, string>(); params.put("key", "value"); 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; } }; requestqueue.add(request);
error log:
error: error: com.android.volley.servererror>>404>>[b@42b1e0d0>>null>>null
update: networkresponse.statuscode comes 404, though url accessible (and return data if use method). if remove header part in post method, still same.
the url:
<?php $response = array(); $jsonstring = file_get_contents('php://input'); $jsonobj = json_decode($jsonstring, true); if(!isset($jsonobj['teststr'])){ $response["msg"] = "no data."; }else{ $response["msg"] = "success: ".$jsonobj['teststr']; } echo json_encode($response); ?>
first, try make sure server works well. can use postman(chrome plug-in) or other way send post request url , see responses.
after make sure there's no problem server, let solve problem volley.
there's problem jsonobjectrequest
when use post method. volley jsonobjectrequest post request not working.
i suggest use stringrequest
first , overwrite getparams
method did before. after survive task, can try write own request, not difficult useful.
i suggest add request.setshouldcache(false)
before requestqueue.add(request);
. default, volley saves response in cache , behavior may cause strange problem.
Comments
Post a Comment