gpt4 book ai didi

android - android 中的 com.thoughtworks.xstream.mapper.CannotResolveClassException

转载 作者:行者123 更新时间:2023-11-30 02:30:44 25 4
gpt4 key购买 nike

我正在将 xml 解析为 java 模型。在将 xml 解析为 java 对象时,我得到了 com.thoughtworks.xstream.mapper.CannotResolveClassException 异常。这是我的代码。我正在从 http 响应中获取 xml。我想把它转换成 java 模型

public class GetCarAsynTask extends AsyncTask<Void,Void,HttpResponse> {

private Context mContext;
private String mApiID;
private String mApikey;
private List<Car> list;
public GetCarAsynTask(Context context, String apiID, String apikey) {
mContext = context;
mApiID = apiID;
mApikey = apikey;
}
@Override
protected HttpResponse doInBackground(Void... params) {

HttpResponse httpResponse = null;
try {
String xmlData = XmlConverter.convertStreamToString(mContext.getAssets().open(ConstantsUtils.GET_CAR_FILE_NAME));
xmlData = String.format(xmlData, mApiID, mApikey);
httpResponse = WebServiceUtils.getCar(xmlData);
list = getCar(httpResponse);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return httpResponse;
}

private List<Car> getCar(HttpResponse response) {
List<Car> list;
try {
if (response != null) {
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream is = entity.getContent();
XStream xstream = new XStream();
xstream.alias("Car", Car.class);
xstream.setClassLoader(Car.class.getClassLoader());
list = (List<Car>) xstream.fromXML(is);
//list = XmlPullParserHandler.getInstance().parse(is);
return list;
}
}
} catch (Exception e) {
Log.i("car", "error");
}
return null;
}
}

这是一个xml

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope>
<soap:Body>
<GetCarsResponse>
<GetCarsResult>
<ResponseCode>0</ResponseCode>
<ResponseText>OK</ResponseText>
<Cars>
<Car>
<CarId>18</CarId>
<CarCode>NHO 314</CarCode>
<CarName>paddy</CarName>
<CarType>8 PAX LIMO</CarType>
</Car>
<Car>
<CarId>19</CarId>
<CarCode>NHO 352</CarCode>
<CarName />
<CarType>8 PAX LIMO</CarType>
</Car>
<Car>
<CarId>20</CarId>
<CarCode>NHO 382</CarCode>
<CarType>A CLASS</CarType>
</Car>
<Car>
<CarId>21</CarId>
<CarCode>NHO 417</CarCode>
<CarName />
<CarType>8 PAX LIMO</CarType>
</Car>
</Cars>
</GetCarsResult>
</GetCarsResponse>
</soap:Body>
</soap:Envelope>

最佳答案

我喜欢你添加 Car.java 文件和 Car.java 需要的所有其他类......

我成功地尝试重现异常。也许您想查看源代码 GitHub:CarParser

如果您执行代码,您将抛出异常。所以取消注释第 22 行,一切正常。

关于android - android 中的 com.thoughtworks.xstream.mapper.CannotResolveClassException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27378834/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com