Android开发-Retrofit-AndroidStudio(四)simplexml解析XML

时间:2022-10-27 07:29:51

转载请注明出处:http://blog.csdn.net/iwanghang/article/details/53184885
项目源码下载:http://download.csdn.net/detail/iwanghang/9684356(免积分)

这里添加Retrofit下面的simplexml.jar包:

直接看下效果图:

Android开发-Retrofit-AndroidStudio(四)simplexml解析XML

Android开发-Retrofit-AndroidStudio(四)simplexml解析XML

Android开发-Retrofit-AndroidStudio(四)simplexml解析XML

MainActivity.java:

package com.iwanghang.retrofitxml;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.simplexml.SimpleXmlConverterFactory;

/**
* 需要导入simplexml的jar包
*/
public class MainActivity extends AppCompatActivity implements Callback<Channel> {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://www.inexus.co")
.addConverterFactory(SimpleXmlConverterFactory.create())
.build();
Service service = retrofit.create(Service.class);
Call<Channel> channel = service.getChannel();
channel.enqueue(this);
}

@Override
public void onResponse(Call<Channel> call, Response<Channel> response) {
Channel channel = response.body();
}

@Override
public void onFailure(Call<Channel> call, Throwable t) {
t.printStackTrace();
}
}

Service.java:

package com.iwanghang.retrofitxml;

import retrofit2.Call;
import retrofit2.http.GET;
// http://www.inexus.co/portal.php?mod=rss&catid=
public interface Service {
@GET("/portal.php?mod=rss&catid=")
Call<Channel> getChannel();
}

Channel.java:

package com.iwanghang.retrofitxml;

import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Path;
import org.simpleframework.xml.Root;

import java.util.List;

// @Root(strict = false) @注解(不严格检查)
@Root(strict = false)
public class Channel {
@Path("channel")
@Element(name = "title")
private String title;
@Path("channel")
@ElementList(name = "item", inline = true)
private List<Item> list;

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public List<Item> getList() {
return list;
}

public void setList(List<Item> list) {
this.list = list;
}
}

Item.java:

package com.iwanghang.retrofitxml;

import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;

@Root(strict = false)
public class Item {
@Element(name = "title")
private String title;
}

另外,build.gradle需要修改一个地方:

    //compile 'com.squareup.retrofit2:converter-simplexml:2.1.0'
compile ('com.squareup.retrofit2:converter-simplexml:2.1.0'){
exclude group: 'xpp3', module: 'xpp3'
exclude group: 'stax', module: 'stax-api'
exclude group: 'stax', module: 'stax'
}



欢迎移动开发爱好者交流
沈阳或周边城市公司有意开发Android,请与我联系
联系方式
Android开发-Retrofit-AndroidStudio(四)simplexml解析XML
微信:iwanghang
QQ:413711276
邮箱:iwanghang@qq.com




转载请注明出处:http://blog.csdn.net/iwanghang/article/details/53184885

项目源码下载: http://download.csdn.net/detail/iwanghang/9684356(免积分)