XmlPullParser error



Im having an issue trying to send some XML to a server. There is an error generated in code. unexpected token (position: text the page was not ..@1:68 in java.io.inputStreamReader@2106ff20)


This would lead one to believe there is an invalid character in the XML. I have ran the XML through a couple of online validators and it passes.



<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<Xml>
<JobPhotos>
<Photo>
<JobID>308</JobID>
<Photo>there is a valid image base 64 base string here</Photo>
<Comment>5tt</Comment>
<DateTaken>2014-09-17 15:11</DateTaken>
<UUID>cb8883f0-215a-4974-a36b-08cd3599800e</UUID>
<Active>True</Active>
</Photo>
</JobPhotos>
</Xml>


The photo 64 is also valid and has been run through an online decoder. Although if I comment out the photo node the XML is sucessfully sent to the server. Now for the really strange part!


the code runs fine on three different windows servers but is throwing this error on one server in particular. As it works fine on three different servers i dont think the code is the issue but instead some environmental issue.


Environment windows server 2012 R2 IIS 8.5


Code to send XML to server



public class SyncJobPhotosManager {

private static Context mContext;
private static PhotoTable photoTable;

static public AbstractResultAsyncTask syncJobPhotos(IResponder responder)
{
mContext=RemoteUtils.context;
photoTable = new PhotoTable(mContext);
return (AbstractResultAsyncTask) new SyncJobPhotos(responder).execute();
}

static public Boolean isAuthenticated = false;
static public String errorMessage;


static public void readIt(InputStream stream) throws IOException
{
// SAXParserFactory
SAXParserFactory fabrique = SAXParserFactory.newInstance();
SAXParser parseur;
try
{
parseur = fabrique.newSAXParser();
ParserBase handler = new ParserBase();

try
{
parseur.parse(stream, handler);
}
catch (EOFException errr)
{
errr.printStackTrace();
}
finally
{
if (parseur!=null)
{
isAuthenticated = handler.isAuthenticated();
errorMessage = handler.getErrorMessage();
}
}

} catch (SAXException e)
{
e.printStackTrace();
} catch (ParserConfigurationException e)
{
e.printStackTrace();
}
}

static private class SyncJobPhotos extends AbstractResultAsyncTask
{
final int engineerID;
final int realEngineerID;

public SyncJobPhotos(IResponder responder)
{
super(responder);
engineerID = Auth.currentEngineer(RemoteUtils.context).id;
realEngineerID = Auth.currentEngineer(RemoteUtils.context).EngineerID;
}

private byte[] getXmlByPhoto(Photo photo) throws IOException, ParseException
{

Date d = new Date();
String strDatetaken = CalendarUtils.getDate(d);

XmlSerializer serializer = Xml.newSerializer();
StringWriter writer = new StringWriter();

serializer.setOutput(writer);
serializer.startDocument("UTF-8", true);

serializer.startTag("", "Xml");


serializer.startTag("", "JobPhotos");

serializer.startTag("", "Photo");
XMLUtils.addTag(serializer, "JobID", photo.getJobID());
XMLUtils.addTag(serializer, "Photo", photo.getImageByteArray().toString());
XMLUtils.addTag(serializer, "Comment", photo.getComment());
XMLUtils.addTag(serializer, "DateTaken", strDatetaken);
XMLUtils.addTag(serializer, "UUID", photo.getId());
XMLUtils.addTag(serializer, "Active", photo.getActive());
serializer.endTag("", "Photo");

serializer.endTag("", "JobPhotos");


serializer.endTag("", "Xml");
serializer.endDocument();


String resultString = writer.toString();

return resultString.getBytes("UTF-8");
}

private static String getDateTime(Date d) {
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss", Locale.getDefault());

return dateFormat.format(d);
}



@Override
protected AbstractEvent doInBackground(Void... params)
{
AbstractEvent result = null;
//For administrator
if (realEngineerID == Auth.ADMIN_REAL_ID)
return new ResultEvent<Boolean>(true, this);
if (RemoteUtils.isOnline())
{


/* final String selection = JobsTable.Columns.EngineerID.name() + "=? AND " +
JobsTable.Columns.StatusID.name() + "=? AND " +
"(" + JobsTable.Columns.InitiatedOnServer.name() + "=? OR " +
JobsTable.Columns.InitiatedOnServer.name() + " is null" + ")";
final String[] selectionArgs = new String[]{String.valueOf(engineerID), String.valueOf(JobStatusDataSource.IN_PROGRESS.getID()), "0"};

Cursor cursor = RemoteUtils.context.getContentResolver().query(FusionContentProvider.URI_JOB, EnumUtils.nameToStringArray(JobsTable.Columns.SYNC_INITIATED_JOB), selection, selectionArgs, null);*/

Cursor cursor = photoTable.getPhotosToSync();
byte[] byteArray;
try
{
if (cursor.moveToFirst())
{
do
{
if (isCancelled())
break;
Photo photo = photoTable.cursorToPhoto(cursor);

byteArray = getXmlByPhoto(photo);
SoapObject request = new SoapObject(SoapMethods.NAMESPACE, SoapMethods.UPLOAD_JOB_PHOTOS);
request.addProperty(SoapMethods.PARAMS_GET_NEW_JOBS.engineerID.name(), realEngineerID);
request.addProperty(SoapMethods.PARAMS_GET_NEW_JOBS.password.name(), Auth.currentEngineer(RemoteUtils.context).MobilePassword);
request.addProperty("f", Base64.encodeToString(byteArray, Base64.DEFAULT));
request.addProperty("fileName", "job_photos.xml");

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
envelope.setOutputSoapObject(request);
envelope.dotNet = SoapMethods.getDotNetService();

Transport transport;
if (SoapMethods.getSSLEnabled())
transport = new HttpsTransportSE(SoapMethods.getHost(), SoapMethods.getPort(), "/" + SoapMethods.getFile(), 10000);
else
transport = new HttpTransportSE(SoapMethods.getURL());

transport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"utf-8\"?>");

try
{
transport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
transport.call(SoapMethods.UPLOAD_JOB_PHOTOS_ACTION, envelope);
SoapPrimitive resultsRequestSOAP = (SoapPrimitive) envelope.getResponse();

readIt(new ByteArrayInputStream(resultsRequestSOAP.toString().getBytes("UTF-8")));

if(isAuthenticated)
{
if (errorMessage == null || errorMessage.equals("") )
{

photoTable.updateFlagTrueByUUID(photo.getId());
//update job
/*job.initiatedOnServer = true;
Uri jobForUpdate = Uri.parse(FusionContentProvider.URI_JOB + "/" + String.valueOf(job.getID()));
ContentValues values = JobsDataSource.updateValues(job, JobsTable.Columns.SYNC_INITIATED_JOB);
RemoteUtils.context.getContentResolver().update(jobForUpdate, values, null, null);

//update JobTimeSheetTable job SET communicated with server = true
JobTimeSheetDataSource.updateSyncedJobTimesheetRecord(intJobTimeSheetID);*/
}
else
result = new FaultEvent(new Fault(errorMessage), this);
}
else
{
result = new FaultEvent(new Fault(errorMessage), this);
}

} catch (IOException e)
{
result = new FaultEvent(new Fault(e.getMessage()), this);
} catch (XmlPullParserException e)
{
result = new FaultEvent(new Fault(e.getMessage()), this);
}
catch (RuntimeException exc)
{
String message = "RuntimeException in " + SoapMethods.UPLOAD_JOB_PHOTOS;
String error = exc.getMessage();
if (error != null)
message = message.concat(": \n" + error);
result = new FaultEvent(new Fault(message), this);
}
} while (cursor.moveToNext());
}
if (result == null)
result = new ResultEvent<Boolean>(true, this);
} catch (Exception e)
{
result = new FaultEvent(new Fault(e.getMessage()), this);
} finally
{
if (cursor != null)
cursor.close();
}
} else
result = new FaultEvent(new Fault(Fault.Faults.NoConnection), this);
return result;
}
}


If anybody could shed any light on this issue it would be much appreciated. Thanks


No comments:

Post a Comment