11_Android中HttpClient的应用,读取网络xml及xml解析流,Handler的应用,LayoutInflater的使用,SmartImageView的使用

本文涉及的产品
云解析 DNS,旗舰版 1个月
全局流量管理 GTM,标准版 1个月
公共DNS(含HTTPDNS解析),每月1000万次HTTP解析
简介: 1 所需的web项目结构如下:2 new.xml的文件内容如下: <?xml version="1.0" encoding="UTF-8" ?> <news>          <new>                    <title>3Q大战宣判: 腾讯获赔500万</title>


1 所需的web项目结构如下:

2 new.xml的文件内容如下:

<?xml version="1.0" encoding="UTF-8" ?>

<news>

         <new>

                   <title>3Q大战宣判: 腾讯获赔500</title>

                   <detail>最高法驳回360上诉, 维持一审宣判.</detail>

                   <comment>6427</comment>

                   <image>http://10.0.2.2:8080/NetEaseServer/images/1.jpg</image>

         </new>

         <new>

                   <title>今日之声:北大雕塑被戴口罩</title>

                   <detail>市民: 因雾霾起诉环保局; 公务员谈"紧日子": 坚决不出去.</detail>

                   <comment>681</comment>

                   <image>http://10.0.2.2:8080/NetEaseServer/images/2.jpg</image>

         </new>

         <new>

                   <title>奥巴马见达赖是装蒜</title>

                   <detail>外文局: 国际民众认可中国大国地位;法院: "流量清零"未侵权.</detail>

                   <comment>1359</comment>

                   <image>http://10.0.2.2:8080/NetEaseServer/images/3.jpg</image>

         </new>

         <new>

                   <title>轻松一刻: 我要沉迷学习不自拔</title>

                   <detail>放假时我醒了不代表我起床了, 如今我起床了不代表我醒了!</detail>

                   <comment>10116</comment>

                   <image>http://10.0.2.2:8080/NetEaseServer/images/4.jpg</image>

         </new>

         <new>

                   <title>男女那些事儿</title>

                   <detail>", 我在东莞被抓, 2万保释金, 快汇钱到xxx!"</detail>

                   <comment>10339</comment>

                   <image>http://10.0.2.2:8080/NetEaseServer/images/5.jpg</image>

         </new>

         <new>

                   <title>3Q大战宣判: 腾讯获赔500</title>

                   <detail>最高法驳回360上诉, 维持一审宣判.</detail>

                   <comment>6427</comment>

                   <image>http://10.0.2.2:8080/NetEaseServer/images/1.jpg</image>

         </new>

         <new>

                   <title>今日之声:北大雕塑被戴口罩</title>

                   <detail>市民: 因雾霾起诉环保局; 公务员谈"紧日子": 坚决不出去.</detail>

                   <comment>681</comment>

                   <image>http://10.0.2.2:8080/NetEaseServer/images/2.jpg</image>

         </new>

         <new>

                   <title>奥巴马见达赖是装蒜</title>

                   <detail>外文局: 国际民众认可中国大国地位;法院: "流量清零"未侵权.</detail>

                   <comment>1359</comment>

                   <image>http://10.0.2.2:8080/NetEaseServer/images/3.jpg</image>

         </new>

         <new>

                   <title>轻松一刻: 我要沉迷学习不自拔</title>

                   <detail>放假时我醒了不代表我起床了, 如今我起床了不代表我醒了!</detail>

                   <comment>10116</comment>

                   <image>http://10.0.2.2:8080/NetEaseServer/images/4.jpg</image>

         </new>

         <new>

                   <title>男女那些事儿</title>

                   <detail>", 我在东莞被抓, 2万保释金, 快汇钱到xxx!"</detail>

                   <comment>10339</comment>

                   <image>http://10.0.2.2:8080/NetEaseServer/images/5.jpg</image>

         </new>

         <new>

                   <title>3Q大战宣判: 腾讯获赔500</title>

                   <detail>最高法驳回360上诉, 维持一审宣判.</detail>

                   <comment>6427</comment>

                   <image>http://10.0.2.2:8080/NetEaseServer/images/1.jpg</image>

         </new>

         <new>

                   <title>今日之声:北大雕塑被戴口罩</title>

                   <detail>市民: 因雾霾起诉环保局; 公务员谈"紧日子": 坚决不出去.</detail>

                   <comment>681</comment>

                   <image>http://10.0.2.2:8080/NetEaseServer/images/2.jpg</image>

         </new>

         <new>

                   <title>奥巴马见达赖是装蒜</title>

                   <detail>外文局: 国际民众认可中国大国地位;法院: "流量清零"未侵权.</detail>

                   <comment>1359</comment>

                   <image>http://10.0.2.2:8080/NetEaseServer/images/3.jpg</image>

         </new>

         <new>

                   <title>轻松一刻: 我要沉迷学习不自拔</title>

                   <detail>放假时我醒了不代表我起床了, 如今我起床了不代表我醒了!</detail>

                   <comment>10116</comment>

                   <image>http://10.0.2.2:8080/NetEaseServer/images/4.jpg</image>

         </new>

         <new>

                   <title>男女那些事儿</title>

                   <detail>", 我在东莞被抓, 2万保释金, 快汇钱到xxx!"</detail>

                   <comment>10339</comment>

                   <image>http://10.0.2.2:8080/NetEaseServer/images/5.jpg</image>

         </new>

