Gunaseelan
Gunaseelan

Reputation: 15535

org.json.JSONException: Expected ':' after <key

Friends. I have download a sample JSON parsing project from AndroidHive.

code is:

// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpPost = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();

is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
} catch (ClientProtocolException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
try {
    BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
        sb.append(line + "\n");
}
is.close();
json = sb.toString(); //json is String variable.
Log.i("String Builder", json);
} catch (Exception e) {
    Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
     jObj = new JSONObject(json);
} catch (JSONException e) {
    Log.e("JSON Parser", "Error parsing data " + e.toString());
}

while running, it throws java.lang.string cannot be converted to JSONObject exception. So after searching solution for this problem I changed

     jObj = new JSONObject(json);

this line as

    jObj = new JSONObject("{" + json + "}");

this line by the instructin given here. After this my logcat shows the following error.

03-28 16:24:23.722: E/JSON Parser(1516): Error parsing data org.json.JSONException: Expected ':' after <!DOCTYPE at character 12 of {<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
03-28 16:24:23.722: E/JSON Parser(1516): <HTML dir=ltr><HEAD><TITLE>The page cannot be displayed</TITLE>
03-28 16:24:23.722: E/JSON Parser(1516): <STYLE id=L_defaultr_1>A:link {
03-28 16:24:23.722: E/JSON Parser(1516):    FONT: 8pt/11pt verdana; COLOR: #ff0000
03-28 16:24:23.722: E/JSON Parser(1516): }
03-28 16:24:23.722: E/JSON Parser(1516): A:visited {
03-28 16:24:23.722: E/JSON Parser(1516):    FONT: 8pt/11pt verdana; COLOR: #4e4e4e
03-28 16:24:23.722: E/JSON Parser(1516): }
03-28 16:24:23.722: E/JSON Parser(1516): </STYLE>
03-28 16:24:23.722: E/JSON Parser(1516): <META content=NOINDEX name=ROBOTS>
03-28 16:24:23.722: E/JSON Parser(1516): <META http-equiv=Content-Type content="text-html; charset=UTF-8">
03-28 16:24:23.722: E/JSON Parser(1516): <META content="MSHTML 5.50.4522.1800" name=GENERATOR></HEAD>
03-28 16:24:23.722: E/JSON Parser(1516): <BODY bgColor=#ffffff>
03-28 16:24:23.722: E/JSON Parser(1516): <TABLE cellSpacing=5 cellPadding=3 width=410>
03-28 16:24:23.722: E/JSON Parser(1516):   <TBODY>
03-28 16:24:23.722: E/JSON Parser(1516):   <TR>
03-28 16:24:23.722: E/JSON Parser(1516):     <TD vAlign=center align=left width=360>
03-28 16:24:23.722: E/JSON Parser(1516):       <H1 id=L_defaultr_2 style="FONT: 13pt/15pt verdana; COLOR: #000000"><ID id=L_defaultr_3><!--Problem-->The page cannot be displayed
03-28 16:24:23.722: E/JSON Parser(1516): </ID></H1></TD></TR>
03-28 16:24:23.722: E/JSON Parser(1516):   <TR>
03-28 16:24:23.722: E/JSON Parser(1516):     <TD width=400 colSpan=2><FONT id=L_defaultr_4
03-28 16:24:23.722: E/JSON Parser(1516):       style="FONT: 8pt/11pt verdana; COLOR: #000000"><ID id=L_defaultr_5><B>Explanation: </B>There is a problem with the page you are trying to reach and it cannot be displayed.</ID></FONT></TD></TR>
03-28 16:24:23.722: E/JSON Parser(1516):   <TR>
03-28 16:24:23.722: E/JSON Parser(1516):     <TD width=400 colSpan=2><FONT id=L_defaultr_6 
03-28 16:24:23.722: E/JSON Parser(1516):       style="FONT: 8pt/11pt verdana; COLOR: #000000">
03-28 16:24:23.722: E/JSON Parser(1516):       <HR color=#c0c0c0 noShade>
03-28 16:24:23.722: E/JSON Parser(1516):       <P id=L_defaultr_7><B>Try the following:</B></P>
03-28 16:24:23.722: E/JSON Parser(1516):       <UL>
03-28 16:24:23.722: E/JSON Parser(1516):         <LI id=L_defaultr_8><B>Refresh page:</B> Search for the page again by clicking the Refresh button. The timeout may have occurred due to Internet congestion.
03-28 16:24:23.722: E/JSON Parser(1516): <LI id=L_defaultr_9><B>Check spelling:</B> Check that you typed the Web page address correctly. The address may have been mistyped.
03-28 16:24:23.722: E/JSON Parser(1516): <LI id=L_defaultr_10><B>Access from a link:</B> If there is a link to the page you are looking for, try accessing the page from that link.
03-28 16:24:23.722: E/JSON Parser(1516):       </UL>
03-28 16:24:23.722: E/JSON Parser(1516):       <HR color=#c0c0c0 noShade>
03-28 16:24:23.722: E/JSON Parser(1516):       <P id=L_defaultr_11>Technical Information (for support personnel)</P>
03-28 16:24:23.722: E/JSON Parser(1516):       <UL>
03-28 16:24:23.722: E/JSON Parser(1516):         <LI id=L_defaultr_12>Error Code: 403 Forbidden. The ISA Server denied the specified Uniform Resource Locator (URL). (12202)
03-28 16:24:23.722: E/JSON Parser(1516):         </UL></FONT></TD></TR></TBODY></TABLE></BODY></HTML>
03-28 16:24:23.722: E/JSON Parser(1516): }

