Java Android SDK:使用GSON解析URL中的JSON

Java Android SDK:使用GSON解析URL中的JSON,java,android,json,gson,Java,Android,Json,Gson,我试图从URL解析JSON,然后将数据添加到数组中。 我正在使用GSON图书馆 My 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" } ]

我试图从URL解析JSON,然后将数据添加到数组中。 我正在使用GSON图书馆

My 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;
        }


    }
公共类异步扩展异步任务{
@凌驾
受保护的字符串doInBackground(字符串…参数){
返回null;
}
}
如何获取每个“img src”和“URL”值

这就是我使用的代码(对我来说很有效)

问候


附言:这样你就可以得到“你的对象”的列表。然后,您可以创建一个带有URL的列表,另一个带有img src的列表。在本用户指南中,您可以找到许多示例:


使用此方法获取数组列表中的数据

 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;
}
下面是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;
     }

}

但是我不知道如何获取这些值,例如“img src”值应该添加到一个列表中,“URL”值应该添加到另一个列表中。但是我不知道为什么我需要另一个类,是否可以/更快地下载数据并将其添加到ArrayList中?@user1417302我不确定。我也有同样的问题,我没有找到其他方法来解决。我看过这本用户指南,但不幸的是,其中的信息太多,无法满足用户的需求beginner@user1417302第一-。第二,你应该做好你的工作。“信息太多”不是一个可以接受的理由。我不同意@Lincoln Hawk。也许所有这些问题中有一半来自那些没有时间或耐心浏览工具文档文件,需要快速回答的人。这只是一种正常的行为,您将使用@SerializedName(“img src”)和@SerializedName(“URL”)来填充您的类变量(这里的类是NewsItem),我从哪里获得WebServiceUtils类?我已经为您编辑了这篇文章,以便您可以查看WebServiceUtil类
 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;
}
  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; 


}
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;
     }

}