Android access to PHP retrieve JSON data

Source: Internet
Author: User
Tags json php code readline

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

An exception is thrown when code is executed php100.com

Java.net.SocketException:Permission denied

This is not sufficient for application access to the network in Androidmanifest.xml, the following configuration is required:

<uses-permission android:name= "Android.permission.INTERNET"/>

is added to

</manifest>

It was good before.

Then the test passes.

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.