网络下载
package com.animee.day15.xml_test;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/**
* Created by Administrator on 2018/1/12.
*/
public class HttpUtils {
public static String getStringContent(String path){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
InputStream is = conn.getInputStream();
int hasRead = 0;
byte[]buf = new byte[1024];
while((hasRead = is.read(buf))!=-1){
baos.write(buf,0,hasRead);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return baos.toString();
}
public static byte[] getByteArray(String path){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
HttpURLConnection connection = (HttpURLConnection) new URL(path).openConnection();
InputStream is = connection.getInputStream();
int hasRead = 0;
byte[]buf = new byte[1024];
while((hasRead = is.read(buf))!=-1){
baos.write(buf,0,hasRead);
}
} catch (IOException e) {
e.printStackTrace();
}
return baos.toByteArray();
}
}
解析XML数据
package com.animee.day15.xml_test;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Administrator on 2018/1/12.
* 解析网络的xml数据的类
*/
public class ParseXML {
public static WeatherBean parseXML(String content){
WeatherBean bean = new WeatherBean();
List<WeatherBean.Weather>weatherList = new ArrayList<>();
List<WeatherBean.Zhishu>zhishuList = new ArrayList<>();
bean.setWeatherList(weatherList);
bean.setZhishuList(zhishuList);
// 开始解析---》pull解析
// 1.获得工厂类对象
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
// 2.获得解析器对象
XmlPullParser pullParser = factory.newPullParser();
// 3.设置需要解析的数据源
// 把String转化成输入流
ByteArrayInputStream bais = new ByteArrayInputStream(content.getBytes());
pullParser.setInput(bais,"utf-8"); //第一个参数:输入流,第二个参数:编码格式
// 4.获得响应码
int code = pullParser.getEventType();
// 在循环开始之前,想把添加到集合当中的对象的引用,获取到
WeatherBean.Weather weather = null;
WeatherBean.Zhishu zhishu = null;
while (code != XmlPullParser.END_DOCUMENT) {
// 获取正在被解析的标签名
String name = pullParser.getName();
switch (code) {
case XmlPullParser.START_TAG:
if (name.equals("city")) {
String city = pullParser.nextText();
bean.setCity(city);
}else if (name.equals("updatetime")) {
bean.setUpdatetime(pullParser.nextText());
}else if (name.equals("wendu")) {
bean.setWendu(pullParser.nextText());
}else if (name.equals("shidu")) {
bean.setShidu(pullParser.nextText());
}else if (name.equals("fengxiang")) {
bean.setFengxiang(pullParser.nextText());
}else if (name.equals("weather")) {
weather = new WeatherBean.Weather();
}else if (name.equals("date")) {
weather.setDate(pullParser.nextText());
}else if (name.equals("high")) {
weather.setHigh(pullParser.nextText());
}else if (name.equals("low")) {
weather.setLow(pullParser.nextText());
}else if (name.equals("zhishu")) {
zhishu = new WeatherBean.Zhishu();
}else if (name.equals("name")) {
zhishu.setName(pullParser.nextText());
}else if (name.equals("value")) {
zhishu.setValue(pullParser.nextText());
}else if (name.equals("detail")) {
zhishu.setDetail(pullParser.nextText());
}
break;
case XmlPullParser.END_TAG:
if (name.equals("weather")) {
weatherList.add(weather);
}else if (name.equals("zhishu")) {
zhishuList.add(zhishu);
}
break;
}
// 移动指针,指向下一个节点
code = pullParser.next();
}
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bean;
}
}
适配器
package com.animee.day15.xml_test;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.animee.day15.R;
import java.util.List;
/**
* Created by Administrator on 2018/1/12.
*/
public class WeatherAdapter extends BaseAdapter{
private Context context;
private List<WeatherBean.Weather>mDatas;
public WeatherAdapter(Context context, List<WeatherBean.Weather> mDatas) {
this.context = context;
this.mDatas = mDatas;
}
@Override
public int getCount() {
return mDatas.size();
}
@Override
public Object getItem(int position) {
return mDatas.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
WeatherViewHolder holder = null;
if (convertView==null) {
convertView = LayoutInflater.from(context).inflate(R.layout.item_sp,null);
holder = new WeatherViewHolder(convertView);
convertView.setTag(holder);
}else{
holder = (WeatherViewHolder) convertView.getTag();
}
WeatherBean.Weather weather = mDatas.get(position);
holder.highTv.setText(weather.getHigh());
holder.lowTv.setText(weather.getLow());
holder.dateTv.setText(weather.getDate());
return convertView;
}
class WeatherViewHolder{
TextView dateTv,highTv,lowTv;
public WeatherViewHolder(View view){
dateTv = (TextView) view.findViewById(R.id.tv_time);
highTv = (TextView) view.findViewById(R.id.tv_high);
lowTv = (TextView) view.findViewById(R.id.tv_low);
}
}
}
适配器
package com.animee.day15.xml_test;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.animee.day15.R;
import java.util.List;
/**
* Created by Administrator on 2018/1/12.
*/
public class ZhishuAdapter extends BaseAdapter{
private Context context;
private List<WeatherBean.Zhishu>mDatas;
public ZhishuAdapter(Context context, List<WeatherBean.Zhishu> mDatas) {
this.context = context;
this.mDatas = mDatas;
}
@Override
public int getCount() {
return mDatas.size();
}
@Override
public Object getItem(int position) {
return mDatas.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ZhishuViewHolder holder = null;
if (convertView==null) {
convertView = LayoutInflater.from(context).inflate(R.layout.item_lv,null);
holder = new ZhishuViewHolder(convertView);
convertView.setTag(holder);
}else{
holder = (ZhishuViewHolder) convertView.getTag();
}
WeatherBean.Zhishu zhishu = mDatas.get(position);
holder.nameTv.setText(zhishu.getName());
holder.valueTv.setText(zhishu.getValue());
holder.detailTv.setText(zhishu.getDetail());
return convertView;
}
class ZhishuViewHolder{
TextView nameTv,valueTv,detailTv;
public ZhishuViewHolder(View view){
nameTv = (TextView) view.findViewById(R.id.tv_name);
valueTv = (TextView) view.findViewById(R.id.tv_value);
detailTv = (TextView) view.findViewById(R.id.tv_detail);
}
}
}
实体类
package com.animee.day15.xml_test;
import java.util.List;
/**
* Created by Administrator on 2018/1/12.
*/
public class WeatherBean {
private String city;
private String updatetime;
private String wendu;
private String shidu;
private String fengxiang;
private List<Weather>weatherList;
private List<Zhishu>zhishuList;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getUpdatetime() {
return updatetime;
}
public void setUpdatetime(String updatetime) {
this.updatetime = updatetime;
}
public String getWendu() {
return wendu;
}
public void setWendu(String wendu) {
this.wendu = wendu;
}
public String getShidu() {
return shidu;
}
public void setShidu(String shidu) {
this.shidu = shidu;
}
public String getFengxiang() {
return fengxiang;
}
public void setFengxiang(String fengxiang) {
this.fengxiang = fengxiang;
}
public List<Weather> getWeatherList() {
return weatherList;
}
public void setWeatherList(List<Weather> weatherList) {
this.weatherList = weatherList;
}
public List<Zhishu> getZhishuList() {
return zhishuList;
}
public void setZhishuList(List<Zhishu> zhishuList) {
this.zhishuList = zhishuList;
}
public WeatherBean() {
}
public WeatherBean(String city, String updatetime, String wendu, String shidu, String fengxiang, List<Weather> weatherList, List<Zhishu> zhishuList) {
this.city = city;
this.updatetime = updatetime;
this.wendu = wendu;
this.shidu = shidu;
this.fengxiang = fengxiang;
this.weatherList = weatherList;
this.zhishuList = zhishuList;
}
public static class Weather{
private String date;
private String high;
private String low;
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getHigh() {
return high;
}
public void setHigh(String high) {
this.high = high;
}
public String getLow() {
return low;
}
public void setLow(String low) {
this.low = low;
}
public Weather() {
}
public Weather(String date, String high, String low) {
this.date = date;
this.high = high;
this.low = low;
}
}
public static class Zhishu{
private String name;
private String value;
private String detail;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Zhishu() {
}
public Zhishu(String name, String value, String detail) {
this.name = name;
this.value = value;
this.detail = detail;
}
}
}
Activity
package com.animee.day15.xml_test;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import com.animee.day15.R;
import java.util.ArrayList;
import java.util.List;
public class XMLTestActivity extends AppCompatActivity {
private ListView xmlLV;
private TextView cityTv,uptimeTv,currentTv;
private Spinner spinner;
private List<WeatherBean.Weather> weatherList; //Spinner的数据
private List<WeatherBean.Zhishu> zhishuList; //ListView的数据源
private ZhishuAdapter zhishuAdapter;
private WeatherAdapter weatherAdapter;
String url = "http://wthrcdn.etouch.cn/WeatherApi?citykey=101010100";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_xmltest);
xmlLV = (ListView) findViewById(R.id.xml_lv);
// 给ListView添加头布局
View headerView = LayoutInflater.from(this).inflate(R.layout.header_layout,null);
xmlLV.addHeaderView(headerView);
cityTv = (TextView) headerView.findViewById(R.id.tv_city);
uptimeTv = (TextView) headerView.findViewById(R.id.tv_time);
currentTv= (TextView) headerView.findViewById(R.id.tv_enviroment);
spinner = (Spinner) headerView.findViewById(R.id.sp_last_five);
// 设置数据源
weatherList = new ArrayList<>();
zhishuList = new ArrayList<>();
// 设置适配器
// 1.1设置listview的适配器
zhishuAdapter = new ZhishuAdapter(this,zhishuList);
xmlLV.setAdapter(zhishuAdapter);
// 1.2设置Spinner的适配器
weatherAdapter = new WeatherAdapter(this,weatherList);
spinner.setAdapter(weatherAdapter);
// 加载网络数据
loadWebData(url);
}
private void loadWebData(String url) {
new AsyncTask<String,Void,String>(){
@Override
protected String doInBackground(String... params) {
// 获取网络数据,并返回
String content = HttpUtils.getStringContent(params[0]);
return content;
}
@Override
protected void onPostExecute(String s) {
if (s!=null&&!s.isEmpty()) {
// 解析数据
WeatherBean bean = ParseXML.parseXML(s);
cityTv.setText("城市:"+bean.getCity());
uptimeTv.setText(bean.getUpdatetime());
currentTv.setText("当前温度:"+bean.getWendu()+",湿度:"+bean.getShidu()+",风向:"+bean.getFengxiang());
weatherList.addAll(bean.getWeatherList());
weatherAdapter.notifyDataSetChanged();
zhishuList.addAll(bean.getZhishuList());
zhishuAdapter.notifyDataSetChanged();
}
}
}.execute(url);
}
}
activitymain布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.animee.day15.xml_test.XMLTestActivity">
<ListView
android:id="@+id/xml_lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@color/colorPrimary"
android:dividerHeight="1dp"></ListView>
</RelativeLayout>
item_lv
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">
<TextView
android:id="@+id/tv_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="晨练指数:"
android:textSize="20sp"
android:textColor="@color/colorAccent"/>
<TextView
android:id="@+id/tv_value"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="适宜"
android:textSize="16sp"/>
<TextView
android:id="@+id/tv_detail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#eee"
android:padding="10dp"
android:textColor="@color/colorPrimary"
android:text="天气不错,空气清新,是您晨练的大好时机,建议不同年龄段的人们积极参加户外健身活动。" />
</LinearLayout> item_sp
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">
<TextView
android:id="@+id/tv_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="12日星期五"
android:textSize="20sp"/>
<TextView
android:id="@+id/tv_high"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/tv_time"
android:layout_marginTop="10dp"
android:text="高温2" />
<TextView
android:id="@+id/tv_low"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/tv_high"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginEnd="90dp"
android:layout_marginRight="90dp"
android:text="低温-8" />
</RelativeLayout>header_layout
<?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:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/tv_city"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:text="城市:北京"
android:textSize="22sp" />
<TextView
android:id="@+id/tv_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/tv_city"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginEnd="50dp"
android:layout_marginRight="50dp"
android:text="10:21"
android:textSize="18sp" />
<TextView
android:id="@+id/tv_enviroment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/tv_city"
android:layout_marginTop="10dp"
android:text="当前温度:-5"
android:textSize="18sp" />
<Spinner
android:id="@+id/sp_last_five"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/tv_enviroment"
android:layout_marginTop="20dp" />
<TextView
android:id="@+id/tv_zhishu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/sp_last_five"
android:layout_marginTop="10dp"
android:text="今日指数:"
android:textSize="20sp" />
</RelativeLayout> <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.animee.day15.demo02.PostActivity">
<TextView
android:id="@+id/tv_post"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.animee.day15">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:name=".UniteApp"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".xml_test.XMLTestActivity" />
<activity android:name=".day_test.DayActivity" />
<activity android:name=".demo01.GetDataActivity" />
<activity android:name=".demo02.PostActivity" />
<activity android:name=".demo03.ImageActivity"></activity>
</application>
</manifest>
3348

被折叠的 条评论
为什么被折叠?