</news>

3 将上面这个web项目部署在云服务器上(这里为安全起见,我略去部分地址),访问地址为:http://XXX:8080/NetEaseServer/new.xml

4 下面开始编写android app项目,最终的项目截图如下:

5 编写Android的清单文件,如下:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.itheima28.neteasedemo"

    android:versionCode="1"

    android:versionName="1.0" >

 

    <uses-sdk

        android:minSdkVersion="8"

        android:targetSdkVersion="19" />

    <uses-permission android:name="android.permission.INTERNET"/>

 

    <application

        android:allowBackup="true"

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

        <activity

            android:name="com.itheima28.neteasedemo.MainActivity"

            android:label="@string/app_name" >

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

 

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>

 

</manifest>

6 编写布局文件activity_main.xml,内容如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".MainActivity" >

 

    <ListView

        android:id="@+id/lv_news"

        android:layout_width="match_parent"

        android:layout_height="match_parent" />

 

</RelativeLayout>

7 编写布局文件listview_item.xml,内容如下:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:padding="5dip" >

 

    <com.loopj.android.image.SmartImageView

        android:id="@+id/siv_listview_item_icon"

        android:layout_width="100dip"

        android:layout_height="60dip"

        android:src="@drawable/a" />

 

    <TextView

        android:id="@+id/tv_listview_item_title"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginLeft="3dip"

        android:layout_toRightOf="@id/siv_listview_item_icon"

        android:singleLine="true"

        android:text="3Q大战宣判: 腾讯获赔500"

        android:textColor="@android:color/black"

        android:textSize="17sp" />

 

    <TextView

        android:id="@+id/tv_listview_item_detail"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignLeft="@id/tv_listview_item_title"

        android:layout_below="@id/tv_listview_item_title"

        android:layout_marginTop="3dip"

        android:text="啊发送旅客登机挥发速度发送旅客登机"

        android:textColor="@android:color/darker_gray"

        android:textSize="14sp" />

 

    <TextView

        android:id="@+id/tv_listview_item_comment"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentBottom="true"

        android:layout_alignParentRight="true"

        android:text="668跟帖"

        android:textColor="#FF0000"

        android:textSize="12sp" />

 

</RelativeLayout>

8 用到的github上的SmartView,代码如下:

package com.loopj.android.image;

 

import android.content.Context;

import android.graphics.Bitmap;

 

public class BitmapImage implements SmartImage {

    private Bitmap bitmap;

 

    public BitmapImage(Bitmap bitmap) {

        this.bitmap = bitmap;

    }

 

    public Bitmap getBitmap(Context context) {

        return bitmap;

    }

}

package com.loopj.android.image;

 

import java.io.InputStream;

 

import android.content.ContentUris;

import android.content.ContentResolver;

import android.content.Context;

import android.provider.ContactsContract;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.net.Uri;

 

public class ContactImage implements SmartImage {

