Reputation: 768
I m Parsing xml into java model. While parsing the xml into java Object I m getting the com.thoughtworks.xstream.mapper.CannotResolveClassException Exception. here is my code. i m getting xml from the http response. and i want to convert it into java model
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;
}
}
here is a 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>
Upvotes: 1
Views: 1703
Reputation: 460
i like you to add the Car.java file and all other classes, which Car.java needs...
i successfully tried to reproduce the exception. maybe you like to take a look into the sourcecode GitHub:CarParser
If you execute the code, you will get the Exception thrown. So uncomment the line 22 and it works all fine.
Upvotes: 1