|
Android访问php取回json数据
转载:http://www.oschina.net/code/snippet_12_1122
6 | echo json_encode($array); |
2.[代码]java代码
01 | private voidstartUrlCheck(String username,String password) |
03 | HttpClient client =new DefaultHttpClient(); |
04 | StringBuilder builder =new StringBuilder(); |
06 | HttpGet myget =new HttpGet("http://10.0.2.2/Android/index.php"); |
08 | HttpResponse response = client.execute(myget); |
09 | BufferedReader reader =new BufferedReader(newInputStreamReader( |
10 | response.getEntity().getContent())); |
11 | for(String s = reader.readLine(); s != null; s = reader.readLine()) { |
14 | JSONObject jsonObject =new JSONObject(builder.toString()); |
15 | String re_username = jsonObject.getString("username"); |
16 | String re_password = jsonObject.getString("password"); |
17 | intre_user_id = jsonObject.getInt("user_id"); |
18 | setTitle("用户id_"+re_user_id); |
19 | Log.v("url response","true="+re_username); |
20 | Log.v("url response","true="+re_password); |
21 | } catch (Exception e) { |
22 | Log.v("url response","false"); |
3.[代码]运行说明
01 | 其中http://10.0.2.2为Android访问本机url的ip地址。对应电脑上测试的http://127.0.0.1 |
05 | java.net.SocketException: Permission denied |
07 | 此为应用访问网络的权限不足 在AndroidManifest.xml中,需要进行如下配置: |
08 | <uses-permission Android:name="android.permission.INTERNET" /> |
|
|
|