获取 JSON 数据并解析
1 import java.io.InputStream; 2 import java.net.HttpURLConnection; 3 import java.net.URL; 4 import java.util.ArrayList; 5 import java.util.List; 6 7 import org.json.JSONArray; 8 import org.json.JSONObject; 9 10 import com.wangjialin.internet.json.domain.News; 11 import com.wangjialin.internet.json.utils.StreamTool; 12 13 public class NewsService { 14 15 /** 16 * 获取最新视频资讯 17 * @return 18 * @throws Exception 19 */ 20 public static List<News> getJSONLastNews() throws Exception{ 21 22 String path = "http://192.168.1.103:8080/ServerForJSON/NewsListServlet"; 23 HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection(); 24 conn.setConnectTimeout(5000); 25 conn.setRequestMethod("GET"); 26 27 if(conn.getResponseCode() == 200){ 28 InputStream json = conn.getInputStream(); 29 return parseJSON(json); 30 } 31 return null; 32 } 33 34 private static List<News> parseJSON(InputStream jsonStream) throws Exception{ 35 36 List<News> list = new ArrayList<News>(); 37 byte[] data = StreamTool.read(jsonStream); 38 String json = new String(data); 39 JSONArray jsonArray = new JSONArray(json); 40 41 for(int i = 0; i < jsonArray.length() ; i++){ 42 JSONObject jsonObject = jsonArray.getJSONObject(i); 43 int id = jsonObject.getInt("id"); 44 String title = jsonObject.getString("title"); 45 int timelength = jsonObject.getInt("timelength"); 46 list.add(new News(id, title, timelength)); 47 } 48 return list; 49 } 50 }
StreamTool
1 import java.io.ByteArrayOutputStream; 2 import java.io.InputStream; 3 4 public class StreamTool { 5 /** 6 * 从流中读取数据 7 * @param inStream 8 * @return 9 */ 10 public static byte[] read(InputStream inStream) throws Exception{ 11 12 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 13 byte[] buffer = new byte[1024]; 14 int len = 0; 15 16 while((len = inStream.read(buffer)) != -1){ 17 outputStream.write(buffer, 0, len); 18 } 19 20 inStream.close(); 21 return outputStream.toByteArray(); 22 } 23 }
显示
1 public class MainActivity extends Activity { 2 /** Called when the activity is first created. */ 3 List<News> newes; 4 List<HashMap<String, Object>> data = null;; 5 String length; 6 7 @Override 8 public void onCreate(Bundle savedInstanceState) { 9 super.onCreate(savedInstanceState); 10 11 setContentView(R.layout.main); 12 ListView listView = (ListView) this.findViewById(R.id.listView); 13 14 length = this.getResources().getString(R.string.length); 15 16 try{ 17 newes = NewsService.getJSONLastNews(); 18 data = new ArrayList<HashMap<String,Object>>(); 19 for(News news : newes){ 20 HashMap<String, Object> item = new HashMap<String, Object>(); 21 item.put("id", news.getId()); 22 item.put("title", news.getTitle()); 23 item.put("timelength", length + news.getTimelength()); 24 data.add(item); 25 } 26 27 SimpleAdapter adapter = new SimpleAdapter(this, 28 data, R.layout.item, 29 new String[]{"title", "timelength"}, 30 new int[]{R.id.title, R.id.timelength} 31 ); 32 listView.setAdapter(adapter); 33 }catch(Exception e) 34 { 35 e.printStackTrace(); 36 }; 37 } 38 }
原文:http://www.cnblogs.com/renzimu/p/4539547.html