Android accesses php to retrieve json data instances

Source: Internet
Author: User

Php code Copy codeThe Code is as follows: $ array = array (
'Username' => 'yangzhu ',
'Password' => '123 ',
'User _ id' => 1
);
Echo json_encode ($ array );

Java codeCopy codeThe Code is as follows: 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 instructionsCopy codeThe Code is as follows: http: // 10.0.2.2 is the IP address of Android to access the local url. Http: // 127.0.0.1 tested on the computer
In addition, an exception is thrown during code execution.
Java.net. SocketException: Permission denied
Therefore, the AndroidManifest. xml file does not have sufficient network access permissions for the application. The following configuration is required:
<Uses-permission Android: name = "android. permission. INTERNET"/>
Add
</Manifest>
Just now
Then pass the test.

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.