android中獲取json數據


package cs.domain;

public class News {
	
	private Integer id;
	private String title;
	private Integer timelength;
	
	public News(Integer id, String title, Integer timelength) {
		this.id = id;
		this.title = title;
		this.timelength = timelength;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public Integer getTimelength() {
		return timelength;
	}
	public void setTimelength(Integer timelength) {
		this.timelength = timelength;
	}
}

package cs.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class StreamTools {

	public static byte[] read(InputStream inStream) throws IOException{
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		byte [] buffer = new byte[1024];
		int len =0;
		while((len = inStream.read(buffer))!=-1){
			outStream.write(buffer);
		}
		inStream.close();
		outStream.close();
		return outStream.toByteArray();
	}
}

package cs.service;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;

import cs.domain.News;
import cs.utils.StreamTools;

public class NewsService {
	
	public static List<News> getjson() throws Throwable{
		
		String path = "http://172.16.32.23:8080/androidweb2/ListServlet?format=json";
		URL url = new URL(path);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setReadTimeout(5000);
		conn.setRequestMethod("POST");
		if(conn.getResponseCode()==200){
			InputStream inStream = conn.getInputStream();
			return parseJSON(inStream);	
		}
		return null;
	}

	private static List<News> parseJSON(InputStream inStream) throws Exception {

		List<News> news = new ArrayList<News>();
		byte[] data = StreamTools.read(inStream);
		String json = new String(data);
		JSONArray arr = new JSONArray(json);
		for (int i = 0; i < arr.length(); i++) {
			JSONObject jsonobject = arr.getJSONObject(i);
			News n = new News(jsonobject.getInt("id"),
					jsonobject.getString("title"),
					jsonobject.getInt("timelength"));
			news.add(n);
		}
		return news;
	}
}


package com.example.news;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import cs.domain.News;
import cs.service.NewsService;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class MainActivity extends Activity {
   ListView mylist;
   
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mylist = (ListView) this.findViewById(R.id.mylist);
	
		try {
			List<News> news = NewsService.getjson();
			List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
			for(News n:news){
				HashMap<String, Object> hm = new HashMap<String, Object>();
				hm.put("id", n.getId());
				hm.put("title", n.getTitle());
				hm.put("timelength","時長:"+n.getTimelength()+"小時");
				data.add(hm);
			}
			SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.items, new String[]{"title","timelength"}, new int[]{R.id.title,R.id.timelength});
			mylist.setAdapter(adapter);
			 
		} catch (Throwable e) {
			Toast.makeText(getApplicationContext(), "讀取失敗", 1).show();
			e.printStackTrace();
		}
	}

}



發佈了22 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章