Git Product home page Git Product logo

Comments (6)

slaughtr avatar slaughtr commented on August 18, 2024

For no apparent reason, it started working again...

from xmltojson.

smart-fun avatar smart-fun commented on August 18, 2024

Strange things happens sometimes ^^

Thanks for using XmlToJson by the way !

Have a nice day !

Arnaud.

from xmltojson.

slaughtr avatar slaughtr commented on August 18, 2024

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.

smart-fun avatar smart-fun commented on August 18, 2024

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.

smart-fun avatar smart-fun commented on August 18, 2024

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.

slaughtr avatar slaughtr commented on August 18, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.