溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何做一個簡易的新聞客戶端

發(fā)布時間:2020-07-17 12:27:07 來源:網絡 閱讀:685 作者:眉間雪 欄目:移動開發(fā)

1,下載一個服務端 tomcat

下載后開始運行,將需要瀏覽的東西,放在webapps-root文件下

這里假設有一個xml小文件,接下來就開始上代碼了,

在同一個包下給mainactivity創(chuàng)造兩個class文件,一個用來解析xml文件(解析方式多種,有興趣可以上網查閱資料),一個用于存放數據

1,存放數據:

package com.example.xinwen;


public class News {

private String city;

private String temp;

private String wind;

private String pm250;

private String p_w_picpath;

public String getCity() {

return city;

}

public void setCity(String city) {

this.city = city;

}

public String getTemp() {

return temp;

}

public void setTemp(String temp) {

this.temp = temp;

}

public String getWind() {

return wind;

}

public void setWind(String wind) {

this.wind = wind;

}

public String getPm250() {

return pm250;

}

public void setPm250(String pm250) {

this.pm250 = pm250;

}

public String getImage() {

return p_w_picpath;

}

public void setImage(String p_w_picpath) {

this.p_w_picpath = p_w_picpath;

}

}









2,解析xml文件

(1)假設xml文件長這樣,將它放進tomcat里面即可

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

<weather>

        <channel>

     <city>北京</city>

<temp>25°</temp>

<p_w_picpath>http://192.168.1.101:8080/img/a.jpg</p_w_picpath>

<wind>1</wind>

<pm250>300</pm250>

 

</channel>

<channel>

     <city>鄭州</city>

<temp>20°</temp>

<p_w_picpath>http://192.168.1.101:8080/img/b.jpg</p_w_picpath>

<wind>2</wind>

<pm250>300</pm250>

 

</channel>

<channel>

     <city>長春</city>

<temp>10°</temp>

<p_w_picpath>http://192.168.1.101:8080/img/c.jpg</p_w_picpath>

<wind>3</wind>

<pm250>100</pm250>

 

</channel>

<channel>

     <city>沈陽</city>

<temp>20°</temp>

<p_w_picpath>http://192.168.1.101:8080/img/d.jpg</p_w_picpath>

<wind>3</wind>

<pm250>50</pm250>

</channel>



</weather>

(2)開始解析

package com.example.xinwen;


import java.io.InputStream;

import java.util.ArrayList;

import java.util.List;


import org.xmlpull.v1.XmlPullParser;

import org.xmlpull.v1.XmlPullParserException;


import android.util.Xml;


public class XmlPasser {

//解析xml的業(yè)務方法

public static List<News> parserXml(InputStream in) throws Exception{

List<News> newsLists=null;

News news=null;

//獲取xml解析器

XmlPullParser parser=Xml.newPullParser();

//設置解析器,要解析的內容

parser.setInput(in,"utf-8");

//獲取要解析的事件類型

int type=parser.getEventType();

//不得向下解析

while(type!=XmlPullParser.END_DOCUMENT){

switch(type){

case XmlPullParser.START_TAG://解析開始節(jié)點

//[6]具體判斷一下是哪個標簽

if("weather".equals(parser.getName())){

newsLists=new ArrayList<News>();

}else if("channel".equals(parser.getName())){

news=new News();

}else if("city".equals(parser.getName())){

news.setCity(parser.nextText());

}else if("temp".equals(parser.getName())){

news.setTemp(parser.nextText());

}else if("p_w_picpath".equals(parser.getName())){

news.setImage(parser.nextText());

}else if("wind".equals(parser.getName())){

news.setWind(parser.nextText());

}else if("pm250".equals(parser.getName())){

news.setPm250(parser.nextText());

}

break;

case XmlPullParser.END_TAG://解析結束標簽

if("channel".equals(parser.getName())){

//把Javabean添加到集合

newsLists.add(news);

}

break;

}

//不停向下解析

type=parser.next();

}

return newsLists;

}

}

3,好了副class文件制作好了,就開始在mainactivity中制作正文了

package com.example.xinwen;


import android.os.Bundle;


import java.io.IOException;

import java.io.InputStream;

import java.net.HttpURLConnection;

import java.net.MalformedURLException;

import java.net.ProtocolException;

import java.net.URL;

import java.util.List;


import android.app.Activity;

import android.view.Menu;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.ImageView;

