Volley XML请求XmLRequest

Demo地址:https://github.com/HarryXR/VolleyXmlDemo
一:XmlRequest.java

public class XMLRequest extends Request<XmlPullParser> {

private final Listener<XmlPullParser> mListener;

public XMLRequest(int method, String url, Listener<XmlPullParser> listener,
        ErrorListener errorListener) {
    super(method, url, errorListener);
    mListener = listener;
}

public XMLRequest(String url, Listener<XmlPullParser> listener,
        ErrorListener errorListener) {
    this(Method.GET, url, listener, errorListener);
}

// 处理网络响应
@Override
protected Response<XmlPullParser> parseNetworkResponse(
        NetworkResponse response) {
    try {
        String xmlString = new String(response.data,
                HttpHeaderParser.parseCharset(response.headers));
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        XmlPullParser xmlPullParser = factory.newPullParser();
        xmlPullParser.setInput(new StringReader(xmlString));
        return Response.success(xmlPullParser,
                HttpHeaderParser.parseCacheHeaders(response));
    } catch (UnsupportedEncodingException e) {
        return Response.error(new ParseError(e));
    } catch (XmlPullParserException e) {
        return Response.error(new ParseError(e));
    }
}

// 回调
@Override
protected void deliverResponse(XmlPullParser response) {
    mListener.onResponse(response);
}

}

二、xml数据,建实体类
< city quName=”黑龙江” pyName=”heilongjiang” cityname=”哈尔滨” state1=”0” state2=”1” stateDetailed=”晴转多云” tem1=”19” tem2=”3” windState=”西风5-6级转西南风3-4级”/>
1、Weather.java

public class Weather {
private WeatherInfo weatherinfo;
public WeatherInfo getWeatherinfo() {
    return weatherinfo;
}
public void setWeatherinfo(WeatherInfo weatherinfo) {
    this.weatherinfo = weatherinfo;
}

}
2、WeatherInfo.java

public class WeatherInfo {
private String shengfen;
private String city;
private String weatherstate;
private String heighttemp;
private String lowtemp;
private String windstate;
private String time;
public String getTime() {
    return time;
}

public void setTime(String time) {
    this.time = time;
}

public String getTemp() {
    return temp;
}

public void setTemp(String temp) {
    this.temp = temp;
}

private String temp;

public String getShengfen() {
    return shengfen;
}

public void setShengfen(String shengfen) {
    this.shengfen = shengfen;
}

public String getCity() {
    return city;
}

public void setCity(String city) {
    this.city = city;
}

public String getWeatherstate() {
    return weatherstate;
}

public void setWeatherstate(String weatherstate) {
    this.weatherstate = weatherstate;
}

public String getHeighttemp() {
    return heighttemp;
}

public void setHeighttemp(String heighttemp) {
    this.heighttemp = heighttemp;
}

public String getLowtemp() {
    return lowtemp;
}

public void setLowtemp(String lowtemp) {
    this.lowtemp = lowtemp;
}

public String getWindstate() {
    return windstate;
}

public void setWindstate(String windstate) {
    this.windstate = windstate;
}

@Override
public String toString() {
    return "WeatherInfo [shengfen=" + shengfen + ", city=" + city
            + ", weatherstate=" + weatherstate + ", heighttemp="
            + heighttemp + ", lowtemp=" + lowtemp + ", windstate="
            + windstate + ", time=" + time + ", temp=" + temp + "]";
}

}
三,请求数据

public class GetXmlActivity extends Activity{
private WeatherInfo weather;
private RequestQueue mQueue;
private List<WeatherInfo> list;
Handler handler = new Handler() {
    public void handleMessage(Message msg) {
        switch (msg.arg1) {
            case 1:
                adapter = new Myadapter(GetXmlActivity.this, list);
                listview.setAdapter(adapter);
                break;

            default:
                break;
        }
    }
};
Myadapter adapter;
ListView listview;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mQueue = Volley.newRequestQueue(this);
    list = new ArrayList<WeatherInfo>();
    listview = (ListView) findViewById(R.id.listview);
    initRequestXml();
}