    private long contactId;

 

    public ContactImage(long contactId) {

        this.contactId = contactId;

    }

 

    public Bitmap getBitmap(Context context) {

        Bitmap bitmap = null;

        ContentResolver contentResolver = context.getContentResolver();

 

        try {

            Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId);

            InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(contentResolver, uri);

            if(input != null) {

                bitmap = BitmapFactory.decodeStream(input);

            }

        } catch(Exception e) {

            e.printStackTrace();

        }

 

        return bitmap;

    }

}

package com.loopj.android.image;

 

import android.content.Context;

import android.graphics.Bitmap;

 

public interface SmartImage {

    public Bitmap getBitmap(Context context);

}

package com.loopj.android.image;

 

import android.content.Context;

import android.graphics.Bitmap;

import android.os.Handler;

import android.os.Message;

 

public class SmartImageTask implements Runnable {

    private static final int BITMAP_READY = 0;

 

    private boolean cancelled = false;

    private OnCompleteHandler onCompleteHandler;

    private SmartImage image;

    private Context context;

 

    public static class OnCompleteHandler extends Handler {

        @Override

        public void handleMessage(Message msg) {

            Bitmap bitmap = (Bitmap)msg.obj;

            onComplete(bitmap);

        }

 

        public void onComplete(Bitmap bitmap){};

    }

 

    public abstract static class OnCompleteListener {

        public abstract void onComplete();

    }

 

    public SmartImageTask(Context context, SmartImage image) {

        this.image = image;

        this.context = context;

    }

 

    @Override

    public void run() {

        if(image != null) {

            complete(image.getBitmap(context));

            context = null;

        }

    }

 

    public void setOnCompleteHandler(OnCompleteHandler handler){

        this.onCompleteHandler = handler;

    }

 

    public void cancel() {

        cancelled = true;

    }

 

    public void complete(Bitmap bitmap){

        if(onCompleteHandler != null && !cancelled) {

            onCompleteHandler.sendMessage(onCompleteHandler.obtainMessage(BITMAP_READY, bitmap));

        }

    }

}

package com.loopj.android.image;

 

import android.content.Context;

import android.graphics.Bitmap;

import android.util.AttributeSet;

import android.widget.ImageView;

 

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

 

public class SmartImageView extends ImageView {

    private static final int LOADING_THREADS = 4;

    private static ExecutorService threadPool = Executors.newFixedThreadPool(LOADING_THREADS);

 

    private SmartImageTask currentTask;

 

 

    public SmartImageView(Context context) {

        super(context);

    }

 

    public SmartImageView(Context context, AttributeSet attrs) {

        super(context, attrs);

    }

 

    public SmartImageView(Context context, AttributeSet attrs, int defStyle) {

        super(context, attrs, defStyle);

    }

 

 

    // Helpers to set image by URL

    public void setImageUrl(String url) {

        setImage(new WebImage(url));

    }

 

    public void setImageUrl(String url, SmartImageTask.OnCompleteListener completeListener) {

        setImage(new WebImage(url), completeListener);

    }

 

    public void setImageUrl(String url, final Integer fallbackResource) {

        setImage(new WebImage(url), fallbackResource);

    }

 

    public void setImageUrl(String url, final Integer fallbackResource, SmartImageTask.OnCompleteListener completeListener) {

        setImage(new WebImage(url), fallbackResource, completeListener);

    }

 

    public void setImageUrl(String url, final Integer fallbackResource, final Integer loadingResource) {

        setImage(new WebImage(url), fallbackResource, loadingResource);

    }

 

    public void setImageUrl(String url, final Integer fallbackResource, final Integer loadingResource, SmartImageTask.OnCompleteListener completeListener) {

        setImage(new WebImage(url), fallbackResource, loadingResource, completeListener);

    }

 

 

    // Helpers to set image by contact address book id

    public void setImageContact(long contactId) {

        setImage(new ContactImage(contactId));

    }

 

    public void setImageContact(long contactId, final Integer fallbackResource) {

        setImage(new ContactImage(contactId), fallbackResource);

    }

 

