android - How to post raw data using volley? -
i have following raw data need send server response.
{ "nodeid": null, "userid": null, "mobilenumber": "0000000", "emailid": "xxx@gmail.com", "userprofile": null, "region": null, "countrycode": "01", "password": "pass@123", "places": [], "trustednetwork": [], "profilepic": null, "fullname": null, "longitude": 0.0, "latitude": 0.0 }
while posting raw data have send authentication header parameters. whatever tried, have not been able post it.
i have used following code has not worked yet
requestqueue requestqueue = volley.newrequestqueue(getactivity()); string url = baseurl; final string mrequestbody = s; stringrequest stringrequest = new stringrequest(request.method.post, url, new response.listener<string>() { @override public void onresponse(string response) { log.i("volley", response); new alertdialog.builder(getactivity()) .settitle("hmm") .setmessage(""+response) .setpositivebutton("ok", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { dialog.dismiss(); } }) .show(); } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { log.e("volley", error.getlocalizedmessage()); new alertdialog.builder(getactivity()) .settitle("sorry") .setmessage(""+error.getlocalizedmessage()) .setpositivebutton("ok", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { dialog.dismiss(); } }) .show(); } }) { @override public string getbodycontenttype() { return "application/json; charset=utf-8"; } @override public byte[] getbody() throws authfailureerror { try { return mrequestbody == null ? null : mrequestbody.getbytes("utf-8"); } catch (unsupportedencodingexception uee) { volleylog.wtf("unsupported encoding while trying bytes of %s using %s", mrequestbody, "utf-8"); return null; } } @override public map<string, string> getheaders() throws authfailureerror { hashmap<string, string> headers = new hashmap<string, string>(); string creds = string.format("%s:%s", "xxxxxx", "xxxxxxxxxxxxxx"); string auth = "basic " + base64.encodetostring(creds.getbytes(), base64.default); headers.put("authorization", auth); headers.put("content-type", "application/json; charset=utf-8"); return headers; } }; requestqueue.add(stringrequest);
here, problem had occurred while creating authorization brought volley exception case. since, password using long base64.default not handling properly. after changing base64.no_wrap, problem solved
Comments
Post a Comment