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

时间:2014-12-09 12:20:40

标签: android xml xstream

我将xml解析为java模型。在将xml解析为java Object时,我得到了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>

1 个答案:

答案 0 :(得分:1)

我希望您添加Car.java文件以及Car.java需要的所有其他类...

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

如果执行代码,则会抛出异常。所以取消对第22行的注释,它的工作正常。