I have a problem parsing a JSON, reusing source code I had:
protected Boolean doInBackground(final String...args) {
String datos = leeJSon(this.ruta);
Log.d("1", datos);
try {
JSONObject jsonObject = new JSONObject(datos);
String strData = jsonObject.getString(""); //Puede que haya que quitar esta línea
JSONArray jsonContent = new JSONArray(strData);
int numItems = jsonContent.length();
MainActivity.hacks = new Hack[numItems];
for (int i = 0; i < numItems; i++) {
JSONObject itemJson = jsonContent.getJSONObject(i);
Hack hack = new Hack();
hack.setTitle(itemJson.getString("Title"));
hack.setBreachDate(itemJson.getString("BreachDate"));
hack.setDescription(itemJson.getString("Description"));
MainActivity.hacks[i] = hack;
}
} catch(Exception e) {
e.printStackTrace();
}
return true;
}
private String leeJSon(String url) {
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
}
} catch(ClientProtocolException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}
return builder.toString();
}