I have searched lot friends. But I cant resolve this problem. So please help me.

Note: I am working behind ISA server

My Url is: http://api.androidhive.info/contacts/

My log.i("String builder",json) returns the following.

03-28 16:43:41.903: I/String Builder(1543): <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
03-28 16:43:41.903: I/String Builder(1543): <HTML dir=ltr><HEAD><TITLE>The page cannot be displayed</TITLE>
03-28 16:43:41.903: I/String Builder(1543): <STYLE id=L_defaultr_1>A:link {
03-28 16:43:41.903: I/String Builder(1543):     FONT: 8pt/11pt verdana; COLOR: #ff0000
03-28 16:43:41.903: I/String Builder(1543): }
03-28 16:43:41.903: I/String Builder(1543): A:visited {
03-28 16:43:41.903: I/String Builder(1543):     FONT: 8pt/11pt verdana; COLOR: #4e4e4e
03-28 16:43:41.903: I/String Builder(1543): }
03-28 16:43:41.903: I/String Builder(1543): </STYLE>
03-28 16:43:41.903: I/String Builder(1543): <META content=NOINDEX name=ROBOTS>
03-28 16:43:41.903: I/String Builder(1543): <META http-equiv=Content-Type content="text-html; charset=UTF-8">
03-28 16:43:41.903: I/String Builder(1543): <META content="MSHTML 5.50.4522.1800" name=GENERATOR></HEAD>
03-28 16:43:41.903: I/String Builder(1543): <BODY bgColor=#ffffff>
03-28 16:43:41.903: I/String Builder(1543): <TABLE cellSpacing=5 cellPadding=3 width=410>
03-28 16:43:41.903: I/String Builder(1543):   <TBODY>
03-28 16:43:41.903: I/String Builder(1543):   <TR>
03-28 16:43:41.903: I/String Builder(1543):     <TD vAlign=center align=left width=360>
03-28 16:43:41.903: I/String Builder(1543):       <H1 id=L_defaultr_2 style="FONT: 13pt/15pt verdana; COLOR: #000000"><ID id=L_defaultr_3><!--Problem-->The page cannot be displayed
03-28 16:43:41.903: I/String Builder(1543): </ID></H1></TD></TR>
03-28 16:43:41.903: I/String Builder(1543):   <TR>
03-28 16:43:41.903: I/String Builder(1543):     <TD width=400 colSpan=2><FONT id=L_defaultr_4
03-28 16:43:41.903: I/String Builder(1543):       style="FONT: 8pt/11pt verdana; COLOR: #000000"><ID id=L_defaultr_5><B>Explanation: </B>There is a problem with the page you are trying to reach and it cannot be displayed.</ID></FONT></TD></TR>
03-28 16:43:41.903: I/String Builder(1543):   <TR>
03-28 16:43:41.903: I/String Builder(1543):     <TD width=400 colSpan=2><FONT id=L_defaultr_6 
03-28 16:43:41.903: I/String Builder(1543):       style="FONT: 8pt/11pt verdana; COLOR: #000000">
03-28 16:43:41.903: I/String Builder(1543):       <HR color=#c0c0c0 noShade>
03-28 16:43:41.903: I/String Builder(1543):       <P id=L_defaultr_7><B>Try the following:</B></P>
03-28 16:43:41.903: I/String Builder(1543):       <UL>
03-28 16:43:41.903: I/String Builder(1543):         <LI id=L_defaultr_8><B>Refresh page:</B> Search for the page again by clicking the Refresh button. The timeout may have occurred due to Internet congestion.
03-28 16:43:41.903: I/String Builder(1543): <LI id=L_defaultr_9><B>Check spelling:</B> Check that you typed the Web page address correctly. The address may have been mistyped.
03-28 16:43:41.903: I/String Builder(1543): <LI id=L_defaultr_10><B>Access from a link:</B> If there is a link to the page you are looking for, try accessing the page from that link.
03-28 16:43:41.903: I/String Builder(1543):       </UL>
03-28 16:43:41.903: I/String Builder(1543):       <HR color=#c0c0c0 noShade>
03-28 16:43:41.903: I/String Builder(1543):       <P id=L_defaultr_11>Technical Information (for support personnel)</P>
03-28 16:43:41.903: I/String Builder(1543):       <UL>
03-28 16:43:41.903: I/String Builder(1543):         <LI id=L_defaultr_12>Error Code: 403 Forbidden. The ISA Server denied the specified Uniform Resource Locator (URL). (12202)
03-28 16:43:41.903: I/String Builder(1543):         </UL></FONT></TD></TR></TBODY></TABLE></BODY></HTML>

Thanks in advance.

Upvotes: 0

Views: 5065

Answers (1)

RvdK
RvdK

Reputation: 19800

You are not getting a JSON response, your getting a HTML webpage (Clearly visible in the LogCat). Therefore it cannot be parsed.

(your url responds with a 403 webpage: )

Error Code: 403 Forbidden. The ISA Server denied the specified Uniform Resource Locator (URL). (12202)

To fix, use a proper URL which you may open and returns JSON data.

Upvotes: 4

Related Questions