java xml 解析 listview_Android解析XML(PULL)展示到ListView

Android解析XML展示到ListView运行后的效果图如下:

b69ff02415fd2d37d7c12eb801ab80fd.png

服务端的请求页面

pageEncoding="UTF-8"%>

Insert title here

获取XML数据

服务端返回结果的页面

${s.sex}

服务端的Java代码

package com.zking.action;

import java.util.ArrayList;

import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import com.zking.entity.Student;

public class StudentAction extends ActionSupport{

public String getXML() throws Exception {

//查询数据库,获取数据

List students=new ArrayList<>();

for (int i = 1; i <=20; i++) {

Student student=new Student("小霜"+i, "女");

students.add(student);

}

//将对象集合保存到请求域中

ServletActionContext.getRequest().setAttribute("students", students);

return "dataResult";

}

}

服务端的配置文件(struts.xml)

/p>

"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"

"http://struts.apache.org/dtds/struts-2.3.dtd">

/dataResult.jsp

服务端的运行结果

4b43c0e283d4a9671e65a8509da628c8.png

Android (布局文件 activity_main.xml)

xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"

android:layout_width="match_parent" android:layout_height="match_parent"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

android:orientation="vertical"

tools:context="com.example.g150825_android29_parsexml.MainActivity">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="获取XML"

android:onClick="getXML"

/>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:id="@+id/lv_main_list"

>

Android(Java代码 MainActivity)

package com.example.g150825_android29_parsexml;

import android.app.ProgressDialog;

import android.os.AsyncTask;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.util.Log;

import android.util.Xml;

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 org.xmlpull.v1.XmlPullParser;

import java.io.BufferedReader;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.net.HttpURLConnection;

import java.net.MalformedURLException;

import java.net.URL;

import java.util.ArrayList;

import java.util.List;

public class MainActivity extends AppCompatActivity {

List studentList=new ArrayList<>();

private ListView lv_main_list;

private MyAdater myAdater;

private ProgressDialog progressDialog;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

//实例化进度条对话框

progressDialog = new ProgressDialog(this);

progressDialog.setMessage("正在拼命加载中.....");

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

//实例化适配器

//设置适配器

myAdater = new MyAdater();

lv_main_list.setAdapter(myAdater);

}

class MyAdater extends BaseAdapter{

@Override

public int getCount() {

return studentList.size();

}

@Override

public Object getItem(int i) {

return studentList.get(i);

}

@Override

public long getItemId(int i) {

return i;

}

@Override

public View getView(int i, View view, ViewGroup viewGroup) {

LinearLayout linearLayout=new LinearLayout(MainActivity.this);

linearLayout.setOrientation(LinearLayout.HORIZONTAL);

TextView textViewName=new TextView(MainActivity.this);

textViewName.setText(studentList.get(i).getName());

TextView textViewSex=new TextView(MainActivity.this);

textViewSex.setText(studentList.get(i).getSex());

linearLayout.addView(textViewName);

linearLayout.addView(textViewSex);

return linearLayout;

}

}

public void getXML(View view){

new MyTask().execute();

}

class MyTask extends AsyncTask{

private Student student;

@Override

protected void onPreExecute() {

super.onPreExecute();

progressDialog.show();

}

@Override

protected Object doInBackground(Object[] objects) {

//01.确定网络数据

String path="http://192.168.43.152:8080/G150825_S2SH/studentActiongetXML.action";

try {

//02.实例化URL

URL url=new URL(path);

//03.获取连接对象

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

//04.设置请求方式

httpURLConnection.setRequestMethod("GET");

//05.设置请求连接超时的时间(优化)

httpURLConnection.setConnectTimeout(5000);

//06.获取响应码(结果码)

int code=httpURLConnection.getResponseCode();

if (code==200){

//07.获取服务器返回过来的数据

InputStream is=httpURLConnection.getInputStream();

//测试(打印)

//缓冲字符流

// BufferedReader br=new BufferedReader(new InputStreamReader(is));

// String str=null;

// while ((str=br.readLine())!=null){

// Log.i("test",str);

// }

//解析XML(PULL)

XmlPullParser xmlPullParser=Xml.newPullParser();

xmlPullParser.setInput(is,"UTF-8");

int type=xmlPullParser.getEventType();

while (type!=XmlPullParser.END_DOCUMENT){

switch (type){

case XmlPullParser.START_TAG:

//获取开始标签

String startTagName=xmlPullParser.getName();

if ("student".equals(startTagName)){

student = new Student();

//获取name属性值

String name=xmlPullParser.getAttributeValue(0);

student.setName(name);

}else if("sex".equals(startTagName)){

//获取sex的文本值

String sex=xmlPullParser.nextText();

student.setSex(sex);

}

break;

case XmlPullParser.END_TAG:

//获取到结束标签的名字

String endTagName=xmlPullParser.getName();

if("student".equals(endTagName)){

studentList.add(student);

}

break;

}

type=xmlPullParser.next();

}

}

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

@Override

protected void onPostExecute(Object o) {

super.onPostExecute(o);

//通知适配器发生改变

myAdater.notifyDataSetChanged();

progressDialog.cancel();

}

}

}

Android (实体类 Student)

package com.example.g150825_android29_parsexml;

public class Student {

private String name;

private String sex;

public Student() {

super();

// TODO Auto-generated constructor stub

}

public Student(String name, String sex) {

super();

this.name = name;

this.sex = sex;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getSex() {

return sex;

}

public void setSex(String sex) {

this.sex = sex;

}

}

在清单文件中添加权限即可(AndroidManifest.xml)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值