private void initRequestXml() {
    XMLRequest xmlRequest = new XMLRequest("http://flash.weather.com.cn/wmaps/xml/china.xml",
        new Response.Listener<XmlPullParser>() {

            @Override
            public void onResponse(XmlPullParser response) {
                try {
                    for (int eventType = response.getEventType();
                         eventType != XmlPullParser.END_DOCUMENT; eventType = response.next()) {
                        switch (eventType) {
                            case XmlPullParser.START_TAG:
                                String nodeName = response.getName();
                                if ("city".equals(nodeName)) {
                                    weather = new WeatherInfo();
                                    list.add(weather);
                                    String pName = response.getAttributeValue(0);
                                    weather.setShengfen(pName);
                                    String citynameString = response.getAttributeValue(2);
                                    weather.setCity(citynameString);
                                    String stateDetailed = response.getAttributeValue(5);
                                    weather.setWeatherstate(stateDetailed);
                                    String tem1 = response.getAttributeValue(6);
                                    weather.setHeighttemp(tem1);
                                    String tem2 = response.getAttributeValue(7);
                                    weather.setLowtemp(tem2);
                                    String windState = response.getAttributeValue(8);
                                    weather.setWindstate(windState);
                                }

                                break;
                        }
                    }
                } catch (XmlPullParserException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Message msg = Message.obtain();
                msg.arg1 = 1;
                handler.sendMessage(msg);
            }
        }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("TAG", error.getMessage(), error);
        }
    });
    mQueue.add(xmlRequest);
}

class Myadapter extends BaseAdapter {
    Context context;
    List<WeatherInfo> list;

    public Myadapter(Context context, List<WeatherInfo> list) {
        this.context = context;
        this.list = list;
    }

    public void addAll(List<WeatherInfo> list) {
        list.addAll(list);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = View.inflate(context, R.layout.listitem, null);
        TextView sheng = (TextView) view.findViewById(R.id.sheng);
        sheng.setText(list.get(position).getShengfen());
        TextView shi = (TextView) view.findViewById(R.id.shi);
        shi.setText(list.get(position).getCity());
        TextView tianqi = (TextView) view.findViewById(R.id.tianqi);
        tianqi.setText(list.get(position).getWeatherstate());
        TextView temp = (TextView) view.findViewById(R.id.temp);
        temp.setText(list.get(position).getHeighttemp());
        TextView feng = (TextView) view.findViewById(R.id.feng);
        feng.setText(list.get(position).getWindstate());
        return view;
    }
}

}

好了,到此基本就完成了,xml解析还是用android提供三大解析之一pull方式。

### 解决 PP-OCRv4 出现的错误 当遇到 `WARNING: The pretrained params backbone.blocks2.0.dw_conv.lab.scale not in model` 这样的警告时,这通常意味着预训练模型中的某些参数未能匹配到当前配置下的模型结构中[^2]。 对于此问题的一个有效解决方案是采用特定配置文件来适配预训练权重。具体操作方法如下: 通过指定配置文件 `ch_PP-OCRv4_det_student.yml` 并利用已有的最佳精度预训练模型 (`best_accuracy`) 来启动训练过程可以绕过上述不兼容的问题。执行命令如下所示: ```bash python3 tools/train.py -c configs/det/ch_PP-OCRv4/ch_PP-OCRv4_det_student.yml ``` 该方案不仅解决了参数缺失带来的警告,还能够继续基于高质量的预训练成果进行微调,从而提升最终检测效果。 关于蒸馏的概念,在机器学习领域内指的是将大型复杂网络(teacher 模型)的知识迁移到小型简单网络(student 模型)。这里 student 和 teacher 的关系是指两个不同规模或架构的神经网络之间的指导与被指导的关系;其中 teacher 已经经过充分训练并具有良好的性能,而 student 则试图模仿前者的行为模式以达到相似的效果但保持更高效的计算特性。 至于提到的 `Traceback` 错误信息部分,由于未提供具体的跟踪堆栈详情,难以给出针对性建议。不过一般而言,这报错往往涉及代码逻辑错误或是环境配置不当等问题。为了更好地帮助定位和解决问题,推荐记录完整的异常日志,并仔细检查最近修改过的代码片段以及确认依赖库版本的一致性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值