    public void setImageContact(long contactId, final Integer fallbackResource, final Integer loadingResource) {

        setImage(new ContactImage(contactId), fallbackResource, fallbackResource);

    }

 

 

    // Set image using SmartImage object

    public void setImage(final SmartImage image) {

        setImage(image, null, null, null);

    }

 

    public void setImage(final SmartImage image, final SmartImageTask.OnCompleteListener completeListener) {

        setImage(image, null, null, completeListener);

    }

 

    public void setImage(final SmartImage image, final Integer fallbackResource) {

        setImage(image, fallbackResource, fallbackResource, null);

    }

 

    public void setImage(final SmartImage image, final Integer fallbackResource, SmartImageTask.OnCompleteListener completeListener) {

        setImage(image, fallbackResource, fallbackResource, completeListener);

    }

 

    public void setImage(final SmartImage image, final Integer fallbackResource, final Integer loadingResource) {

        setImage(image, fallbackResource, loadingResource, null);

    }

 

    public void setImage(final SmartImage image, final Integer fallbackResource, final Integer loadingResource, final SmartImageTask.OnCompleteListener completeListener) {

        // Set a loading resource

        if(loadingResource != null){

            setImageResource(loadingResource);

        }

 

        // Cancel any existing tasks for this image view

        if(currentTask != null) {

            currentTask.cancel();

            currentTask = null;

        }

 

        // Set up the new task

        currentTask = new SmartImageTask(getContext(), image);

        currentTask.setOnCompleteHandler(new SmartImageTask.OnCompleteHandler() {

            @Override

            public void onComplete(Bitmap bitmap) {

                if(bitmap != null) {

                    setImageBitmap(bitmap);

                } else {

                    // Set fallback resource

                    if(fallbackResource != null) {

                        setImageResource(fallbackResource);

                    }

                }

 

                if(completeListener != null){

                    completeListener.onComplete();

                }

            }

        });

 

        // Run the task in a threadpool

        threadPool.execute(currentTask);

    }

 

    public static void cancelAllTasks() {

        threadPool.shutdownNow();

        threadPool = Executors.newFixedThreadPool(LOADING_THREADS);

    }

}

package com.loopj.android.image;

 

import java.io.InputStream;

import java.net.URL;

import java.net.URLConnection;

 

import android.content.Context;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

 

public class WebImage implements SmartImage {

    private static final int CONNECT_TIMEOUT = 5000;

    private static final int READ_TIMEOUT = 10000;

 

    private static WebImageCache webImageCache;

 

    private String url;

 

    public WebImage(String url) {

        this.url = url;

    }

 

    public Bitmap getBitmap(Context context) {

        // Don't leak context

        if(webImageCache == null) {

            webImageCache = new WebImageCache(context);

        }

 

        // Try getting bitmap from cache first

        Bitmap bitmap = null;

        if(url != null) {

            bitmap = webImageCache.get(url);

            if(bitmap == null) {

                bitmap = getBitmapFromUrl(url);

                if(bitmap != null){

                    webImageCache.put(url, bitmap);

                }

            }

        }

 

        return bitmap;

    }

 

    private Bitmap getBitmapFromUrl(String url) {

        Bitmap bitmap = null;

 

        try {

            URLConnection conn = new URL(url).openConnection();

            conn.setConnectTimeout(CONNECT_TIMEOUT);

            conn.setReadTimeout(READ_TIMEOUT);

            bitmap = BitmapFactory.decodeStream((InputStream) conn.getContent());

        } catch(Exception e) {

            e.printStackTrace();

        }

 

        return bitmap;

    }

 

    public static void removeFromCache(String url) {

        if(webImageCache != null) {

            webImageCache.remove(url);

        }

    }

}

                                      package com.loopj.android.image;

 

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.lang.ref.SoftReference;

import java.util.concurrent.ConcurrentHashMap;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

 

import android.content.Context;

import android.graphics.Bitmap;

import android.graphics.Bitmap.CompressFormat;

import android.graphics.BitmapFactory;

 

public class WebImageCache {

    private static final String DISK_CACHE_PATH = "/web_image_cache/";

 

    private ConcurrentHashMap<String, SoftReference<Bitmap>> memoryCache;

