/** * Parse the error message included in the body response, if any, and set the specific result * code * * @param bodyResponse okHttp response body * @param resultCode our own custom result code * @throws IOException */ private void parseErrorMessageAndSetCode(String bodyResponse, ResultCode resultCode) { if (bodyResponse != null && bodyResponse.length() > 0) { InputStream is = new ByteArrayInputStream(bodyResponse.getBytes()); ErrorMessageParser xmlParser = new ErrorMessageParser(); try { String errorMessage = xmlParser.parseXMLResponse(is); if (errorMessage != "" && errorMessage != null) { mCode = resultCode; mHttpPhrase = errorMessage; } } catch (Exception e) { Log_OC.w(TAG, "Error reading exception from server: " + e.getMessage()); // mCode stays as set in this(success, httpCode, headers) } } }
/** * Parse OCS node * @param parser * @return reason for exception * @throws XmlPullParserException * @throws IOException */ private String readError (XmlPullParser parser) throws XmlPullParserException, IOException { String errorMessage = ""; parser.require(XmlPullParser.START_TAG, ns , NODE_ERROR); while (parser.next() != XmlPullParser.END_TAG) { if (parser.getEventType() != XmlPullParser.START_TAG) { continue; } String name = parser.getName(); // read NODE_MESSAGE if (name.equalsIgnoreCase(NODE_MESSAGE)) { errorMessage = readText(parser); } else { skip(parser); } } return errorMessage; }
/** * Parse exception response * @param is * @return errorMessage for an exception * @throws XmlPullParserException * @throws IOException */ public String parseXMLResponse(InputStream is) throws XmlPullParserException, IOException { String errorMessage = ""; try { // XMLPullParser XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); factory.setNamespaceAware(true); XmlPullParser parser = Xml.newPullParser(); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false); parser.setInput(is, null); parser.nextTag(); errorMessage = readError(parser); } finally { is.close(); } return errorMessage; }