能夠解析出來 但是listview中沒有任何顯示。大致上的代碼如圖!大神能夠幫忙的話,源碼在這鏈接:http://pan.baidu.com/s/1jGVzvHc 密碼:1nsl
public class HttpUtils {
public static void getNewsJSON(final String url, final Handler handler){
new Thread(new Runnable() {
@Override
public void run() {
HttpURLConnection conn;
InputStream is;
try {
conn = (HttpURLConnection) new URL(url).openConnection();
conn.setRequestMethod("GET");
is = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line = "";
StringBuilder result = new StringBuilder();
while ( (line = reader.readLine()) != null ){
result.append(line);
}
Message msg = new Message();
msg.obj = result.toString();
handler.sendMessage(msg);
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
public static void setPicBitmap(final ImageView thumb_value, final String custom_fields){
new Thread(new Runnable() {
@Override
public void run() {
try {
HttpURLConnection conn = (HttpURLConnection) new URL(custom_fields).openConnection();
conn.connect();
InputStream is = conn.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(is);
thumb_value.setImageBitmap(bitmap);
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
}
public class News {
private String title;
private String excerpt;
private String date;
private String id;
private String thumb_value;
private String custom_fields;
public News(String title, String excerpt, String date, String id, String custom_fields,String thumb_value ){
setTitle(title);
setExcerpt(excerpt);
setDate(date);
setId(id);
setCustom_fields(custom_fields);
setThumb_value(thumb_value);}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getExcerpt() {
return excerpt;
}
public void setExcerpt(String excerpt) {
this.excerpt = excerpt;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getThumb_value() {
return thumb_value;
}
public void setThumb_value(String thumb_value) {
this.thumb_value = thumb_value;
}
public String getCustom_fields() {
return custom_fields;
}
public void setCustom_fields(String custom_fields) {
this.custom_fields = custom_fields;
}
}
public class NewsAdapter extends BaseAdapter {
private Context context;
private List<News> newsList;
public NewsAdapter(Context context, List<News> newsList){
this.context = context;
this.newsList = newsList;
}
@Override
public int getCount() {
return newsList.size();
}
@Override
public News getItem(int position) {
return newsList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null){
convertView = LayoutInflater.from(context).inflate(R.layout.news_item, null);
}
TextView Title = (TextView) convertView.findViewById(R.id.Title);
TextView excerpt = (TextView) convertView.findViewById(R.id.excerpt);
TextView date = (TextView) convertView.findViewById(R.id.date);
ImageView thumb_value = (ImageView) convertView.findViewById(R.id.thumb_value);
News news = newsList.get(position);
Title.setText(news.getTitle());
excerpt.setText(news.getExcerpt());
date.setText(news.getDate());
String custom_fields = news.getCustom_fields();
HttpUtils.setPicBitmap(thumb_value,null);
return convertView;
}
}
private Handler getNewsHandler = new Handler(){
public void handleMessage(android.os.Message msg) {
String jsonData = (String) msg.obj;
System.out.println(jsonData);
try {
JSONArray jsonArray = new JSONArray(jsonData);
for (int i=0;i<jsonArray.length();i++){
JSONObject object = jsonArray.getJSONObject(i);
String id = object.getString("id");
String title = object.getString("title");
String excerpt = object.getString("excerpt");
String date = object.getString("date");
String custom_fields = object.getString("custom_fields");
String thumb_value = object.getString("thumb_value");
/*System.out.println("title = " +title);
System.out.println("thumb_value = " +thumb_value);*/
newsList.add(new News(id, title, excerpt, date,custom_fields, thumb_value ));
}
/*adapter.notifyDataSetChanged();*/
} catch (Exception e) {
e.printStackTrace();
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lvNews = (ListView) findViewById(R.id.lvNews);
newsList = new ArrayList<News>();
adapter = new NewsAdapter(this, newsList);
lvNews.setAdapter(adapter);
lvNews.setOnItemClickListener(this);
HttpUtils.getNewsJSON(GET_NEWS_URL, getNewsHandler);
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
News news = newsList.get(position);
Intent intent = new Intent(this, BrowseNewsActivity.class);
intent.putExtra("Excerpt", news.getExcerpt());
startActivity(intent);
}
}
public class BrowseNewsActivity extends Activity {
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.acitivy_browse_news);
webView = (WebView) findViewById(R.id.webView);
String Excerpt = getIntent().getStringExtra("Excerpt");
webView.loadUrl(Excerpt);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
}
}
這是jason鏈接獲取的數組,貌似我開頭那段沒有獲取
{
"status": "ok",
"count": 20,
"count_total": 2440,
"pages": 122,
"posts": [{
"id": 3562,
"title": "\u6d77\u5b81\u6709\u4eba\u975e\u6cd5\u96c6\u8d44600\u591a\u4e07\uff0c\u8fd180\u4eba\u53d7\u9a97\uff01",
"excerpt": "<p>\u4ece\u53bb\u5e74\u5f00\u59cb\uff0c\u6709\u5f88\u591a\u8001\u4eba\u5bb6\u53d7\u5230\u9ad8\u989d\u5229\u606f\u7684\u8bf1\u60d1\uff0c\u628a\u94b1\u6295\u8fdb\u4e86\u4e00\u4e2a\u6240\u8c13\u7684\u6d77\u5b81\u6295\u8d44\u7ba1\u7406\u516c\u53f8\u3002\u7ed3\u679c\u5462\u8fd9\u4e00\u5bb6\u6295\u8d44\u516c\u53f8\u641e\u7684\u662f\u975e ...<\/p>\n",
"date": "2016-01-13 10:22:18",
"comment_count": 0,
"custom_fields": {}
}, {
"id": 3534,
"title": "\u6628\u5929\u4e0b\u5348\uff0c\u6d77\u5b81\u8857\u5934\u60ca\u73b03000\u4e07\u4fdd\u65f6\u6377\u8f66\u961f\uff0c\u8fd9\u662f\u600e\u4e48\u4e86\uff1f",
"excerpt": "<p>\u6628\u513f\u4e2a\u4e0b\u5348\uff0c\u6d77\u5b81\u7684\u8857\u5934\u5f88\u662f\u70ed\u95f9\uff0c\u4e00\u6392\u7684\u4fdd\u65f6\u6377\u8f66\u8f86\u961f\u4f0d\u5de1\u6e38\uff0c\u90a3\u573a\u9762\u662f\u76f8\u5f53\u58ee\u89c2\uff0c\u636e\u8bf4\u662f\u4fdd\u65f6\u6377\u5e74\u4f1a\uff1f\u5230\u5e95\u53d1\u751f\u4e86\u4ec0\u4e48\uff0c ...<\/p>\n",
"date": "2016-01-08 10:08:34",
"comment_count": 0,
"custom_fields": {}
}, {
"id": 3530,
"title": "\u6628\u5929\u65e9\u4e0a\uff0c\u6d77\u5b81\u4e01\u6865\u53d1\u751f\u4e00\u8d77\u60b2\u5267\uff0c\u649e\u6210\u8fd9\u6837\u5f00\u7684\u5f97\u6709\u591a\u5feb\uff01",
"excerpt": "<p>\u636e\u7f51\u53cb\u7206\u6599\uff1a\u6628\u5929\u65e9\u4e0a6\u70b9\u591a\u5de6\u53f3\uff0c\u6d77\u5b81\u4e01\u6865\u8054\u4e01\u7ebf\u9644\u8fd1\u53d1\u751f\u8f66\u7978\uff0c\u4e00\u8f86\u96ea\u4f5b\u5170\u8f7f\u8f66\u649e\u5728\u4e86\u6811\u4e0a\uff0c\u9a7e\u9a76\u5ba4\u88ab\u6324\u533e\u3002\u636e\u56f4\u89c2\u7fa4\u4f17 ...<\/p>\n",
"date": "2016-01-04 11:09:36",
"comment_count": 0,
"custom_fields": {
"thumb_value": ["http:\/\/www.zjhaining.com\/wp-content\/uploads\/2016\/01\/640.webp-2.jpg"]
}
}, {
"id": 3522,
"title": "\u6d77\u5b81\u8bb8\u6751\u53d1\u751f\u8f66\u7978 \u5730\u4e0a\u90fd\u662f\u8840 \u6c7d\u8f66\u51b2\u8fdb\u8349\u576a\uff01",
"excerpt": "<p>\u636e\u7f51\u53cb\u201c@\u5ff5\u5ff5\u4e0d\u5fd8\u201d\u7206\u6599\uff1a\u6d77\u5b81\u897f\u7ad9\u62a5\u56fd\u6751\u9644\u8fd1\u53d1\u751f\u4ea4\u901a\u4e8b\u6545\uff0c\u4e24\u8f66\u76f8\u649e\uff0c\u8f66\u5934\u635f\u6bc1\u4e25\u91cd\uff0c\u4e00\u8f66\u76f4\u63a5\u51b2\u8fdb\u4e86\u8349\u576a\u91cc\uff0c\u6709\u4eba\u5458 ...<\/p>\n",
"date": "2016-01-03 10:30:58",
"comment_count": 1,
"custom_fields": {
"thumb_value": ["http:\/\/www.zjhaining.com\/wp-content\/uploads\/2016\/01\/640.webp-22.jpg"]
}
}, {
"id": 3408,
"title": "\u5728\u6d77\u5b81\uff0c\u7adf\u4e5f\u80fd\u770b\u5230\u5982\u6b64\u7f8e\u4e3d\u7684\u661f\u7a7a\uff01",
"excerpt": "<p>\u8bb0\u5f97\u591a\u4e45\u6ca1\u6709\u770b\u5230\u8fc7\u661f\u7a7a\u4e86\u4e48\uff1f\u4e60\u60ef\u4e8e\u4e0d\u518d\u62ac\u5934\u4ef0\u671b\uff0c\u4e60\u60ef\u4e8e\u88ab\u96fe\u973e\u4ee5\u53ca\u706f\u706b\u901a\u660e\u56f4\u7ed5\uff0c\u4e60\u60ef\u4e8e\u8d8a\u6765\u8d8a\u539a\u91cd\u7684\u955c\u7247\u3002\u4eca\u5929\uff0c\u6307 ...<\/p>\n",
"date": "2015-12-16 09:45:00",
"comment_count": 2,
"custom_fields": {
"thumb_value": ["http:\/\/www.zjhaining.com\/wp-content\/uploads\/2015\/12\/640.webp-211.jpg"]
}
}, {
"id": 3400,
"title": "\u3010\u7206\u6599\u3011\u6d77\u5b81\u65b0\u82d1\u8def\u4e24\u8f66\u76f8\u649e\uff0c\u8f66\u76f4\u63a5\u9a91\u5728\u4e86\u6811\u4e0a\uff01",
"excerpt": "<p>\u636e\u7f51\u53cb\u7206\u6599\uff1a14\u65e5\u4e0a\u5348\uff0c\u6d77\u5b81\u98ce\u548c\u4e3d\u82d1\u95e8\u53e3\u4e24\u8f66\u76f8\u649e\uff0c\u8f66\u5934\u53d7\u635f\u4e25\u91cd\uff0c\u6811\u90fd\u649e\u6b6a\u4e86\uff01\u76ee\u524d\u4e8b\u6545\u7684\u5177\u4f53\u539f\u56e0\u8fd8\u5728\u8c03\u67e5\u4e2d\u3002 \u5c0f ...<\/p>\n",
"date": "2015-12-16 09:33:17",
"comment_count": 0,
"custom_fields": {
"thumb_value": ["http:\/\/www.zjhaining.com\/wp-content\/uploads\/2015\/12\/640.webp-110.jpg"]
}
}, {
"id": 3383,
"title": "8\u53f7\u508d\u665a\uff0c\u6d77\u5b81\u4e0a\u7a7a\u60ca\u73b0\u4e0d\u660e\u98de\u884c\u7269\uff1f",
"excerpt": "<p>8\u53f7\u508d\u665a\u4e94\u70b9\u5de6\u53f3\uff0c\u6d77\u5b81\u5f88\u591a\u7f51\u53cb\u8868\u793a\u770b\u5230\u4e86\u5929\u7a7a\u4e2d\u7684\u4e0d\u660e\u98de\u884c\u7269\u3002\u5728\u5929\u7a7a\u4e2d\uff0c\u6709\u4e00\u6761\u50cf\u706b\u7bad\u7684\u5c3e\u90e8\u55b7\u5c04\u51fa\u7684\u706b\u7130\u4e00\u822c\u7684\u98de\u884c ...<\/p>\n",
使用Gson解析復雜的json數據
http://blog.csdn.net/tkwxty/article/details/34474501
我只是個搬運工。