2012-04-24 4 views
-1

나는 이렇게하는 데 많은 혼란을 겪고 있습니다. 내가해야 할 일은 서버에서 JSONP 응답을 파싱하고 표시하는 것입니다. Android에서 JSONP 응답을 구문 분석 할 수 있습니까? JSONP (Padding with JS) 응답을 구문 분석하는 방법은 무엇입니까?

{ 
    "Reference1":"String content", 
    "Reference2":"String content", 
    "Reference3":"String content", 
    "Reference4":"String content" 

} 

이 패딩 자체가 문제가 결코 당신에게

+4

JSON이 아닌 JSON입니다. – Quentin

+0

@Quentin,하지만이 작업은 JSONP 응답을 지원합니다. 콜백 함수는 "콜백"URL 쿼리 매개 변수를 사용하여 지정할 수 있습니다. 도움말 페이지에서 .. – wolverine

+0

- 브라우저에서 실행되는 JS를 작성하지 않으므로 JSONP가 필요하지 않으므로 선택적으로 제공 할 수있는 서비스의 기능은 적절하지 않습니다. – Quentin

답변

0

먼저 아래 코드를 사용해 된 JSONObject에 대한 HTTP 스트림에서 읽은 문자열을 변환하는 것입니다. 아래

String url="Your URL Goes Here"; 
DefaultHttpClient httpclient = new DefaultHttpClient(); 
HttpGet httpget = new HttpGet(url); 
HttpResponse response; 
try { 
    try 
    { 
     InetAddress i = InetAddress.getByName(url); 
    } catch (UnknownHostException e1) { 
     e1.printStackTrace(); 
    } 
    response = httpclient.execute(httpget); 
    HttpEntity entity = response.getEntity(); 
    if (entity != null) { 
     InputStream instream = entity.getContent(); 
     String result= convertStreamToString(instream); 
     stringContent=new StringContent(result); 
     instream.close(); 
    } 
} catch (ClientProtocolException e) { 
    e.printStackTrace(); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 

그리고 convertStreamToString 블록

입니다 : 내 HTTP 코드를 얻을 여기에 지금

String reference1=jsonobject.getString("Reference1"); 

referece1의 값 = String content

을 그리고 : 코드의 경우는 같은 것을 할 것

private static String convertStreamToString(InputStream is) 
{ 
    /* 
    * To convert the InputStream to String we use the BufferedReader.readLine() 
    * method. We iterate until the BufferedReader return null which means 
    * there's no more data to read. Each line will appended to a StringBuilder 
    * and returned as String. 
    */ 
     BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
     StringBuilder sb = new StringBuilder(); 

     String line = null; 
     try { 
      while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     try { 
      is.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
    return sb.toString(); 
} 
+0

url에 특수 문자를 전달 하시겠습니까? – wolverine

+0

내가 restclient에 URL을 보낼 때, 응답하고, 응답은 json 형식이지만, 나는 이클립스 프로그램을 통해 null 값을 보여주고있다. .. – wolverine

+0

http를 추가하면 물건이 나에게 알려준다. 문제가있다. –

0

감사합니다

여기 내 대답 모습이다. 점잖은 JSON 파싱 라이브러리가 모든 것을 능동적으로 처리합니다. 나는 개인적으로 GSON 라이브러리를 사용하도록 조언합니다. 나는 그것을 많은 프로젝트에서 사용했고 결코 불평할만한 것을 찾지 못했습니다.

추신 : 아마도 당신을 오해하지만, 구문 분석을 말할 때, 속성에 대한 값을 가져와 패딩을 유지할 필요가 없다고 가정합니다. 맞습니까?

0

나는이 tutorial이 android.Jope에서 json을 구문 분석하는 매우 자세한 내용이라고 생각합니다.이 도움이됩니다.

JSONObject jsonOBJ=new JSONObject(jsonString); 

그런 다음 각 태그를 읽을 jsonobject.getString("tag")를 사용 : 나는 많은 프로젝트를하고있다 무엇

+0

다른 곳의 답변이 실제로 "좋은 답변"입니까? ([아니, 아니야] (http://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers) – Quentin

관련 문제