    private String diskCachePath;

    private boolean diskCacheEnabled = false;

    private ExecutorService writeThread;

 

    public WebImageCache(Context context) {

        // Set up in-memory cache store

        memoryCache = new ConcurrentHashMap<String, SoftReference<Bitmap>>();

 

        // Set up disk cache store

        Context appContext = context.getApplicationContext();

        diskCachePath = appContext.getCacheDir().getAbsolutePath() + DISK_CACHE_PATH;

 

        File outFile = new File(diskCachePath);

        outFile.mkdirs();

 

        diskCacheEnabled = outFile.exists();

 

        // Set up threadpool for image fetching tasks

        writeThread = Executors.newSingleThreadExecutor();

    }

 

    public Bitmap get(final String url) {

        Bitmap bitmap = null;

 

        // Check for image in memory

        bitmap = getBitmapFromMemory(url);

 

        // Check for image on disk cache

        if(bitmap == null) {

            bitmap = getBitmapFromDisk(url);

 

            // Write bitmap back into memory cache

            if(bitmap != null) {

                cacheBitmapToMemory(url, bitmap);

            }

        }

 

        return bitmap;

    }

 

    public void put(String url, Bitmap bitmap) {

        cacheBitmapToMemory(url, bitmap);

        cacheBitmapToDisk(url, bitmap);

    }

 

    public void remove(String url) {

        if(url == null){

            return;

        }

 

        // Remove from memory cache

        memoryCache.remove(getCacheKey(url));

 

        // Remove from file cache

        File f = new File(diskCachePath, getCacheKey(url));

        if(f.exists() && f.isFile()) {

            f.delete();

        }

    }

 

    public void clear() {

        // Remove everything from memory cache

        memoryCache.clear();

 

        // Remove everything from file cache

        File cachedFileDir = new File(diskCachePath);

        if(cachedFileDir.exists() && cachedFileDir.isDirectory()) {

            File[] cachedFiles = cachedFileDir.listFiles();

            for(File f : cachedFiles) {

                if(f.exists() && f.isFile()) {

                    f.delete();

                }

            }

        }

    }

 

    private void cacheBitmapToMemory(final String url, final Bitmap bitmap) {

        memoryCache.put(getCacheKey(url), new SoftReference<Bitmap>(bitmap));

    }

 

    private void cacheBitmapToDisk(final String url, final Bitmap bitmap) {

        writeThread.execute(new Runnable() {

            @Override

            public void run() {

                if(diskCacheEnabled) {

                    BufferedOutputStream ostream = null;

                    try {

                        ostream = new BufferedOutputStream(new FileOutputStream(new File(diskCachePath, getCacheKey(url))), 2*1024);

                        bitmap.compress(CompressFormat.PNG, 100, ostream);

                    } catch (FileNotFoundException e) {

                        e.printStackTrace();

                    } finally {

                        try {

                            if(ostream != null) {

                                ostream.flush();

                                ostream.close();

                            }

                        } catch (IOException e) {}

                    }

                }

            }

        });

    }

 

    private Bitmap getBitmapFromMemory(String url) {

        Bitmap bitmap = null;

        SoftReference<Bitmap> softRef = memoryCache.get(getCacheKey(url));

        if(softRef != null){

            bitmap = softRef.get();

        }

 

        return bitmap;

    }

 

    private Bitmap getBitmapFromDisk(String url) {

        Bitmap bitmap = null;

        if(diskCacheEnabled){

            String filePath = getFilePath(url);

            File file = new File(filePath);

            if(file.exists()) {

                bitmap = BitmapFactory.decodeFile(filePath);

            }

        }

        return bitmap;

    }

 

    private String getFilePath(String url) {

        return diskCachePath + getCacheKey(url);

    }

 

    private String getCacheKey(String url) {

        if(url == null){

            throw new RuntimeException("Null url passed in");

        } else {

            return url.replaceAll("[.:/,%?&=]", "+").replaceAll("[+]+", "+");

        }

    }

}

9 需要的实体NewInfo.java

package com.itheima28.neteasedemo.domain;

 

/**

 * @author andong

 * 新闻信息实体类

 */

