重慶分公司,新征程啟航
為企業提供網站建設、域名注冊、服務器等服務
為企業提供網站建設、域名注冊、服務器等服務
具體步驟如下:
創新互聯專注于企業成都全網營銷、網站重做改版、平和網站定制設計、自適應品牌網站建設、H5高端網站建設、商城開發、集團公司官網建設、外貿營銷網站建設、高端網站制作、響應式網頁設計等建站業務,價格優惠性價比高,為平和等各大城市提供網站開發制作服務。
1 布局文件中控件的設計
2 訪問遠程服務器的資源xml文件,該文件包含新聞的內容等信息
3 訪問到內容后把訪問內容顯示到頁面上
具體代碼如下:
1 MainActivity
package com.yuanlp.newsclient; import android.graphics.Color; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import com.yuanlp.newsclient.bean.NewsBean; import com.yuanlp.newsclient.utils.XMLToBean; import com.yuanlp.newsclient.view.SmartImageView; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.List; public class MainActivity extends AppCompatActivity { private static final int LOAD_ERROR =2 ; private static final int LOAD_SUCCESS =1 ; private ListView mLv_news; private LinearLayout mLoading; //new 一個Handler來做android的消息機制,處理子線程數據 private Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { //不管加載成功失敗,進度條應該隱藏 mLoading.setVisibility(View.INVISIBLE); switch (msg.what){ case LOAD_ERROR: Toast.makeText(MainActivity.this, "加載失敗", Toast.LENGTH_SHORT).show(); break; case LOAD_SUCCESS: //顯示listView的內容 mLv_news.setAdapter(new MyNewsAdapter()); break; } } }; private ListmList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /** * 初始化獲取界面元素 */ initView(); /** * 加載遠程資源到本地文件 */ loadMessage(); //readMessage(); } /** * 初始化界面 */ private void initView() { setContentView(R.layout.activity_main); mLv_news = (ListView) findViewById(R.id.lv_news); mLoading = (LinearLayout) findViewById(R.id.ll_loading); //打開客戶端后,設置ll_loading可見 mLoading.setVisibility(View.VISIBLE); } /** * 加載遠程資源到本地 */ private void loadMessage() { NewsBean newsBean=null; //訪問網絡不能再主線程中進行,需要一個新線程 new Thread(){ @Override public void run() { try { Thread.sleep(5000); NewsBean newsBean=null; URL url = new URL("http://192.168.1.107:8080/WebServer/news.xml"); HttpURLConnection conn= (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); int code = conn.getResponseCode(); if (code==200){ //獲取到資源 InputStream is = conn.getInputStream(); //調用utils方法,將xml文件轉為list類型返回 mList = XMLToBean.readStream(newsBean,is); //利用android的循環消息機制,放到UI線程中執行結果 Message msg = Message.obtain(); msg.what=LOAD_SUCCESS; handler.sendMessage(msg); }else{ Message msg = Message.obtain(); msg.what=LOAD_ERROR; handler.sendMessage(msg); } } catch (Exception e) { e.printStackTrace(); Message msg = Message.obtain(); msg.what=LOAD_ERROR; handler.sendMessage(msg); } } }.start(); } /** * 顯示listView中的數據 */ private class MyNewsAdapter extends BaseAdapter { @Override public int getCount() { return mList.size(); } @Override public View getView(int position, View convertView, ViewGroup parent) { //布局打氣筒,把xml資源文件轉變為view 即加載另外的一個布局文件 View view = View.inflate(MainActivity.this, R.layout.news, null); //獲取子布局文件中的各個控件 SmartImageView img = (SmartImageView) view.findViewById(R.id.iv_img); TextView tv_title= (TextView) view.findViewById(R.id.tv_title); TextView tv_desc= (TextView) view.findViewById(R.id.tv_desc); TextView tv_type= (TextView) view.findViewById(R.id.tv_type); //獲取當前位置的對象 NewsBean item = getItem(position); //設置圖片顯示 img.showImgByPath(item.getImg()); tv_title.setText(item.getTitle()); tv_desc.setText(item.getDescription()); String type=item.getType(); if ("1".equals(type)){ //當類型為1時,表示有評論,而且顯示評論數 tv_type.setText("評論:"+item.getComment()); tv_type.setTextColor(Color.BLACK); tv_type.setBackgroundColor(Color.TRANSPARENT); }else if ("2".equals(type)){ tv_type.setText("專題"); tv_type.setBackgroundColor(Color.RED); tv_type.setTextColor(Color.WHITE); }else if ("3".equals(type)){ tv_type.setText("直播"); tv_type.setBackgroundColor(Color.RED); tv_type.setTextColor(Color.WHITE); } return view; } @Override public NewsBean getItem(int position) { return mList.get(position); } @Override public long getItemId(int position) { return position; } } }
2 utils 中的將xml轉為list,可以參考前面幾天寫的博客,android解析xml。
package com.yuanlp.newsclient.utils; import android.util.Xml; import com.yuanlp.newsclient.bean.NewsBean; import org.xmlpull.v1.XmlPullParser; import java.io.InputStream; import java.util.ArrayList; import java.util.List; /** * Created by 原立鵬 on 2017/6/21. */ public class XMLToBean { public static ListreadStream(NewsBean newsBean, InputStream is){ List list=null; try { XmlPullParser xmlPullParser = Xml.newPullParser(); xmlPullParser.setInput(is,"utf-8"); int event=xmlPullParser.getEventType(); while(event!=XmlPullParser.END_DOCUMENT){ switch (event){ //根據eventType來區分,分為START_DOCUMENT,START_TAG,END_TAG,END_DOCUMENT case XmlPullParser.START_DOCUMENT: list=new ArrayList (); break; case XmlPullParser.START_TAG: String tagName=xmlPullParser.getName(); //獲取標簽名稱 if (tagName.equalsIgnoreCase("item")){ newsBean=new NewsBean(); }else if (tagName.equalsIgnoreCase("title")){ newsBean.setTitle(xmlPullParser.nextText()); }else if (tagName.equalsIgnoreCase("description")){ newsBean.setDescription(xmlPullParser.nextText()); }else if (tagName.equalsIgnoreCase("p_w_picpath")){ newsBean.setImg(xmlPullParser.nextText()); }else if (tagName.equalsIgnoreCase("type")){ newsBean.setType(xmlPullParser.nextText()); }else if (tagName.equalsIgnoreCase("comment")){ newsBean.setComment(xmlPullParser.nextText()); } break; case XmlPullParser.END_TAG: if (xmlPullParser.getName().equalsIgnoreCase("item")&&newsBean!=null){ list.add(newsBean); newsBean=null; } } event=xmlPullParser.next(); } } catch (Exception e) { e.printStackTrace(); } return list; } }
3 獲取遠程新聞的方法
package com.yuanlp.newsclient.view; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Handler; import android.os.Message; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.widget.ImageView; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; /** * Created by 原立鵬 on 2017/6/21. */ /** * 根據url地址去獲取遠程圖片,并顯示到新聞標題的左側 */ public class SmartImageView extends ImageView { private Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { Bitmap bitmap= (Bitmap) msg.obj; setImageBitmap(bitmap); } }; public SmartImageView(Context context) { super(context); } public SmartImageView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); } public SmartImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public void showImgByPath(final String imgURL){ new Thread(){ @Override public void run() { try { URL url = new URL(imgURL); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); int code = conn.getResponseCode(); if (code==200){ InputStream is = conn.getInputStream(); Bitmap bitmap = BitmapFactory.decodeStream(is); Message msg = Message.obtain(); msg.what=1; msg.obj=bitmap; handler.sendMessage(msg); } } catch (Exception e) { e.printStackTrace(); } } }.start(); } }
4 activity_main.xml
5 news.xml