Retrofit进行网络请求,如何解析XML
最近在学习一个项目"开源中国",遇到了些问题,原因是一些网络请求返回的数据是XML.
因为之前遇到的都是json多,很少解析xml.
通过翻阅资料得知现在的网络访问比较流行Retrofit,而且带有一个SimpleXmlConverterFactory转换器可以解析xml得到一个javabean对象.
可能是我用gsonorxmlformat插件生成的bean有问题,enqueue回调一直走失败方法.下面是代码,请各位前辈指点指点
java代码:
public class MainActivity extends AppCompatActivity {
private Retrofit mRetrofit;
private osChinaApp mOsChinaApp;
private Call<LoginUserBean> mCall;
private EditText mEtUsername;
private EditText mEtPwd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mEtUsername = (EditText) findViewById(R.id.et_username);
mEtPwd = (EditText) findViewById(R.id.et_pwd);
}
public void login(View view) {
initData(mEtUsername.getText().toString().trim(), mEtPwd.getText().toString().trim());
}
private void initData(String username, String psw) {
mRetrofit =
new Retrofit.Builder()
.baseUrl("http://www.oschina.net")
.addConverterFactory(SimpleXmlConverterFactory.create())
.build();
mOsChinaApp = mRetrofit.create(osChinaApp.class);
mCall = mOsChinaApp.getResponseXML(1, username, psw);
mCall.enqueue(new Callback<LoginUserBean>() {
@Override
public void onResponse(Call<LoginUserBean> call, Response<LoginUserBean> response) {
Toast.makeText(getApplication(), "ok", Toast.LENGTH_SHORT).show();
Log.e("123", response.body().getOschina().getUser().getName());
}
@Override
public void onFailure(Call<LoginUserBean> call, Throwable t) {
Toast.makeText(getApplication(), "gg", Toast.LENGTH_SHORT).show();
t.printStackTrace();
}
});
}
public interface osChinaApp {
@GET("/api/login_validate")
Call<LoginUserBean> getResponseXML(@Query("keep_login") int pager, @Query("username") String username, @Query("pwd") String pwd);
}
}
javabean:
public class LoginUserBean {
/**
* result : {"errorMessage":"登录成功","errorCode":1}
* user : {"uid":6666666,"score":0,"followers":0,"gender":1,"name":"Wecher","location":"广东 广州","portrait":"","favoritecount":0,"fans":0}
* notice : {"newLikeCount":0,"reviewCount":0,"newFansCount":0,"msgCount":1,"atmeCount":0}
*/
private OschinaBean oschina;
public OschinaBean getOschina() {
return oschina;
}
public void setOschina(OschinaBean oschina) {
this.oschina = oschina;
}
public static class OschinaBean {
/**
* errorMessage : 登录成功
* errorCode : 1
*/
private ResultBean result;
/**
* uid : 6666666
* score : 0
* followers : 0
* gender : 1
* name : Wecher
* location : 广东 广州
* portrait :
* favoritecount : 0
* fans : 0
*/
private UserBean user;
public ResultBean getResult() {
return result;
}
public void setResult(ResultBean result) {
this.result = result;
}
public UserBean getUser() {
return user;
}
public void setUser(UserBean user) {
this.user = user;
}
public static class ResultBean {
private String errorMessage;
private int errorCode;
public String getErrorMessage() {
return errorMessage;
}
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
public int getErrorCode() {
return errorCode;
}
public void setErrorCode(int errorCode) {
this.errorCode = errorCode;
}
}
public static class UserBean {
private int uid;
private int score;
private int followers;
private int gender;
private String name;
private String location;
private String portrait;
private int favoritecount;
private int fans;
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public int getFollowers() {
return followers;
}
public void setFollowers(int followers) {
this.followers = followers;
}
public int getGender() {
return gender;
}
public void setGender(int gender) {
this.gender = gender;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getPortrait() {
return portrait;
}
public void setPortrait(String portrait) {
this.portrait = portrait;
}
public int getFavoritecount() {
return favoritecount;
}
public void setFavoritecount(int favoritecount) {
this.favoritecount = favoritecount;
}
public int getFans() {
return fans;
}
public void setFans(int fans) {
this.fans = fans;
}
}
}
}
请求回来的xml:
<?xml version="1.0" encoding="UTF-8"?><oschina>
<result>
<errorCode>1</errorCode>
<errorMessage><![CDATA[登录成功]]></errorMessage>
</result>
<user>
<uid>6666666</uid>
<location><![CDATA[广东 广州]]></location>
<name><![CDATA[Wecher]]></name>
<followers>0</followers>
<fans>0</fans>
<score>0</score>
<portrait></portrait>
<favoritecount>0</favoritecount>
<gender>1</gender>
</user>
<notice>
<atmeCount>0</atmeCount>
<msgCount>1</msgCount>
<reviewCount>0</reviewCount>
<newFansCount>0</newFansCount>
<newLikeCount>0</newLikeCount>
</notice>
</oschina>
试过修改接口泛型为String,可以走onResponse回调...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论