PHP handles Android's post data

Source: Internet
Author: User

Today, using PHP to develop the data interface of the Android network, I found that thinkphp's I function (PHP $_post) does not get to androidpost data.

The Android code is as follows:

1map<string,string> map =NewHashmap<>();2Map.put ("Itemid", This. Itemid);3Jsonobjectrequest Jor =NewJsonobjectrequest (Request.method.post,url,NewJsonobject (map),NewResponse.listener<jsonobject>() {4 @Override5              Public voidonresponse (Jsonobject jsonobject) {6                 Try{7                     if("OK". Equals (Jsonobject.getstring ("status")))){8Jsonobject returnddata = jsonobject.getjsonobject ("Data"));9 data.clear ();Ten                         if("Out". Equals (Returnddata.getstring ("InOut"))){ OneData.add ("Type: Expense"); A}Else{ -Data.add ("Type: Revenue"); -                         } theData.add ("Subject:" +returnddata.getstring ("info")); -Data.add ("Amount:" +returnddata.getstring ("Money")); -Data.add ("Time:" +xlfunc.xl_timestamp_to_datetime ("Returnddata.getlong") *1000, "yyyy-mm-d")); -Data.add ("Settlement:" +returnddata.getstring ("result")); + adapter.notifydatasetchanged (); - Listvview.setlayoutanimation (LAC); +}Else{ AXlfunc.xl_toast (detailactivity. This, jsonobject.getstring ("data"), 0); at                     } -}Catch(jsonexception e) { -Xlfunc.xl_toast (detailactivity. This, "Network exception! ", 0); -                 } -             } -},NewResponse.errorlistener () { in @Override -              Public voidonerrorresponse (Volleyerror volleyerror) { toXlfunc.xl_toast (detailactivity. This, "An error occurred while getting the details! Please check the network is OK! ", 0); +             } -         }){ the @Override *              PublicMap<string, string> getheaders ()throwsAuthfailureerror { $map<string,string> headers =NewHashmap<>();Panax NotoginsengHeaders.put ("Content-type", "Application/json; Charset=utf-8"); -                 returnheaders; the             } +         }; AJor.setshouldcache (false); the          This. Requestqqueue.add (Jor);

Headers settings sent, nor use Getparams (Online said use this method service end get post data, can only overload jsonobjectrequest, another words, in this do not mention! )

But the PHP side

Var_dump ($_post);

is empty!

After searching the Internet, we find that the post data can be obtained using the following methods:

1         $postDdata file_get_contents (' Php://input '); 2         $postDdata = Json_decode ($postDdatatrue); 3         4         $return            Array (); 5         $itemid            intval ($postDdata[' itemid ']);        

That is, the use of the Php://input input stream is obtained, that is, the Android side has already submitted data, then the problem appears on the PHP side!

Headers of data submitted by Android:

Headers.put ("Content-type", "Application/json; Charset=utf-8")

The Android side is sending JSON,

View PHP instructions for $_post:

$HTTP _post_vars contains the same initial information, but was not a superglobal. (Note that $HTTP _post_vars and $_post is different variables and that PHP handles them as such)

Then look at the difference between $_post and php://input:

$HTTP _raw_post_data contains the RAW POST DATA. In general, PHP://input should is used instead of $HTTP _raw_post_data.

Php://input usually replaces $http_raw_post_data and $http_raw_post_data contains the original submitted data

And post is filtering out unresolved data, such as JSON data! The filtered data is put into the $http_raw_post_data, so we need to use Php://input to get the JSON data for the POST!

PHP handles Android's post 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.