网络请求
public static String HttpGet1() {
String path = "http://i.dxy.cn/snsapi/event/count/list/all";
try {
URL url = new URL(path);
HttpURLConnection openConnection = (HttpURLConnection) url
.openConnection();
BufferedReader br = new BufferedReader(new InputStreamReader(
openConnection.getInputStream(), "utf-8"));
String readline = "";
StringBuffer sb = new StringBuffer();
while ((readline = br.readLine()) != null) {
sb.append(readline);
}
return sb.toString();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return null;
}
数据
public class MyThread1 extends Thread {
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
try {
tlist = new ArrayList<TBean>();
String str = HttpUrlConnection1.HttpGet1();
Log.i("TAg", str);
JSONObject json = new JSONObject(str);
JSONArray jsonArray = json.getJSONArray("items");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String title = jsonObject.getString("title");
String path = jsonObject.getString("path");
TBean bean = new TBean();
bean.setPath(path);
bean.setTitle(title);
tlist.add(bean);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
Message msg = new Message();
msg.obj = tlist;
msg.what = 1;
myHandler.sendMessage(msg);
}
}
public class MyThread extends Thread {
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
try {
slist = new ArrayList<Bean>();
String str = HttpUrlConnection1.HttpGet2();
Log.i("TAg", str);
JSONObject json = new JSONObject(str);
JSONArray jsonArray = json.getJSONArray("items");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String infoAvatar = jsonObject.getString("infoAvatar");
String section = jsonObject.getString("section");
String content=jsonObject.getString("content");
Bean bean = new Bean();
bean.setInfoAvatar(infoAvatar);
bean.setSection(section);
bean.setContent(content);
slist.add(bean);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
Message msg = new Message();
msg.obj = slist;
msg.what = 2;
myHandler.sendMessage(msg);
}
}
public Handler myHandler = new Handler() {
@SuppressWarnings("unchecked")
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 1:
t = (List<TBean>) msg.obj;
MyAdapter adapter = new MyAdapter(MainActivity.this, t);
vp.setAdapter(adapter);
// 写一个计时器
Timer tt = new Timer();
TimerTask task = new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
runOnUiThread(new Runnable() {
public void run() {
int index = vp.getCurrentItem();
vp.setCurrentItem(++index);
}
});
}
};
tt.schedule(task, 1000, 2000);
vp.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
// 将arg0换算到viewList的索引范围之内
int position = arg0 % t.size();
// 遍历所有的点对应的ImageView ,判断点的索引是否跟ViewPager当前的索引一致
for (int i = 0; i < ll.getChildCount(); i++) {
ImageView image = (ImageView) ll.getChildAt(i);
if (i == position) {
image.setSelected(true);
} else {
image.setSelected(false);
}
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
// 添加下面的圆点
for (int i = 0; i < t.size(); i++) {
// 动态实例化ImageView对象,添加到LinearLayout里面
ImageView imageview = new ImageView(getApplicationContext());
// 手动代码设置间距
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(5, 0, 5, 0);
// 将当前属性设置给ImageView
imageview.setLayoutParams(params);
// 给ImageView设置显示资源
imageview.setBackgroundResource(R.drawable.item_selector);
// 将ImageView添加到LinearLayout里面
ll.addView(imageview);
// 设置默认选中第一个
if (i == 0) {
imageview.setSelected(true);
}
}
break;
case 2:
List<Bean> ssslist=(List<Bean>) msg.obj;
MyBeanAdapter bAdapter=new MyBeanAdapter(MainActivity.this,ssslist);
lv.setAdapter(bAdapter);
default:
break;
}
};
};
}