Comments (6)
For no apparent reason, it started working again...
from xmltojson.
Strange things happens sometimes ^^
Thanks for using XmlToJson by the way !
Have a nice day !
Arnaud.
from xmltojson.
Hrm. Having the issue again today, with, yet again, no changes in code :(
Here's my code:
public static ArrayList<Paste> processResults(String type, Response response) {
ArrayList<Paste> pastes = new ArrayList<>();
//turn response body (XML) to string, build to JSON from that
try {
Log.d("API response", response.body().string());
XmlToJson resultJSON = new XmlToJson.Builder(response.body().string()).build();
Log.d("xmltojson result", resultJSON.toString());
JSONObject resultJSONObject = new JSONObject(resultJSON.toString());
JSONArray resultsJSONArray = resultJSONObject.getJSONArray("paste");
for (int i = 0; i < resultsJSONArray.length(); i++) {
JSONObject eachPaste = resultsJSONArray.getJSONObject(i);
final Paste paste = new Paste(eachPaste.optString("paste_title", "Untitled"), eachPaste.optString("paste_key", "No key"), eachPaste.optString("paste_date", "No date"), eachPaste.optString("paste_hits", "No hit count"), eachPaste.optString("paste_size", "No size"), eachPaste.optString("paste_expire_date", "No expire date"));
if (type.equals("trending")) {
paste.setBody(getTrendingPasteBody(paste));
} else if (type.equals("ownPastes")) {
paste.setBody(getOwnPasteBody(paste));
} else { Log.d("listService", "Something went wrong!"); }
pastes.add(paste);
}
} catch (JSONException e) { e.printStackTrace(); }
catch (IOException e) { e.printStackTrace(); }
return pastes;
}
And here's the logcat:
06-09 12:38:55.606 9394-10122/net.a40two.pext D/API response: <paste>
06-09 12:38:55.606 9394-10122/net.a40two.pext D/API response: <paste>
<paste_key>UadFsUs3</paste_key>
<paste_date>1496963323</paste_date>
<paste_title>asdf</paste_title>
<paste_size>8</paste_size>
<paste_expire_date>1499555323</paste_expire_date>
<paste_private>0</paste_private>
<paste_format_long>BrainFuck</paste_format_long>
<paste_format_short>bf</paste_format_short>
<paste_url>https://pastebin.com/UadFsUs3</paste_url>
<paste_hits>16</paste_hits>
</paste>
<paste>
<paste_key>g6yMtNCk</paste_key>
<paste_date>1496849400</paste_date>
<paste_title>Xi Xia here ya go</paste_title>
<paste_size>45</paste_size>
<paste_expire_date>1497454200</paste_expire_date>
<paste_private>0</paste_private>
<paste_format_long>C#</paste_format_long>
<paste_format_short>csharp</paste_format_short>
<paste_url>https://pastebin.com/g6yMtNCk</paste_url>
<paste_hits>49</paste_hits>
</paste>
<paste>
<paste_key>iVs4jjnm</paste_key>
<paste_date>1496429897</paste_date>
<paste_title>testytest</paste_title>
<paste_size>28</paste_size>
<paste_expire_date>0</paste_expire_date>
<paste_private>0</paste_private>
<paste_format_long>None</paste_format_long>
<paste_format_short>text</paste_format_short>
<paste_url>https://pastebin.com/iVs4jjnm</paste_url>
<paste_hits>51</paste_hits>
</paste>
06-09 12:38:55.611 9394-10122/net.a40two.pext I/XmlToJson: unknown xml eventType 1
06-09 12:38:55.611 9394-10122/net.a40two.pext D/xmltojson result: {}
06-09 12:38:55.612 9394-10122/net.a40two.pext W/System.err: org.json.JSONException: No value for paste
Any help is sincerely appreciated, as the API response working is 95% of requirements for this project.
from xmltojson.
Hello,
it looks like your xml is not valid: if I copy/paste it in a xml validator, I get an error. For example with this one: http://codebeautify.org/xmlvalidator
There are several objects called <paste> but no root for them. All the hierarchy must be contained in 1 root. So it fails when parsing the 2nd <paste> object.
Probably it worked when you received only 1 <paste>, but as soon as there are several objects, it fails.
One solution would be to add a root at the beginning, like <pastelist> and close it at the end </pastelist>. Then you can try the validator it works.
hope this helps,
Arnaud.
<pastelist>
<paste>
<paste_key>UadFsUs3</paste_key>
<paste_date>1496963323</paste_date>
<paste_title>asdf</paste_title>
<paste_size>8</paste_size>
<paste_expire_date>1499555323</paste_expire_date>
<paste_private>0</paste_private>
<paste_format_long>BrainFuck</paste_format_long>
<paste_format_short>bf</paste_format_short>
<paste_url>https://pastebin.com/UadFsUs3</paste_url>
<paste_hits>16</paste_hits>
</paste>
<paste>
<paste_key>g6yMtNCk</paste_key>
<paste_date>1496849400</paste_date>
<paste_title>Xi Xia here ya go</paste_title>
<paste_size>45</paste_size>
<paste_expire_date>1497454200</paste_expire_date>
<paste_private>0</paste_private>
<paste_format_long>C#</paste_format_long>
<paste_format_short>csharp</paste_format_short>
<paste_url>https://pastebin.com/g6yMtNCk</paste_url>
<paste_hits>49</paste_hits>
</paste>
<paste>
<paste_key>iVs4jjnm</paste_key>
<paste_date>1496429897</paste_date>
<paste_title>testytest</paste_title>
<paste_size>28</paste_size>
<paste_expire_date>0</paste_expire_date>
<paste_private>0</paste_private>
<paste_format_long>None</paste_format_long>
<paste_format_short>text</paste_format_short>
<paste_url>https://pastebin.com/iVs4jjnm</paste_url>
<paste_hits>51</paste_hits>
</paste>
</pastelist>
from xmltojson.
If it does not solve your problem, please launch in debug mode, put a breakpoint and get a value for
String xmlSource = response.body().string();
and copy/paste the value of xmlSource to this ticket so that I can have a closer look.
Arnaud.
from xmltojson.
That did it! This is my first foray into XML so I didn't realize what was going on. Oddly enough, it DID work before, with that same code. I'll try not to think about that too much.
Also, I still see the 06-09 13:39:20.753 17069-26300/net.a40two.pext I/XmlToJson: unknown xml eventType 1
bit in the log, even though everything is working.
Final code, in case anyone runs across this same issue someday:
public static ArrayList<Paste> processResults(String type, Response response) {
ArrayList<Paste> pastes = new ArrayList<>();
//turn response body (XML) to string, build to JSON from that
try {
String withRoot = "<pastelist>"+response.body().string()+"</pastelist>";
Log.d("API response", withRoot);
XmlToJson resultJSON = new XmlToJson.Builder(withRoot).build();
// Log.d("xmltojson result", resultJSON.toString());
JSONObject resultJSONObject = new JSONObject(resultJSON.toString());
JSONObject resultJSONObjectChildren = resultJSONObject.getJSONObject("pastelist");
JSONArray resultsJSONArray = resultJSONObjectChildren.getJSONArray("paste");
for (int i = 0; i < resultsJSONArray.length(); i++) {
JSONObject eachPaste = resultsJSONArray.getJSONObject(i);
final Paste paste = new Paste(eachPaste.optString("paste_title", "Untitled"), eachPaste.optString("paste_key", "No key"), eachPaste.optString("paste_date", "No date"), eachPaste.optString("paste_hits", "No hit count"), eachPaste.optString("paste_size", "No size"), eachPaste.optString("paste_expire_date", "No expire date"));
if (type.equals("trending")) {
paste.setBody(getTrendingPasteBody(paste));
} else if (type.equals("ownPastes")) {
paste.setBody(getOwnPasteBody(paste));
} else { Log.d("listService", "Something went wrong!"); }
pastes.add(paste);
}
} catch (JSONException e) { e.printStackTrace(); }
catch (IOException e) { e.printStackTrace(); }
return pastes;
}
from xmltojson.
Related Issues (20)
- empty tags are skipped HOT 3
- org.xmlpull.v1.XmlPullParserException: attr value delimiter missing! (position:START_TAG <root author='1.0'>@1:52 in java.io.StringReader@26f223d) HOT 3
- Different values from attribute HOT 1
- Failed to resolve: com.github.smart-fun:XmlToJson:1.4.4 HOT 1
- Truncated conversion xml file HOT 3
- An invalid xml can also be converted to json HOT 1
- What is ProGuard rules? HOT 2
- cant import repo HOT 1
- How to skip \n\t HOT 8
- Force list paths: use patterns instead of full paths HOT 4
- Can't parse XMLs containing BOM characters HOT 9
- Parse android xml views HOT 3
- To check: when a tag contains "<" or ">" HOT 1
- xml with list object conflict
- Error with json array HOT 1
- jsonToXml.toString() OutOfMemoryError
- Need to add underscore at each attribute start
- Can't parse XML with two space HOT 1
- Replace com.android.support:support-annotations with AndroidX library HOT 2
- escaping double quote ? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from xmltojson.