Android——Pull方式解析XML数据

XML文件建立已经Apache服务器建立已经在上一篇介绍过,此处不再多过阐述
正文:
添加jar包

implementation 'com.squareup.okhttp3:okhttp:3.4.1'

闭包里申请网络权限


布局文件中就一个Button,此处不再阐述,解析的XML数据将以日志方式体现
MainActivity.java代码如下:

public class MainActivity extends AppCompatActivity {
    private Button Request;
    private TextView ResponseText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Request = (Button) findViewById(R.id.Click);
        ResponseText = (TextView) findViewById(R.id.Response);

        Request.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SendRequestToOkhttp();
            }
        });
    }
    private void SendRequestToOkhttp() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient Client = new OkHttpClient();
                    Request request = new Request.Builder().url("http://192.168.0.60/data.xml").build();//发送请求
                    Response response = Client.newCall(request).execute();//发送请求并获取服务器返回的数据
                    String ResponseData = response.body().string();//获取具体数据
                    ParseXMLWithPull(ResponseData);//显示数据
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void ParseXMLWithPull(String XMLData){
        try{
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            XmlPullParser xmlPullParser = factory.newPullParser();
            xmlPullParser.setInput(new StringReader(XMLData));
            int eventType = xmlPullParser.getEventType();//设置初始节点
            String id = "";
            String name = "";
            String version = "";
            while (eventType != XmlPullParser.END_DOCUMENT){
                String nodename = xmlPullParser.getName();
                //判断标签类型
                switch (eventType){
                    //开始解析
                    case XmlPullParser.START_TAG:{//开始标签
                        if ("id".equals(nodename)){
                            id = xmlPullParser.nextText();
                        }else if ("name".equals(nodename)){
                            name = xmlPullParser.nextText();
                        }else if ("version".equals(nodename)){
                            version = xmlPullParser.nextText();
                        }
                        break;
                    }
                    case XmlPullParser.END_TAG:{//结束标签
                        if ("app".equals(nodename)){
                            Log.d("ID = ",id+"");
                            Log.d("Name = ",name+"");
                            Log.d("Version = ",version+"");
                        }
                        break;
                    }
                }
                eventType = xmlPullParser.next();
            }
        }catch (Exception e){
          e.printStackTrace();
        }
    }
}

你可能感兴趣的:(Android,Studio,java,android,xml,android,studio)