1、配置应用访问网络权限,参考:
Android java.net.SocketException: Permission denied
2、服务器端用php实现,代码如下:
<?php $zhike = array( 'id' => 'zhike', 'title' => '止咳推拿', 'content' => 'zhikezhikezhike' ); $list = array(); array_push($list,$zhike); echo json_encode($list); ?>
3、客户端代码:
public List<Map<String,Object>> getList() throws Exception{ List<Map<String,Object>> list = new ArrayList<Map<String,Object>>(); Map<String, Object> item = null; StringBuilder builder = new StringBuilder(); HttpClient client = new DefaultHttpClient(); HttpGet get = new HttpGet(url); HttpResponse response = client.execute(get); BufferedReader reader = new BufferedReader( new InputStreamReader(response.getEntity().getContent())); String s = null; while((s = reader.readLine()) != null){ builder.append(s); } JSONArray ja = new JSONArray(builder.toString()); Log.i("---my tag---", ja.toString()); JSONObject joItem = null; for (int i = 0; i < ja.length(); i++) { joItem = ja.getJSONObject(i); item = new HashMap<String, Object>(); item.put("id", joItem.getString("id")); item.put("title", joItem.getString("title")); item.put("content", joItem.getString("content")); list.add(item); } return list; }