Volley post non-JSON format data and get JSON data

Source: Internet
Author: User

You cannot post data in non-JSON format when using Jsonobjectrequest, so you can use Stringrequest to get the corresponding data and then convert to JSON-formatted data.

//the context here needs to be discussedprivate static Requestqueue Mrequestqueue =Volley.newrequestqueue (Global.getcontext ());p ublic interface iresponse{void Subscribedata (Object data);}/**post way to get data from the network*/Public static void Postdatafromnet (Final iresponse iresponse, String URL, final HashMap<String,String>map) {Stringrequest Request=New Stringrequest (Request.Method.POST, URL, new Response.listener<String>() {@Override public void Onresponse (String response) {try {           Iresponse.subscribedata (new Jsonobject (response)); //returning data to} catch (Jsonexception e) {e.printstacktrace (); }}, new Response.errorlistener () {@Override public void Onerrorresponse (Volleyerror volleyerror) {LOG.E ("#StringRequest: error#", Volleyerror.getmessage ()); }}) {@Override public Map<string, string>Getheaders () throws Authfailureerror {Map<String,String> Header = new hashmap<>();Header.put ("token", "9foyt495e6bcflokye36f8c618533c3e");            return header; } @Override protected Map<string, string>Getparams () throws Authfailureerror {return map;        }        }; Request.settag (""+URL);    Mrequestqueue.add (Request); }

Volley post non-JSON format data and get JSON data

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.