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.