先上代码public class FragmentMainchatnews extends Fragment {
private Button button;
private Bitmap imageView;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.main_chat_news, null);
Thread thread=new Thread(runnable);
Log.i("尼玛","0");
return view;
}
//创建Hander对象,用于接收子线程的
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
if (msg.arg1 == 1) {
List> listItems = new ArrayList>();
listItems= (List>) msg.obj;
// 创建一个SimpleAdapter实现界面的更新
SimpleAdapter simpleAdapter = new SimpleAdapter(getActivity(), listItems, R.layout.news_simple_item,
new String[]{"image", "title", "abs"},
new int[]{R.id.image, R.id.title, R.id.abs});
ListView list = (ListView) getView().findViewById(R.id.chat_news);
// 为ListView设置Adapter
list.setAdapter(simpleAdapter);
//imageView.setImageBitmap(bitmap);
}
}
;
};
//开启子线程得到数据
Runnable runnable= new Runnable() {
public void run() {
final Message message = handler.obtainMessage();
Parameters para = new Parameters();
Log.i("尼玛","1");
//para.put("keyword", "娱乐");
ApiStoreSDK.execute("http://apis.baidu.com/songshuxiansheng/news/news",
ApiStoreSDK.GET,
para,
new ApiCallBack() {
@Override
public void onSuccess(int status, String responseString) {
Log.i("连接状态:", "连接成功");
try {
JSONObject dataJson = new JSONObject(responseString);
JSONArray data = dataJson.getJSONArray("retData");
// 创建一个List集合,List集合的元素是Map
List> listItems = new ArrayList>();
//for (int i = 0; i
JSONObject info = data.getJSONObject(0);
Map listItem = new HashMap();
//imageView = getHttpBitmap(info.getString("image_url"));
listItem.put("title", info.getString("title"));
//listItem.put("image",imageView);
listItem.put("abs", info.getString("abstract"));
listItems.add(listItem);
message.obj = listItems;
//定义标识子线程
message.arg1 = 1;
handler.sendMessage(message);
//}
} catch (JSONException e) {
Log.i("获取到数据:", responseString);
}
}
@Override
public void onComplete() {
Log.i("sdkdemo", "onComplete");
}
@Override
public void onError(int status, String responseString, Exception e) {
Log.i("sdkdemo", "onError, status: " + status);
Log.i("sdkdemo", "errMsg: " + (e == null ? "" : e.getMessage()));
}
});
}
};
//获取网络图片
private Bitmap getHttpBitmap(String urlString) {
Log.i("尼玛","7");
URL url;
Bitmap bitmap = null;
try {
url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(6000);
connection.setDoInput(true);
connection.setUseCaches(true);
InputStream is = connection.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
is.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bitmap;
}
}
不知怎么回事就是不显示,请大神看看我这么写有什么错误嘛