Android access to PHP retrieve JSON data

Source: Internet
Author: User
Tags array json log php code readline client access

PHP code ============================ $array = Array (' username ' => ' Yang Ji ', ' Password ' => ' 123456 ', ' user_id ' =>1);

echo Json_encode ($array);

Java Code ============================

private void Starturlcheck (String username,string password) {httpclient client = new Defaulthttpclient (); StringBuilder builder = new StringBuilder ();   HttpGet myget = new HttpGet ("http://10.0.2.2/Android/index.php"); try {httpresponse response = Client.execute (Myget); BufferedReader reader = new BufferedReader (New InputStreamReader (Response.getentity (). GetContent ())); for (String s = reader.readline (); s!= null; s = Reader.readline ()) {builder.append (s);} Jsonobject jsonobject = new Jsonobject (builder.tostring ()); String re_username = jsonobject.getstring ("username"); String Re_password = jsonobject.getstring ("password"); int re_user_id = Jsonobject.getint ("user_id"); Settitle ("User id_" +re_user_id); LOG.V ("url response", "true=" +re_username); LOG.V ("url response", "true=" +re_password); catch (Exception e) {log.v ("url response", "false"); E.printstacktrace ();}   Run results =================================== where http://10.0.2.2 is the IP address of the native URL for Android access. http://127.0.0.1 for testing on the computer  throws an exception when executing code php100.com   Java.net.SocketException:Permission denied   This has insufficient permissions to access the network for Applications In Androidmanifest.xml, the following configuration is required: <uses-permission android:name= "Android.permission.INTERNET"/> is added to the </ Manifest> before the test is done.

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.