网络下载
package com.animee.test;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* Created by Administrator on 2018/1/13.
*/
public class HttpUtils {
public static String getStringContent(String path){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection();
InputStream is = conn.getInputStream();
byte[]buf = new byte[1024];
int hasRead = 0;
while((hasRead = is.read(buf))!=-1){
baos.write(buf,0,hasRead);
}
} catch (IOException e) {
e.printStackTrace();
}
return baos.toString();
}
}
package com.animee.test;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.util.ArrayList;
import java.util.List;
/**
* 完成
* 需要注意的:
* 1.关于xml解析,并没有固定的规范,在判断时大家要按照便签的名称进行分析,然后依次解析
* 2.关于解析类的封装,没有json数据类型的gsonformat的封装方式,但是大家要根据界面的需要进行封装数据,
* 看界面需要什么,就把什么添加到解析类当中。
* 3.步骤 :
* 1.布局---》找到控件
* 2.写解析生成的bean类,作为数据源
* 3.写数据源(空的),写适配器
* 4.设置适配器
* 5.网络请求数据
* 6,开始解析
* 7.把解析后的数据,添加到原有的数据源当中
* 8.通知适配器更新数据。
*
* */
public class MainActivity extends AppCompatActivity {
private TabLayout tabLayout;
private ViewPager vp;
// 接口
public String bjUrl = "http://api.map.baidu.com/telematics/v3/weather?location=%E5%8C%97%E4%BA%AC&output=xml&ak=mXBIDrvTOwwmYaTtN03Lo0j2";
public String syUrl = "http://api.map.baidu.com/telematics/v3/weather?location=%E6%B2%88%E9%98%B3&output=xml&ak=mXBIDrvTOwwmYaTtN03Lo0j2";
public String hzUrl = "http://api.map.baidu.com/telematics/v3/weather?location=%E4%B8%8A%E6%B5%B7&output=xml&ak=mXBIDrvTOwwmYaTtN03Lo0j2";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
tabLayout = (TabLayout) findViewById(R.id.tablayout);
vp = (ViewPager) findViewById(R.id.vp);
initPager();
}
private void initPager() {
List<Fragment>fragments = new ArrayList<>();
String url[] = {bjUrl,syUrl,hzUrl};
List<String>titleList = new ArrayList<>();
titleList.add("北京");
titleList.add("沈阳");
titleList.add("杭州");
for (int i = 0; i < 3; i++) {
Fragment frag = new WeatherFragment();
Bundle bundle = new Bundle();
bundle.putString("url",url[i]);
frag.setArguments(bundle);
fragments.add(frag);
}
VPAdapter adapter = new VPAdapter(getSupportFragmentManager(), fragments, titleList);
vp.setAdapter(adapter);
// 使TabLayout和ViewPager产生关联
tabLayout.setupWithViewPager(vp);
}
}
Pull解析
package com.animee.test;
import android.util.Log;
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/13.
*/
public class ParseXML {
public static String TAG = "ParseXML";
public static WeatherBean readXML(String content){
WeatherBean bean = new WeatherBean();
List<WeatherBean.WeatherDate> weatherList = new ArrayList<>();
List<WeatherBean.Zhishu> zhishuList = new ArrayList<>();
bean.setWeather_dateList(weatherList);
bean.setZhishuList(zhishuList);
// 开始解析
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser pullParser = factory.newPullParser();
ByteArrayInputStream bais = new ByteArrayInputStream(content.getBytes());
pullParser.setInput(bais,"utf-8");
int code = pullParser.getEventType();
WeatherBean.WeatherDate weatherDate = null;
WeatherBean.Zhishu zhishu = null;
while (code != XmlPullParser.END_DOCUMENT) {
String name = pullParser.getName();
Log.i(TAG, "readXML: ==name==="+name);
switch (code) {
case XmlPullParser.START_TAG:
if (name.equals("date")) {
Log.i(TAG, "readXML: ==date=创建对象了=");
weatherDate = new WeatherBean.WeatherDate();
weatherDate.setDate(pullParser.nextText());
}else if (name.equals("dayPictureUrl")) {
weatherDate.setDayPictureUrl(pullParser.nextText());
}else if (name.equals("nightPictureUrl")) {
weatherDate.setNightPictureUrl(pullParser.nextText());
}else if (name.equals("weather")) {
weatherDate.setWeather(pullParser.nextText());
}else if (name.equals("wind")) {
weatherDate.setWind(pullParser.nextText());
}else if (name.equals("temperature")) {
weatherDate.setTemperature(pullParser.nextText());
weatherList.add(weatherDate);
}else if (name.equals("title")) {
zhishu = new WeatherBean.Zhishu();
zhishu.setTitle(pullParser.nextText());
}else if (name.equals("zs")) {
zhishu.setZs(pullParser.nextText());
}else if (name.equals("tipt")) {
zhishu.setTipt(pullParser.nextText());
}else if (name.equals("des")){
zhishu.setDes(pullParser.nextText());
zhishuList.add(zhishu);
}
break;
case XmlPullParser.END_TAG:
if (name.equals("temperature")) {
Log.i(TAG, "readXML: ==添加到集合了=="); //没执行到? 没找到原因,但是换一种写法
// weatherList.add(weatherDate);
}else if (name.equals("des")) {
// zhishuList.add(zhishu);
}
break;
}
code = pullParser.next();
}
} catch (XmlPullParserException e) {
Log.i(TAG, "readXML: ====XmlPullParserException===");
e.printStackTrace();
} catch (IOException e) {
Log.i(TAG, "readXML: =====IOException=====");
e.printStackTrace();
}
Log.i(TAG, "readXML: ===="+bean);
return bean;
}
}
Fragment适配器
package com.animee.test;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.List;
/**
* Created by Administrator on 2018/1/13.
*/
public class VPAdapter extends FragmentPagerAdapter{
private List<Fragment>mDatas;
private List<String>mTitleList;
public VPAdapter(FragmentManager fm,List<Fragment>mDatas,List<String>mTitleList) {
super(fm);
this.mDatas = mDatas;
this.mTitleList = mTitleList;
}
@Override
public Fragment getItem(int position) {
return mDatas.get(position);
}
@Override
public int getCount() {
return mDatas.size();
}
@Override
public CharSequence getPageTitle(int position) {
return mTitleList.get(position);
}
}
Spinner适配器
package com.animee.test;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import java.util.List;
/**
* Created by Administrator on 2018/1/13.
*/
public class WeatherAdapter extends BaseAdapter{
private Context context;
private List<WeatherBean.WeatherDate>mDatas;
public WeatherAdapter(Context context, List<WeatherBean.WeatherDate> 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.WeatherDate weatherDate = mDatas.get(position);
holder.wenduTv.setText("温度:"+weatherDate.getTemperature());
holder.dateTv.setText(weatherDate.getDate());
holder.windTv.setText(weatherDate.getWind());
holder.weaherTv.setText("白天情况:"+weatherDate.getWeather());
Picasso.with(context).load(weatherDate.getDayPictureUrl()).into(holder.dayIv);
Picasso.with(context).load(weatherDate.getNightPictureUrl()).into(holder.nightTv);
return convertView;
}
class WeatherViewHolder{
TextView dateTv,wenduTv,windTv,weaherTv;
ImageView dayIv,nightTv;
public WeatherViewHolder(View view){
dateTv = (TextView) view.findViewById(R.id.sp_tv_date);
wenduTv = (TextView) view.findViewById(R.id.sp_tv_wendu);
windTv = (TextView) view.findViewById(R.id.sp_tv_fengxiang);
weaherTv = (TextView) view.findViewById(R.id.sp_tv_day);
dayIv = (ImageView) view.findViewById(R.id.sp_img_bt);
nightTv = (ImageView) view.findViewById(R.id.sp_img_night);
}
}
}
Fragment类
package com.animee.test;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Spinner;
import java.util.ArrayList;
import java.util.List;
/**
* A simple {@link Fragment} subclass.
*/
public class WeatherFragment extends Fragment {
private String url;
private ListView lv;
private Spinner sp;
private List<WeatherBean.WeatherDate> weatherList;
private List<WeatherBean.Zhishu> zhishuList;
private ZhishuAdapter zhishuAdapter;
private WeatherAdapter weatherAdapter;
private String TAG = "WeatherFragment";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_weather, container, false);
Bundle bundle = getArguments();
url = bundle.getString("url");
initView(view);
return view;
}
private void initView(View view) {
lv = (ListView)view.findViewById(R.id.fragment_lv);
View headerView = LayoutInflater.from(getActivity()).inflate(R.layout.header_layout,null);
lv.addHeaderView(headerView);
sp = (Spinner) headerView.findViewById(R.id.header_sp);
// 数据源:
weatherList = new ArrayList<>();
zhishuList = new ArrayList<>();
zhishuAdapter = new ZhishuAdapter(getActivity(),zhishuList);
lv.setAdapter(zhishuAdapter);
weatherAdapter = new WeatherAdapter(getActivity(),weatherList);
sp.setAdapter(weatherAdapter);
loadData(url);
}
private void loadData(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.readXML(s);
Log.i(TAG, "onPostExecute:Weather_dateList().size =="+bean.getWeather_dateList().size());
weatherList.addAll(bean.getWeather_dateList());
weatherAdapter.notifyDataSetChanged();
Log.i(TAG, "onPostExecute: ZhishuList().size====="+bean.getZhishuList().size());
zhishuList.addAll(bean.getZhishuList());
zhishuAdapter.notifyDataSetChanged();
}
}
}.execute(url);
}
}
适配器
package com.animee.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 java.util.List;
/**
* Created by Administrator on 2018/1/13.
*/
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.titletTv.setText(zhishu.getTitle());
holder.tiptTv.setText(zhishu.getTipt());
holder.zsTv.setText(zhishu.getZs());
holder.detailTv.setText(zhishu.getDes());
return convertView;
}
class ZhiShuViewHolder{
TextView titletTv,zsTv,tiptTv,detailTv;
public ZhiShuViewHolder(View view){
titletTv = (TextView) view.findViewById(R.id.lv_tv1);
zsTv = (TextView) view.findViewById(R.id.lv_tv3);
tiptTv = (TextView) view.findViewById(R.id.lv_tv2);
detailTv = (TextView) view.findViewById(R.id.lv_tv4);
}
}
}