添加的权限
<uses-permission android:name="android.permission.INTERNET"/>
public class MainActivity extends AppCompatActivity {
private ListView lv ;private ArrayList<Data> list;
private MyAdapter mAdatper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv= (ListView) findViewById(R.id.lv);
startThead();
}
//开启子线程 从网络上获取数据 并解析
private void startThead(){
new Thread(){
@Override
public void run() {
super.run();
//访问网络
String str = HttpUtil.getStr(Cans.PATH_URL,"utf-8");
final ArrayList<Data> datas = PareUtils.getDatas(str);
runOnUiThread(new Runnable() {
@Override
public void run() {
list = datas;
mAdatper = new MyAdapter(list,MainActivity.this);
lv.setAdapter(mAdatper);
}
});
}
}.start();
}
}
//另一个类网址
public class Cans {
public static final String PATH_URL = "http://baobab.kaiyanapp.com/api/v4/tabs/selected?udid=11111&vc=168&vn=3.3.1&deviceModel=Huawei%36&first_channel=eyepetizer_baidu_market&last_channel=eyepetizer_baidu_market&system_version_code=20";
}
//另一个类实体类
public class Data {
private String title;
private String playURL;
public Data(String title, String playURL) {
this.title = title;
this.playURL = playURL;
}
public Data() {
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getPlayURL() {
return playURL;
}
public void setPlayURL(String playURL) {
this.playURL = playURL;
}
@Override
public String toString() {
return "Data{" +
"title='" + title + '\'' +
", playURL='" + playURL + '\'' +
'}';
}
}
//另一个类解析的
public class HttpUtil {
/**
*�ж�����״̬
*/
public static boolean isNetConnected(Context mContext){
ConnectivityManager manager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = manager.getActiveNetworkInfo();
if(info !=null && info.isConnectedOrConnecting()){
return true;
}
return false;
}
/**
* ����������Json����String
* @param path
* @return
*/
public static String getStr(String path,String encoding){
URL url = null;
HttpURLConnection huc = null;
BufferedReader rb = null;
// BufferedInputStream bis = null;
StringBuilder sbR = null;
try {
url = new URL(path);
huc = (HttpURLConnection) url.openConnection();
// huc.setConnectTimeout(5*1000);
huc.setRequestMethod("GET");
huc.connect();
if(huc.getResponseCode()==200){
rb = new BufferedReader(new InputStreamReader(huc.getInputStream()));
// bis = new BufferedInputStream(huc.getInputStream());
sbR = new StringBuilder();
byte[] b = new byte[1024];
String r;
while ((r=rb.readLine())!=null) {
sbR.append(r);
}
}
return sbR.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(rb!=null){
try {
rb.close();
} catch (IOException e) {
e.printStackTrace();
}
}
huc.disconnect();
}
return null;
}
/**
* �������ϻ�ȡbyte[]
* @param path
* @return
*/
public static byte[] getByte(String path){
URL url = null;
HttpURLConnection huc = null;
BufferedInputStream bis = null;
ByteArrayOutputStream bos = null;
byte[] byW=null;
try {
url = new URL(path);
huc = (HttpURLConnection) url.openConnection();
// huc.setConnectTimeout(5*1000);
huc.setRequestMethod("GET");
huc.connect();
if(huc.getResponseCode()==200){
bis = new BufferedInputStream(huc.getInputStream());
bos= new ByteArrayOutputStream();
byte[] byR = new byte[1024];
int len;
while ((len=bis.read(byR))!=-1) {
bos.write(byR, 0, len);
bos.flush();
}
byW = bos.toByteArray();
}
return byW;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(bis!=null){
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
huc.disconnect();
}
return null;
}
public static Bitmap getBitmap(String path){
URL url = null;
HttpURLConnection huc = null;
Bitmap bitmap = null;
try {
url = new URL(path);
huc = (HttpURLConnection) url.openConnection();
// huc.setConnectTimeout(5*1000);
huc.setRequestMethod("GET");
huc.connect();
if(huc.getResponseCode()==200){
bitmap = BitmapFactory.decodeStream(huc.getInputStream());
}
return bitmap;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
huc.disconnect();
}
return null;
}
//��byte[]ת��bitmap����
public static Bitmap ByteToBitmap(byte[] b ){
ByteArrayInputStream inputStream = null;
inputStream = new ByteArrayInputStream(b);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
}
}
//适配器
public class MyAdapter extends BaseAdapter {
private ArrayList<Data> list;
private Context mContext;
private LayoutInflater mInflater;
private ViewHolder vh = null ;
private boolean isFlag = true;
private Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
vh.progress_tv.setText(msg.what+"%");
if(msg.what ==100){
isFlag = false;
}
}
};
public MyAdapter(ArrayList<Data> list, Context context) {
this.list = list;
mContext = context;
mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
if(list!=null){
return list.size();
}
return 0;
}
@Override
public Data getItem(int i) {
if(list!=null){
return list.get(i);
}
return null;
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
if(view == null){
vh = new ViewHolder();
view = mInflater.inflate(R.layout.item_layout,null);
vh.title_tv = (TextView) view.findViewById(R.id.title_tv);
vh.mVideoView = (VideoView) view.findViewById(R.id.playUrl_videoview);
vh.progress_tv = (TextView) view.findViewById(R.id.progress_tv);
view.setTag(vh);
}else {
vh = (ViewHolder) view.getTag();
}
Data data = getItem(i);
vh.title_tv.setText(data.getTitle());
Uri uri = Uri.parse(data.getPlayURL());
vh.mVideoView.setVideoURI(uri);
vh.mVideoView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
vh.mVideoView.start();
}
});
return view;
}
class ViewHolder{
TextView title_tv;
VideoView mVideoView;
TextView progress_tv;
}
}
//另一个类
public class PareUtils {
public static ArrayList<Data> getDatas(String str){
ArrayList<Data> list = new ArrayList<>();
try {
JSONObject jo = new JSONObject(str);
JSONArray ja = jo.getJSONArray("itemList");
for (int i = 0; i <ja.length() ; i++) {
JSONObject jo1 = ja.getJSONObject(i);
JSONObject jo2 = jo1.getJSONObject("data");
String title = null;
if(jo2.has("title") ){
title = jo2.getString("title");
}
String url = null;
if(jo2.has("playInfo")){
JSONArray ja3 = jo2.getJSONArray("playInfo");
for (int j = 0; j < ja3.length(); j++) {
JSONObject jo4 = ja3.getJSONObject(j);
String name = jo4.getString("name");
if(name.equals("高清")){
url = jo4.getString("url");
}
}
}
if(title!=null && url !=null){
Data data = new Data(title,url);
list.add(data);
}
}
return list;
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
}