public class NewInfo {

 

    private String title; // 标题

    private String detail; // 详细

    private Integer comment; // 跟帖数量

    private String imageUrl; // 图片连接

    @Override

    public String toString() {

       return "NewInfo [title=" + title + ", detail=" + detail + ", comment="

              + comment + ", imageUrl=" + imageUrl + "]";

    }

    public NewInfo(String title, String detail, Integer comment, String imageUrl) {

       super();

       this.title = title;

       this.detail = detail;

       this.comment = comment;

       this.imageUrl = imageUrl;

    }

    public NewInfo() {

       super();

       // TODO Auto-generated constructor stub

    }

    public String getTitle() {

       return title;

    }

    public void setTitle(String title) {

       this.title = title;

    }

    public String getDetail() {

       return detail;

    }

    public void setDetail(String detail) {

       this.detail = detail;

    }

    public Integer getComment() {

       return comment;

    }

    public void setComment(Integer comment) {

       this.comment = comment;

    }

    public String getImageUrl() {

       return imageUrl;

    }

    public void setImageUrl(String imageUrl) {

       this.imageUrl = imageUrl;

    }

}

10 Activity中的代码如下:

package com.itheima28.neteasedemo;

 

import java.io.InputStream;

import java.util.ArrayList;

import java.util.List;

 

import org.apache.http.HttpResponse;

import org.apache.http.client.HttpClient;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.impl.client.DefaultHttpClient;

import org.xmlpull.v1.XmlPullParser;

 

import android.app.Activity;

import android.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.util.Log;

import android.util.Xml;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.ListView;

import android.widget.TextView;

import android.widget.Toast;

 

import com.itheima28.neteasedemo.domain.NewInfo;

import com.loopj.android.image.SmartImageView;

 

public class MainActivity extends Activity {

 

    private static final String TAG = "MainActivity";

    private final int SUCCESS = 0;

    private final int FAILED = 1;

    private ListView lvNews;

    private List<NewInfo> newInfoList;

   

    private Handler handler = new Handler() {

 

       /**

         * 接收消息

         */

       @Override

       public void handleMessage(Message msg) {

           switch (msg.what) {

           case SUCCESS:// 访问成功, 有数据

              // Listview列表绑定数据

              newInfoList = (List<NewInfo>) msg.obj;

 

              MyAdapter adapter = new MyAdapter();

              lvNews.setAdapter(adapter);

              break;

           case FAILED:  // 无数据

              Toast.makeText(MainActivity.this, "当前网络崩溃了.", 0).show();

              break;

           default:

              break;

           }

       }

    };

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

       

