2

Tengo un problema al parsear un JSON, reutilizando código fuente que tenía:

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();
}
3
  • Seria de agrado que si puedes especificar los deprecates cuales son
    – Codelaby
    el 16 jun. 2016 a las 15:35
  • @Webserveis Son HttpClient, DefaultHttpClien, httpGet y ClientProtocolException. Pero mi mayor problema ahora es hacer que funcione y poder recorer el JSON que es lo que me está fallando al llegar a String strData = jsonObject.getString("") el 16 jun. 2016 a las 15:37
  • Esta linea es incorrecta String strData = jsonObject.getString(""); tal vez necesitas definir un valor, te sugiero agregues el archivo json de respuesta con eso podemos definir que valor necesitas ahí.
    – Jorgesys
    el 16 jun. 2016 a las 16:41

1 respuesta 1

1

HttpClient, DefaultHttpClient, httpGet y ClientProtocolException son clases de Apache, tienes que cambiar tu implementación usando HttpUrlConnection

si es necesario usar las clases de Apache temporalmente, tienes que agregar dentro de tu archivo build.gradle:

android {
    ...
    ...
    useLibrary 'org.apache.http.legacy'
    ...
}

lo recomendable es cambiar a HttpUrlConnection

En cuanto a que en esta línea tienes un problema :

 String strData = jsonObject.getString(""); 

es incorrecto que tenga un string vacio, tienes que definir que valor obtener del json.

o elimina esa línea y define cual es el nombre del array:

// String strData = jsonObject.getString(""); 
  JSONArray jsonContent = new JSONArray("nombre del array");

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.