2013-03-28 12 views
0

친구 뒤에 ':'가 올 것으로 예상 됨. AndroidHive에서 샘플 JSON 구문 분석 프로젝트를 다운로드했습니다.org.json.JSONException : <key

코드는 다음과 같습니다

// 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()); 
} 

실행하는 동안, 그것은 java.lang.string cannot be converted to JSONObject 예외가 발생합니다. 그래서이 문제에 대한 해결책을 검색 한 후 I는

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

here 주어진 instructin하여이 개별로

 jObj = new JSONObject(json); 

이 라인 변경

. 이 후 내 logcat 다음과 같은 오류가 표시됩니다.

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): } 

나는 많은 친구들을 수색했다. 그러나 나는이 문제를 해결할 수 없다. 도와주세요.

참고 : 나는 ISA 서버 뒤에

내 URL을하고 있어요 것은 : http://api.androidhive.info/contacts/

내 log.i ("문자열 빌더", JSON) 다음을 반환합니다.

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> 

미리 감사드립니다.

+0

웹 서비스를 받기 위해 올바른 URL을 사용하고 있는지 확인하십시오. –

+0

내 URL 친구를 제공했습니다. 확인해 봐. – Gunaseelan

+0

다음 행에 기록 된 메시지를 게시하십시오 : Log.i ("String Builder", json); –

답변

4

HTML 웹 페이지를 얻는 중 JSON 응답을받지 못했습니다 (분명히 LogCat에 표시됨). 따라서 파싱 할 수 없습니다.

오류 코드 :

는 (당신의 URL은 403 웹 페이지로 응답) : (403) 금지. ISA 서버가 지정된 유니폼 URL (Resource Locator)을 거부했습니다. (12202)

수정하려면 열 수있는 적절한 URL을 사용하고 JSON 데이터를 반환하십시오.

+0

내 코드를 완벽하게 친구로 바꾸려면 어떤 변화가 있었습니까? – Gunaseelan

+0

올바른 URL 사용 – RvdK

+0

타격 할 URL을 변경하십시오. JSON 응답을 반환하는 적절한 웹 서비스를 가리키는 지 확인하십시오. – SudoRahul

관련 문제