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.