import android.widget.ListView;

import android.widget.TextView;


public class MainActivity extends Activity {

private List<News> newsLists;

private ListView lv;


@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

lv = (ListView) findViewById(R.id.lv);

//[2]準備listview要顯示的數據,去服務器取數據進行封裝

initListData();

}

//準備listview的數據

private void initListData(){

new Thread(){


public void run(){

try {

//[2]去服務器取數據http://192.168.1.104:8080/weather.xml

String path="http://192.168.1.101:8080/weather.xml";

                            //小白強烈注意,192.168.1.101使用的是本地ip,至于如何查看本地ip 上網百度

//[2.2]創(chuàng)建URL 對象指定我們要訪問的 網址(路徑)

URL url = new URL(path);

//[2.3]拿到httpurlconnection對象  用于發(fā)送或者接收數據 

HttpURLConnection  conn = (HttpURLConnection) url.openConnection();

//[2.4]設置發(fā)送get請求 

conn.setRequestMethod("GET");//get要求大寫  默認就是get請求

//[2.5]設置請求超時時間

conn.setConnectTimeout(5000);

//[2.6]獲取服務器返回的狀態(tài)碼 

int code = conn.getResponseCode();

//[2.7]如果code == 200 說明請求成功

if(code==200){

//[2.8]獲取服務器返回的數據   是以流的形式返回的  由于把流轉換成字符串是一個非常常見的操作  所以我抽出一個工具類(utils)

InputStream in = conn.getInputStream(); 

newsLists = XmlPasser.parserXml(in);

runOnUiThread(new Runnable(){


@Override

public void run() {

// TODO 自動生成的方法存根

//更新ui把數據展示到子線程

lv.setAdapter(new MyAdapter());

}

});

}

} catch (Exception e) {

// TODO 自動生成的 catch 塊

e.printStackTrace();

}

};}.start();

}

private class MyAdapter extends BaseAdapter{


@Override

public int getCount() {

// TODO 自動生成的方法存根

return newsLists.size();

}


@Override

public Object getItem(int arg0) {

// TODO 自動生成的方法存根

return null;

}


@Override

public long getItemId(int arg0) {

// TODO 自動生成的方法存根

return 0;

}


@Override

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

View view;

if(convertView==null){

view=View.inflate(getApplicationContext(), R.layout.item, null);

}else{

view =convertView;

}

//找到控件顯示集合里面的數據

ImageView iv_icon=(ImageView) view.findViewById(R.id.iv_icon);

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);

//展示數據

tv_title.setText(newsLists.get(position).getCity());

tv_desc.setText(newsLists.get(position).getTemp());

String typee=newsLists.get(position).getWind();

String comment=newsLists.get(position).getPm250();

int type=Integer.parseInt(typee);

switch(type){

case 1:

tv_type.setText(comment+"國內");

break;

case 2:

tv_type.setText("跟帖");

break;

case 3:

tv_type.setText("國外");

break;

}

return view;

}

}

}

至此src下的正文部分結束

我們接下來要來控制按鈕這些內容了,在res-layout下創(chuàng)建一個子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="match_parent" >


    <ImageView

        android:id="@+id/iv_icon"

        android:layout_width="80dp"

        android:layout_height="80dp"

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


    <TextView

        android:id="@+id/tv_title"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_marginTop="3dp"

        android:layout_toRightOf="@+id/iv_icon"

        android:ellipsize="end"

        android:singleLine="true"

        android:text="sadasaddsasdasdada"

        android:textColor="#000000"

        android:textSize="18sp" />


    <TextView

        android:id="@+id/tv_desc"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_alignBottom="@+id/iv_icon"

        android:layout_below="@id/tv_title"

        android:layout_marginTop="6dp"

        android:layout_toRightOf="@id/iv_icon"

        android:ellipsize="end"

        android:maxLines="2"

        android:text="啊結果了敬愛個路口就愛看的兩個件上的故事格式的公共的十大歌手大事"

        android:textColor="#999999"

        android:textSize="14sp" />


    <TextView

        android:id="@+id/tv_type"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignBottom="@id/iv_icon"

        android:layout_alignParentRight="true"

        android:layout_marginRight="3dp"

        android:text="跟帖"

        android:textColor="#ff0000"

        android:textSize="14sp" />


</RelativeLayout>

這樣就制作成功了,最后勿忘給安卓adt一個上網許可(百度)

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI