HttpResponse a JSON

I'm executing the following:

HttpGet getMethod = new HttpGet(url);
getMethod.addHeader("Accept","application/x-oslc-cm-change-request+json");
HttpResponse httpResponse = httpClient.execute(getMethod);

where the URL is pointing to an XML file, which is:

<oslc_cm:Collection oslc_cm:totalCount="1">
    <rtc_cm:Project rdf:resource="https://private"/>
</oslc_cm:Collection>

Next, I execute

InputStream is = httpResponse.getEntity().getContent();
String json = convertStreamToString(is);
JSON jsonObject = JSONSerializer.toJSON(json);

At the last line, I get an exception, JSONException: Invalid JSON String.

Is there something that I'm not doing, or missing something?

The variable json contains the http response, which is:

<!DOCTYPE html>

<!--
a comment

-->

<html>

<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=8">
<title></title>

<link type="text/css" rel="stylesheet" href="/es/jazz/web/_style/?inclu
VelSxGN_en_US&_proxyURL=%2Fjazz">
<link rel="shortcut icon" href="/es/jazz/web/net.jazz.ajax/jazz.ico">

<style type="text/css">
#net-jazz-ajax-NoScriptMessage {
    width: 100%;
    color: #D0D0D0;
    font-size: 2em;
    text-align: center;
    position: absolute;
    top: 1%;
    z-index: 999;
}
    </style>

</head>

<body class="tundra">
    <noscript><div id="net-jazz-ajax-NoScriptMessage">Javascript is disabled or not available in your Browser</div></noscript>
<div id="net-jazz-ajax-InitialLoadMessage">Loading...</div>
<div id="net-jazz-ajax-WorkbenchRoot"></div>

<script type="text/javascript">
        djConfig = {
                isDebug: false,
                usePlainJson: true,
                baseUrl: "/jazz/web/dojo/",
                locale: "en-us",
                localizationComplete: true
        };
        /*null*/
        net = {jazz: {ajax: {}}};
        net.jazz.ajax._contextRoot = "/jazz";
        net.jazz.ajax._webuiPrefix = "/web/";
</script>

<script type="text/javascript" src="/jazz/web/_js/?include=Z~&eten_US"></script>


<script type="text/javascript">
/* <![CDATA[ */

dojo.getObject('jazz.core.loader', true)._loaded=['Z'];
net.jazz.ajax._rootServices = 'https://private';

net.jazz.ajax._storageService = 'https://private';

net.jazz.ajax._queryService = 'https://private';

net.jazz.ajax._infoCenterRoot = 'https://private';

net.jazz.ajax._oauthRealmName = 'Jazz';

net.jazz.ajax._viewletServiceRoot = 'https://private';

net.jazz.ajax._viewletWebUIRoot = 'https://private';

net.jazz.ajax._hasLocalRepository = true;
net.jazz.ajax._proxyLegacyMode = false;
net.jazz.ajax._oauthDomains = ['https://private']

net.jazz.ajax._friendDomains = ['https://private']


/* ]]> */
</script>

<script type="text/javascript">
/* <![CDATA[ */
        dojo.addOnLoad( function() {
                net.jazz.ajax.ui.PlatformUI.createAndRunWorkbencweb.app.authrequired");
        });
/* ]]> */
</script>
</body>
</html

Muchas Gracias

preguntado el 01 de febrero de 12 a las 22:02

Why are you trying to parse XML like it's JSON, then? -

did you print out json? what did it contain? -

What is the purpose of this? Do you want to convert the XML inside the String json to JSON? This will not work. -

I added the output of the HTTP response (which is the variable json) -

2 Respuestas

You are missing something. You are trying to parse XML as JSON, even though the formats are completely different. Use an XML parser like SAX instead of a JSON parser.

Respondido 02 Feb 12, 02:02

URL url = new URL("http://www.example.com/");
URLConnection con = url.openConnection();
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
System.out.println(body);

Respondido el 06 de diciembre de 12 a las 22:12

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.