这是一个效仿网易新闻客户端一个小页面的案例,通过此案例落实前期所学:
直接贴上代码成果
MainActivity
package com.zhongkechuangda.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.loopj.android.image.SmartImageView;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<News> newsList;
private TextView tv_title;
private TextView tv_content;
private TextView tv_type;
private ListView lv;
private View items;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获得需要的控件
lv = findViewById(R.id.lv);
initListDate();
}
//先拿到需要展示的数据列表
public void initListDate() {
new Thread() {
@Override
public void run() {
super.run();
try {
//从服务器上获取xml文件
String path = "http://192.168.0.102:8080/news.xml";
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
int code = conn.getResponseCode();
if (code == 200) {
InputStream in = con