2012-07-02 98 views
0

這是我的JSON:爲什麼null值在android中的json解析中返回?

{ 

    "feed": [ 
     { 
      "id": "124124124124", 
      "source": "aaaaaa", 
      "comment_count": 0, 
      "link": "bbbbb", 
      "created_time": "2012-05-30T15:57:05+0000", 
      "message": "ccccc", 
      "type": "dddd", 
      "thumbnail": "eeeee" 
     }, 
     { 
      "id": "35464423423412414", 
      "source": "qweqddq", 
      "comment_count": 0, 
      "link": "adadasdwe", 
      "created_time": "2012-05-30T15:52:21+0000", 
      "message": "dadsadad", 
      "type": "sfdcsdv", 
      "thumbnail": "csdgfsd" 
     }, 
     { 

    "name": "asdadqwff", 
    "id": "73182372381", 
    "source": "lajhdkbad", 
    "comment_count": 0, 
    "link": "adjkbxczckbj", 
    "created_time": "2012-05-30T15:40:28+0000", 
    "message": "awjasdjands", 
    "type": "lalkdm", 
    "thumbnail": "akmldsncj" 

    } 

    ] 

    } 

這是我的Java類解析使用:

public class Feed { 

    @SerializedName("feed") 
    ArrayList<FeedResult> feed; 

    public ArrayList<FeedResult> getFeed() { 
     return feed; 
    } 

    public void setFeed(ArrayList<FeedResult> feed) { 
     this.feed = feed; 
    } 

} 


public class FeedResult { 

    @SerializedName("id") 
    String id; 

    @SerializedName("created_time") 
    String created_time; 

    @SerializedName("message") 
    String message; 

    @SerializedName("thumbnail") 
    String thumbnail; 

    @SerializedName("comment_count") 
    String comment_count; 


    public String getId() { 
     return id; 
    } 

    public void setId(String id) { 
     this.id = id; 
    } 

    public String getCreated_time() { 
     return created_time; 
    } 

    public void setCreated_time(String created_time) { 
     this.created_time = created_time; 
    } 

    public String getMessage() { 
     return message; 
    } 

    public void setMessage(String message) { 
     this.message = message; 
    } 

    public String getThumbnail() { 
     return thumbnail; 
    } 

    public void setThumbnail(String thumbnail) { 
     this.thumbnail = thumbnail; 
    } 

    public String getComment_count() { 
     return comment_count; 
    } 

    public void setComment_count(String comment_count) { 
     this.comment_count = comment_count; 
    } 


} 

這是主類的內部:

InputStream source = retrieveStream(url); 

      Gson gson=new Gson(); 

      Reader reader = new InputStreamReader(source); 

      Feed feeds=gson.fromJson(reader,Feed.class); 

      ArrayList<FeedResult>feed=new ArrayList<FeedResult>(); 

      feed=feeds.getFeed(); 

      Toast.makeText(this,"feed --> ?? "+ feed, Toast.LENGTH_LONG).show(); 

feeds.getFeed()返回null。這是爲什麼?感謝您的幫助。

回答

0

,而不是直接給予的InputStream到GSON構造,從URL首先下載JSON字符串,然後把它傳遞到構造函數如下解析:

String source = read(url); 
Feed feeds=new Gson().fromJson(source ,Feed.class); 
ArrayList<FeedResult>feed=new ArrayList<FeedResult>(); 
feed=feeds.getFeed(); 
for(FeedResult feedResult:feedResults) 
    System.out.println(feedResult); 

讀功能:

public static String read(String url){ 

    System.out.println("Connecting to service URL : "+url); 
    InputStream is = null; 
    String result = ""; 
    // http post 
    try { 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost(url); 
     HttpResponse response = httpclient.execute(httppost); 
     HttpEntity entity = response.getEntity(); 
     is = entity.getContent(); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    // convert response to string 
    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(); 
     result = sb.toString(); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 


    return result; 
} 

在FeedResult類中添加toString()函數,以便以可讀方式將其打印到控制檯:

/* (non-Javadoc) 
* @see java.lang.Object#toString() 
*/ 
@Override 
public String toString() { 
    return "FeedResult [id=" + id + ", created_time=" + created_time 
      + ", message=" + message + ", thumbnail=" + thumbnail 
      + ", comment_count=" + comment_count + "]"; 
} 
+0

我試過並得到這個錯誤 - > com.google.gson.JsonParseException:期待找到對象:「<!DOCTYPE」 – selenk

+0

@S.Kartoğlu發生這種情況是因爲您的服務返回非JSON響應。所以我建議你檢查服務是否正常工作。或者可能是您將不正確的參數傳遞給該服務 – sunil