4ris
16/06/16, 17:23:42
Buenas compis, tengo un problemita al parsear un JSON, pues no tiene título y estoy volviendo a android después de iOS y tengo un caos bastante grande. Cabe destacar que estoy reutilizando algo de código que tenía (tengo un par de deprecates que tendré que arreglar).
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();
}
Se supone que al no llevar título no debería poner el jsonObject.getString?
El JSon empieza así:
[{"Title":"loquesea"....
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();
}
Se supone que al no llevar título no debería poner el jsonObject.getString?
El JSon empieza así:
[{"Title":"loquesea"....