我用這個
JSONParser parser = new JSONParser();
boolean downloaded = parser.downloadJSON(url, JSONParser.HTTP_GET);
if (!downloaded) throw new Exception("Oops");
JSONObject root = parser.getRoot();
JSONArray list = parser.getList(root, "list");
if (list != null)
{
for (int i = 0; i < list.length(); i++)
{
JSONObject jo = list.getJSONObject(i);
if (jo == null) continue;
// let's go...
}
}
這。 ...
public class JSONParser
{
public static int HTTP_GET = 0;
public static int HTTP_POST = 1;
private String json;
// constructor
public JSONParser()
{
json = null;
}
/**
* Scarica il documento JSON e lo rende disponibile ai vari metodi sempre
* disponibili in questa classe
*
* @param url indirizzo url da cui scaricare il documento JSON
* @param type tipo di richiesta GET o HTTP
* @return indica se è stato scaricato, altrimenti lancia eccezione
*/
public boolean downloadJSON(String url, int type) throws Exception
{
try
{
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpUriRequest httpRequest = type == HTTP_POST ? new HttpPost(url) : new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpRequest);
HttpEntity httpEntity = httpResponse.getEntity();
json = EntityUtils.toString(httpEntity);
return true;
}
catch (UnsupportedEncodingException ex)
{
WLogger.error("XMLParser", "UnsupportedEncodingException downloadJSON", ex);
throw ex;
}
catch (ClientProtocolException ex)
{
WLogger.error("XMLParser", "ClientProtocolException downloadJSON", ex);
throw ex;
}
catch (IOException ex)
{
WLogger.error("XMLParser", "IOException downloadJSON", ex);
throw ex;
}
// WHttpRequestAsyncTask task = new WHttpRequestAsyncTask();
// task.setType(type);
// task.execute(url);
// json = task.getResult();
// return json != null;
}
public void setJsonData(String json)
{
this.json = json;
}
public JSONObject getRoot()
{
try
{
return new JSONObject(json);
}
catch (JSONException ex)
{
WLogger.error("JSONParser", "getRoot error", ex);
return null;
}
}
public JSONArray getList(JSONObject object, String name)
{
try
{
if (object == null || Utility.isNullOrEmpty(name) || object.isNull(name))
return null;
return object.getJSONArray(name);
}
catch (Exception ex)
{
WLogger.error("JSONParser", "getList error", ex);
return null;
}
}
public JSONObject getObject(JSONObject object, String name)
{
try
{
if (object == null || object.isNull(name)) return null;
return object.getJSONObject(name);
}
catch (Exception ex)
{
WLogger.error("JSONParser", "getObject error", ex);
return null;
}
}
public String getString(JSONObject object, String name)
{
try
{
if (object == null || object.isNull(name)) return null;
return object.getString(name);
}
catch (Exception ex)
{
WLogger.error("JSONParser", "getObject error", ex);
return null;
}
}
public Integer getInt(JSONObject object, String name)
{
try
{
if (object == null || object.isNull("id")) return null;
return object.getInt("id");
}
catch (Exception ex)
{
WLogger.error("JSONParser", "getObject error", ex);
return null;
}
}
public Double getDouble(JSONObject object, String name)
{
try
{
if (object == null || object.isNull(name)) return null;
return object.getDouble(name);
}
catch (Exception ex)
{
WLogger.error("JSONParser", "getObject error", ex);
return null;
}
}
public Long getLong(JSONObject object, String name)
{
try
{
if (object == null || object.isNull(name)) return null;
return object.getLong(name);
}
catch (Exception ex)
{
WLogger.error("JSONParser", "getObject error", ex);
return null;
}
}
public Boolean getBoolean(JSONObject object, String name)
{
try
{
if (object == null || object.isNull(name)) return null;
return object.getBoolean(name);
}
catch (Exception ex)
{
WLogger.error("JSONParser", "getObject error", ex);
return null;
}
}
}
哎呀對不起,這是一個錯誤,我改名爲項目,使我的觀點更加明確 –
item.optString將保持應用程序崩潰,如果這是有史以來空。只需檢查空字符串。 –