Android accesses php to retrieve json data

Source: Internet
Author: User
PHP code ===============================$ array = array (& amp; #39; username & amp; #39 ;=& gt; & amp; #39; Yang Zhu & amp; #39 ;,& amp; #39; password & amp; #39; = & gt; & amp; #39; 123456 & amp; #39 ;,&

 

PHP code =============================$ array = array ('username' => 'yangzhu ', 'password' => '000000', '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.exe cute (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 ();}} running result = http://10.0.2.2 The IP address used by Android to access the local url. Corresponding to the test on the computer http://127.0.0.1 In addition, an exception PHP100.com java.net. SocketException: Permission denied is thrown during code execution. This means that the application has insufficient network access permissions in AndroidManifest. xml and requires the following configuration: AddIt was done before and the test passed.

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.