Android Access PHP Retrieve JSON data instance _android

Source: Internet
Author: User
Tags readline
PHP code
Copy Code code as follows:

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

Java code
Copy Code code 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.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 instructions
Copy Code code as follows:

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
Exceptions are thrown when code is executed
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.