问题描述
- 在修改别人的新闻客户端,在logcat中能够解析出jason信息,但是在手机中缺无法显示
-
能够解析出来 但是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": "u6d77u5b81u6709u4ebau975eu6cd5u96c6u8d44600u591au4e07uff0cu8fd180u4ebau53d7u9a97uff01", "excerpt": "<p>u4eceu53bbu5e74u5f00u59cbuff0cu6709u5f88u591au8001u4ebau5bb6u53d7u5230u9ad8u989du5229u606fu7684u8bf1u60d1uff0cu628au94b1u6295u8fdbu4e86u4e00u4e2au6240u8c13u7684u6d77u5b81u6295u8d44u7ba1u7406u516cu53f8u3002u7ed3u679cu5462u8fd9u4e00u5bb6u6295u8d44u516cu53f8u641eu7684u662fu975e ...</p>n", "date": "2016-01-13 10:22:18", "comment_count": 0, "custom_fields": {} }, { "id": 3534, "title": "u6628u5929u4e0bu5348uff0cu6d77u5b81u8857u5934u60cau73b03000u4e07u4fddu65f6u6377u8f66u961fuff0cu8fd9u662fu600eu4e48u4e86uff1f", "excerpt": "<p>u6628u513fu4e2au4e0bu5348uff0cu6d77u5b81u7684u8857u5934u5f88u662fu70edu95f9uff0cu4e00u6392u7684u4fddu65f6u6377u8f66u8f86u961fu4f0du5de1u6e38uff0cu90a3u573au9762u662fu76f8u5f53u58eeu89c2uff0cu636eu8bf4u662fu4fddu65f6u6377u5e74u4f1auff1fu5230u5e95u53d1u751fu4e86u4ec0u4e48uff0c ...</p>n", "date": "2016-01-08 10:08:34", "comment_count": 0, "custom_fields": {} }, { "id": 3530, "title": "u6628u5929u65e9u4e0auff0cu6d77u5b81u4e01u6865u53d1u751fu4e00u8d77u60b2u5267uff0cu649eu6210u8fd9u6837u5f00u7684u5f97u6709u591au5febuff01", "excerpt": "<p>u636eu7f51u53cbu7206u6599uff1au6628u5929u65e9u4e0a6u70b9u591au5de6u53f3uff0cu6d77u5b81u4e01u6865u8054u4e01u7ebfu9644u8fd1u53d1u751fu8f66u7978uff0cu4e00u8f86u96eau4f5bu5170u8f7fu8f66u649eu5728u4e86u6811u4e0auff0cu9a7eu9a76u5ba4u88abu6324u533eu3002u636eu56f4u89c2u7fa4u4f17 ...</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": "u6d77u5b81u8bb8u6751u53d1u751fu8f66u7978 u5730u4e0au90fdu662fu8840 u6c7du8f66u51b2u8fdbu8349u576auff01", "excerpt": "<p>u636eu7f51u53cbu201c@u5ff5u5ff5u4e0du5fd8u201du7206u6599uff1au6d77u5b81u897fu7ad9u62a5u56fdu6751u9644u8fd1u53d1u751fu4ea4u901au4e8bu6545uff0cu4e24u8f66u76f8u649euff0cu8f66u5934u635fu6bc1u4e25u91cduff0cu4e00u8f66u76f4u63a5u51b2u8fdbu4e86u8349u576au91ccuff0cu6709u4ebau5458 ...</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": "u5728u6d77u5b81uff0cu7adfu4e5fu80fdu770bu5230u5982u6b64u7f8eu4e3du7684u661fu7a7auff01", "excerpt": "<p>u8bb0u5f97u591au4e45u6ca1u6709u770bu5230u8fc7u661fu7a7au4e86u4e48uff1fu4e60u60efu4e8eu4e0du518du62acu5934u4ef0u671buff0cu4e60u60efu4e8eu88abu96feu973eu4ee5u53cau706fu706bu901au660eu56f4u7ed5uff0cu4e60u60efu4e8eu8d8au6765u8d8au539au91cdu7684u955cu7247u3002u4ecau5929uff0cu6307 ...</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": "u3010u7206u6599u3011u6d77u5b81u65b0u82d1u8defu4e24u8f66u76f8u649euff0cu8f66u76f4u63a5u9a91u5728u4e86u6811u4e0auff01", "excerpt": "<p>u636eu7f51u53cbu7206u6599uff1a14u65e5u4e0au5348uff0cu6d77u5b81u98ceu548cu4e3du82d1u95e8u53e3u4e24u8f66u76f8u649euff0cu8f66u5934u53d7u635fu4e25u91cduff0cu6811u90fdu649eu6b6au4e86uff01u76eeu524du4e8bu6545u7684u5177u4f53u539fu56e0u8fd8u5728u8c03u67e5u4e2du3002 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": "8u53f7u508du665auff0cu6d77u5b81u4e0au7a7au60cau73b0u4e0du660eu98deu884cu7269uff1f", "excerpt": "<p>8u53f7u508du665au4e94u70b9u5de6u53f3uff0cu6d77u5b81u5f88u591au7f51u53cbu8868u793au770bu5230u4e86u5929u7a7au4e2du7684u4e0du660eu98deu884cu7269u3002u5728u5929u7a7au4e2duff0cu6709u4e00u6761u50cfu706bu7badu7684u5c3eu90e8u55b7u5c04u51fau7684u706bu7130u4e00u822cu7684u98deu884c ...</p>n",
解决方案
使用Gson解析复杂的json数据
http://blog.csdn.net/tkwxty/article/details/34474501
我只是个搬运工。
解决方案二:
实体类的问题吧 你可以用gsonformat自动生成实体类
解决方案三:
快来个大神指点我一下
解决方案四:
这个 debug 连接到手机调试看看就知道了。
时间: 2025-01-19 09:15:25