2012-05-02 3 views
0

http://www.taxmann.com/TaxmannWhatsnewService/Services.aspx?service=getStatutesTabNewsJson Parsin in android

이것은 내 웹 서비스입니다. 나는 그것을 분석하고 나는 show_id와 news title을 보여주고 싶다. 게시하여, 모든 값을 문자열에 저장할 수 있도록 파싱하는 방법을 보여주십시오. 나는 시도했지만 여전히 결과를 얻을 수없는 경우 코드 아래 사용할 수 있습니다 Exception ..

try 
    { 
     HttpClient httpclient = new DefaultHttpClient(); 

     HttpPost httppost = new HttpPost("http://www.taxmann.com/TaxmannWhatsnewService/Services.aspx?service=getStatutesTabNews"); 
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
     HttpResponse response = httpclient.execute(httppost); 
     HttpEntity entity = response.getEntity(); 
     is = entity.getContent(); 
    }catch(Exception e) 
    { 
     Log.e("log_tag", "Error in http connection"+e.toString()); 
    } 

    //convert response to string 
    try 
    { 
     BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"),8); 
     sb = new StringBuilder(); 
     sb.append(reader.readLine() + "\n"); 
     String line="0"; 
     while ((line = reader.readLine()) != null) 
     { 
      sb.append(line + "\n"); 
     } 
     is.close(); 
     result=sb.toString(); 
    }catch(Exception e) 
    { 
     Log.e("log_tag", "Error converting result "+e.toString()); 
    } 


// String name; 
    try 
    { 
     jArray = new JSONArray(result); 
     JSONObject json_data=null; 
     for(int i=0;i<jArray.length();i++) 
     { 
      HashMap<String, String> map = new HashMap<String, String>(); 
      json_data = jArray.getJSONObject(i); 

//    name=json_data.getString("name"); 
      map.put("id", String.valueOf(json_data.getString("news_id"))); 

      map.put("title",json_data.getString("news_title")); 
      map.put("shortdescription",json_data.getString("news_short_description")); 
      map.put("date",json_data.getString("news_date")); 
      mylist.add(map); 
     } 


    } 
     catch(Exception e) 
    { 
    } 
} 
+0

는 – Herry

+0

은 또한 당신이 여기 JSON 문자열을 넣을 수 있습니다 도움이 될 수 있습니다 귀하의 질문에 예외를 보여줍니다. – Herry

+0

http://www.taxmann.com/TaxmannWhatsnewService/Services.aspx?service=getStatutesTabNews –

답변

1

을 얻고있다.

static InputStream is = null; 
    static JSONObject jObj = null; 
    static JSONArray jsonArray=null; 
    static String json = ""; 



mJsonArray=getJSONFromUrl(url); 
     try{ 
     JSONObject mJsonObject=null; 
     for(int i =0;i<mJsonArray.length();i++){ 
      if(!mJsonArray.isNull(i)){ 
       HashMap<String, String> map = new HashMap<String, String>(); 
       mJsonObject=mJsonArray.getJSONObject(i); 
       map.put("title",mJsonObject.getString("news_title")); 
       map.put("shortdescription",mJsonObject.getString("news_short_description")); 
       map.put("date",mJsonObject.getString("news_date")); 
      //add you map in to list 
      } 
     } 
     }catch(JSONException jexc){ 
      jexc.printStackTrace(); 
     } 




public JSONArray getJSONFromUrl(String url) { 

      // Making HTTP request 
      try { 
       // defaultHttpClient 
       DefaultHttpClient httpClient = new DefaultHttpClient(); 
       HttpPost httpPost = new HttpPost(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(); 
      } catch (Exception e) { 
       Log.e("Buffer Error", "Error converting result " + e.toString()); 
      } 

      // try parse the string to a JSON object 
      try { 
      jsonArray =new JSONArray(json); 
      } catch (JSONException e) { 
       Log.e("JSON Parser", "Error parsing data " + e.toString()); 
      } 

      // return JSON String 
      return jsonArray; 

     } 
2

당신은 GSON 파서를 사용하여 구문 분석 할 수 있습니다. 그래서 일단 다운로드 GSON-1.1.jarhttp://findjar.com/jar/com/google/code/gson/gson/1.1/gson-1.1.jar.html

에서 파일 다음 (코드 아래로 파싱 코드를 대체 간단한) 다음 프로젝트 빌드 경로에 jar 파일을 추가 분석을 위해 아래의 코드를 사용

try 
    { 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost("http://www.taxmann.com/TaxmannWhatsnewService/Services.aspx?service=getStatutesTabNews"); 
     HttpResponse response = httpclient.execute(httppost); 
     HttpEntity entity = response.getEntity(); 
     String data = EntityUtils.toString(entity); 

     Gson gson = new Gson(); 
     Type collectionType = new TypeToken<List<NewsData>>(){}.getType(); 
     List<NewsData> details = gson.fromJson(data, collectionType); 
    } 
    catch (Exception e) 
    { 
     Log.i("error","error"); 
     e.printStackTrace(); 
    } 
위의 코드에 대한

빈은

public class NewsData 
{ 
    private String news_id = null; 
    private String news_title = null; 
    private String news_short_description = null; 
    private String news_date = null; 

    public String getNews_id() 
    { 
     return news_id; 
    } 
    public void setNews_id(String newsId) 
    { 
     news_id = newsId; 
    } 
    public String getNews_title() 
    { 
     return news_title; 
    } 
    public void setNews_title(String newsTitle) 
    { 
     news_title = newsTitle; 
    } 
    public String getNews_short_description() 
    { 
     return news_short_description; 
    } 
    public void setNews_short_description(String newsShortDescription) 
    { 
     news_short_description = newsShortDescription; 
    } 
    public String getNews_date() 
    { 
     return news_date; 
    } 
    public void setNews_date(String newsDate) 
    { 
     news_date = newsDate; 
    } 
} 

하고 매니페스트

에서 인터넷 권한을 추가
<uses-permission 
     android:name="android.permission.INTERNET" /> 

은 내가 당신을 도울 바랍니다.