2012-07-09 126 views
3

我想從URL解析JSON然後將數據添加到數組。 我正在使用GSON庫。Android SDK:使用GSON從URL解析JSON

我的JSON格式如下:

[ 
    { 
     "img-src":"http://website.com/images/img1.png", 
     "URL":"http://google.com" 
    }, 
    { 
     "img-src":"http://website.com/images/img2.jpg", 
     "URL":"http://yahoo.com" 
    } 
] 

我要搶在一個單獨的線程上面的數據,我有以下代碼:

public class Async extends AsyncTask<String, Integer, Object>{ 

     @Override 
     protected String doInBackground(String... params) { 



      return null; 
     } 


    } 

我怎麼去抓住每一個「img-src」和「URL」值?

回答

6

使用此方法以獲取你的數據數組列表

public ArrayList<NewsItem> getNews(String url) { 
    ArrayList<NewsItem> data = new ArrayList<NewsItem>(); 

    java.lang.reflect.Type arrayListType = new TypeToken<ArrayList<NewsItem>>(){}.getType(); 
    gson = new Gson(); 

    httpClient = WebServiceUtils.getHttpClient(); 
    try { 
     HttpResponse response = httpClient.execute(new HttpGet(url)); 
     HttpEntity entity = response.getEntity(); 
     Reader reader = new InputStreamReader(entity.getContent()); 
     data = gson.fromJson(reader, arrayListType); 
    } catch (Exception e) { 
     Log.i("json array","While getting server response server generate error. "); 
    } 
    return data; 
} 

這應該是你應該如何申報您的ArrayList類型類(這裏的NewsItem)

import com.google.gson.annotations.SerializedName; 
    public class NewsItem { 

@SerializedName("title") 
public String title; 

@SerializedName("content") 
public String title_details; 

@SerializedName("date") 
public String date; 

@SerializedName("featured") 
public String imgRawUrl; 


} 

這是WebSErvice Util類。

public class WebServiceUtils { 

public static HttpClient getHttpClient(){ 
     HttpParams httpParameters = new BasicHttpParams(); 
     // Set the timeout in milliseconds until a connection is established. 
     int timeoutConnection = 50000; 
     HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); 
     // Set the default socket timeout (SO_TIMEOUT) 
     // in milliseconds which is the timeout for waiting for data. 
     int timeoutSocket = 50000; 
     HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);   
     HttpClient httpclient = new DefaultHttpClient(httpParameters);   
     return httpclient; 
    } 

} 
+0

您將在哪裏使用@SerializedName(「img-src」)和@SerializedName(「URL」)來填充您的類變量(這裏的類是NewsItem) – 2012-07-09 13:49:30

+0

我在哪裏獲得WebServiceUtils類? – user1417302 2012-07-09 14:59:53

+0

我已經編輯了你的帖子,以便你可以看看WebServiceUtil類 – 2012-07-10 04:53:07

0

這就是我使用的代碼(適合我)。

//Initialize the list 
Type listType = new TypeToken<ArrayList<YourObject>>(){}.getType(); 
//Parse 
List<YourObject> List= new Gson().fromJson(response, listType); 

YourObject應該是這樣的:

public class Category { 
    private String URL; 
    private String img-src; 

    public Category(String URL, String img-src){ 
     this.URL= URL; 
     this.img-src= img-src; 
    } 
} 

問候。

Ps:通過這個,您將獲得「YourObject」的列表。然後,你可以創建列出一個URL和其他與IMG-SRC

+0

我不知道如何獲得這些值本身雖然,例如「IMG-SRC」的價值觀應該被加入到一個列表和「URL」的價值觀應該被添加到另一個列表 – user1417302 2012-07-09 13:42:24

+0

正確的,但我不知道爲什麼我需要另一個類,是不是可能/更快地下載數據並將其添加到ArrayList? – user1417302 2012-07-09 13:51:52

+0

@ user1417302我不確定。我有同樣的問題,你和我發現沒有另一種方式這樣做。 – David 2012-07-09 13:54:44

0

在這個用戶的Guid,你可以找到很多的例子:

https://sites.google.com/site/gson/gson-user-guide

+0

我看過這個用戶指南,但不幸的是,對於初學者來說,有太多的信息 – user1417302 2012-07-09 13:47:56

+1

@ user1417302第一 - [我(代詞)](http://en.wikipedia.org/wiki/I_(代詞))。其次,你應該做你的工作。 「太多的信息」不是一個可以接受的理由。 – 2012-07-10 14:02:34