        init();

    }

 

    private void init() {

       lvNews = (ListView) findViewById(R.id.lv_news);

 

       // 抓取新闻数据

       new Thread(new Runnable() {

           @Override

           public void run() {

              // 获得新闻集合

              List<NewInfo> newInfoList = getNewsFromInternet();

              Message msg = new Message();

              if(newInfoList != null) {

                  msg.what = SUCCESS;

                  msg.obj = newInfoList;

              } else {

                  msg.what = FAILED;

              }

              handler.sendMessage(msg);

           }

       }).start();

      

      

    }

 

    /**

     * 返回新闻信息

     */

    private List<NewInfo> getNewsFromInternet() {

       HttpClient client = null;

       try {

           // 定义一个客户端

           client = new DefaultHttpClient();

          

           // 定义get方法

           HttpGet get = new HttpGet("http://114.215.142.191:8080/NetEaseServer/new.xml");

          

           // 执行请求

           HttpResponse response = client.execute(get);

          

           int statusCode = response.getStatusLine().getStatusCode();

          

           if(statusCode == 200) {

              InputStream is = response.getEntity().getContent();

              List<NewInfo> newInfoList = getNewListFromInputStream(is);

              return newInfoList;

           } else {

              Log.i(TAG, "访问失败: " + statusCode);

           }

       } catch (Exception e) {

           e.printStackTrace();

       } finally {

           if(client != null) {

              client.getConnectionManager().shutdown();     // 关闭和释放资源

           }

       }

       return null;

    }

   

    /**

     * 从流中解析新闻集合

     * @param is

     * @return

     */

    private List<NewInfo> getNewListFromInputStream(InputStream is) throws Exception {

       XmlPullParser parser = Xml.newPullParser();   // 创建一个pull解析器

       parser.setInput(is, "utf-8");   // 指定解析流, 和编码

      

       int eventType = parser.getEventType();

      

       List<NewInfo> newInfoList = null;

       NewInfo newInfo = null;

       while(eventType != XmlPullParser.END_DOCUMENT) { // 如果没有到结尾处, 继续循环

          

           String tagName = parser.getName(); // 节点名称

           switch (eventType) {

           case XmlPullParser.START_TAG: // <news>

              if("news".equals(tagName)) {

                  newInfoList = new ArrayList<NewInfo>();

              } else if("new".equals(tagName)) {

                  newInfo = new NewInfo();

              } else if("title".equals(tagName)) {

                  newInfo.setTitle(parser.nextText());

              } else if("detail".equals(tagName)) {

                  newInfo.setDetail(parser.nextText());

              } else if("comment".equals(tagName)) {

                  newInfo.setComment(Integer.valueOf(parser.nextText()));

              } else if("image".equals(tagName)) {

                  newInfo.setImageUrl(parser.nextText());

               }

              break;

           case XmlPullParser.END_TAG: // </news>

              if("new".equals(tagName)) {

                  newInfoList.add(newInfo);

              }

              break;

           default:

              break;

           }

           eventType = parser.next();      // 取下一个事件类型

       }

       return newInfoList;

    }

   

    class MyAdapter extends BaseAdapter {

 

       /**

        * 返回列表的总长度

        */

       @Override

       public int getCount() {

           return newInfoList.size();

       }

 

       /**

        * 返回一个列表的子条目的布局

        */

       @Override

       public View getView(int position, View convertView, ViewGroup parent) {

           View view = null;

          

           if(convertView == null) {

              LayoutInflater inflater = getLayoutInflater();

              view = inflater.inflate(R.layout.listview_item, null);

           } else {

              view = convertView;

           }

          

           // 重新赋值, 不会产生缓存对象中原有数据保留的现象

           SmartImageView sivIcon = (SmartImageView) view.findViewById(R.id.siv_listview_item_icon);

           TextView tvTitle = (TextView) view.findViewById(R.id.tv_listview_item_title);

           TextView tvDetail = (TextView) view.findViewById(R.id.tv_listview_item_detail);

           TextView tvComment = (TextView) view.findViewById(R.id.tv_listview_item_comment);

          

           NewInfo newInfo = newInfoList.get(position);

          

           sivIcon.setImageUrl(newInfo.getImageUrl());      // 设置图片

           tvTitle.setText(newInfo.getTitle());

           tvDetail.setText(newInfo.getDetail());

           tvComment.setText(newInfo.getComment() + "跟帖");

           return view;

       }

      

       @Override

       public Object getItem(int position) {

           return null;

       }

 

       @Override

       public long getItemId(int position) {

           return 0;

       }        

    }

}

 

至此,代码完成,相应的资源图片可以自己找图片替代。

目录
相关文章
|
23天前
|
开发框架 前端开发 Android开发
Flutter 与原生模块(Android 和 iOS)之间的通信机制,包括方法调用、事件传递等,分析了通信的必要性、主要方式、数据传递、性能优化及错误处理,并通过实际案例展示了其应用效果,展望了未来的发展趋势
本文深入探讨了 Flutter 与原生模块(Android 和 iOS)之间的通信机制,包括方法调用、事件传递等,分析了通信的必要性、主要方式、数据传递、性能优化及错误处理,并通过实际案例展示了其应用效果,展望了未来的发展趋势。这对于实现高效的跨平台移动应用开发具有重要指导意义。
90 4
|
11天前
|
JSON Java API
探索安卓开发:打造你的首个天气应用
在这篇技术指南中,我们将一起潜入安卓开发的海洋,学习如何从零开始构建一个简单的天气应用。通过这个实践项目,你将掌握安卓开发的核心概念、界面设计、网络编程以及数据解析等技能。无论你是初学者还是有一定基础的开发者,这篇文章都将为你提供一个清晰的路线图和实用的代码示例,帮助你在安卓开发的道路上迈出坚实的一步。让我们一起开始这段旅程,打造属于你自己的第一个安卓应用吧!
36 14
|
14天前
|
Java Linux 数据库
探索安卓开发:打造你的第一款应用
在数字时代的浪潮中,每个人都有机会成为创意的实现者。本文将带你走进安卓开发的奇妙世界,通过浅显易懂的语言和实际代码示例,引导你从零开始构建自己的第一款安卓应用。无论你是编程新手还是希望拓展技术的开发者,这篇文章都将为你打开一扇门,让你的创意和技术一起飞扬。
|
12天前
|
搜索推荐 前端开发 测试技术
打造个性化安卓应用:从设计到开发的全面指南
在这个数字时代,拥有一个定制的移动应用不仅是一种趋势,更是个人或企业品牌的重要延伸。本文将引导你通过一系列简单易懂的步骤,从构思你的应用理念开始,直至实现一个功能齐全的安卓应用。无论你是编程新手还是希望拓展技能的开发者,这篇文章都将为你提供必要的工具和知识,帮助你将创意转化为现实。
|
12天前
|
Java Android开发 开发者
探索安卓开发:构建你的第一个“Hello World”应用
在安卓开发的浩瀚海洋中,每个新手都渴望扬帆起航。本文将作为你的指南针,引领你通过创建一个简单的“Hello World”应用,迈出安卓开发的第一步。我们将一起搭建开发环境、了解基本概念,并编写第一行代码。就像印度圣雄甘地所说:“你必须成为你希望在世界上看到的改变。”让我们一起开始这段旅程,成为我们想要见到的开发者吧!
21 0
|
21小时前
|
SQL 安全 网络安全
网络安全与信息安全:知识分享####
【10月更文挑战第21天】 随着数字化时代的快速发展,网络安全和信息安全已成为个人和企业不可忽视的关键问题。本文将探讨网络安全漏洞、加密技术以及安全意识的重要性,并提供一些实用的建议,帮助读者提高自身的网络安全防护能力。 ####
33 17
|
11天前
|
存储 SQL 安全
网络安全与信息安全:关于网络安全漏洞、加密技术、安全意识等方面的知识分享
随着互联网的普及,网络安全问题日益突出。本文将介绍网络安全的重要性,分析常见的网络安全漏洞及其危害,探讨加密技术在保障网络安全中的作用,并强调提高安全意识的必要性。通过本文的学习,读者将了解网络安全的基本概念和应对策略,提升个人和组织的网络安全防护能力。
|
12天前
|
SQL 安全 网络安全
网络安全与信息安全:关于网络安全漏洞、加密技术、安全意识等方面的知识分享
随着互联网的普及,网络安全问题日益突出。本文将从网络安全漏洞、加密技术和安全意识三个方面进行探讨,旨在提高读者对网络安全的认识和防范能力。通过分析常见的网络安全漏洞,介绍加密技术的基本原理和应用,以及强调安全意识的重要性,帮助读者更好地保护自己的网络信息安全。
35 10
|
13天前
|
SQL 安全 网络安全
网络安全与信息安全:关于网络安全漏洞、加密技术、安全意识等方面的知识分享
在数字化时代,网络安全和信息安全已成为我们生活中不可或缺的一部分。本文将介绍网络安全漏洞、加密技术和安全意识等方面的内容,并提供一些实用的代码示例。通过阅读本文,您将了解到如何保护自己的网络安全,以及如何提高自己的信息安全意识。
43 10
|
14天前
|
存储 监控 安全
云计算与网络安全:云服务、网络安全、信息安全等技术领域的融合与挑战
本文将探讨云计算与网络安全之间的关系,以及它们在云服务、网络安全和信息安全等技术领域中的融合与挑战。我们将分析云计算的优势和风险,以及如何通过网络安全措施来保护数据和应用程序。我们还将讨论如何确保云服务的可用性和可靠性,以及如何处理网络攻击和数据泄露等问题。最后,我们将提供一些关于如何在云计算环境中实现网络安全的建议和最佳实践。