Error making GET of image url on Json Android

0

I have the following code:

    private class DownloadJsonAsyncTask extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {...}

    @Override
    protected Void doInBackground(Void... voids) {
        p = new ControllerPosts();
        arrayList = new ArrayList<>();

        // Creating service handler class instance
        cx =  new Conexao();
        String jsonStr = cx.get(url);

        Log.d("Resposta: ", "> " + jsonStr);

        if (jsonStr != null) {

            try {
                // De-serialize the JSON
                JSONObject jsonObj = new JSONObject(jsonStr);
                // Getting JSON Array node
                 posts = jsonObj.getJSONArray(TAG_TIPO);

                // looping through All Contacts
                for (int i = 0; i < posts.length(); i++) {
                    JSONObject c = posts.getJSONObject(i);

                    p.setId(Integer.parseInt(c.getString(TAG_ID)));
                    p.setTitulo(c.getString(TAG_TITULO));
                    p.setData(c.getString(TAG_DATA));
                    p.setConteudo(c.getString(TAG_CONTEUDO));
                   // p.setImagem(c.getString(TAG_IMAGEM));

                    // Aqui onde está gerando o erro
                    JSONObject img = c.getJSONObject(TAG_IMAGEM);
                 //   JSONObject thumb = img.getJSONObject("thumbnail");
                    p.setImagem(c.getString(img.getString(TAG_URL_IMAGEM)));

                    // tmp hash map for single contact
                    HashMap<String, String> post= new HashMap<>();

                    // adding each child node to HashMap key => value
                    post.put(TAG_ID, String.valueOf(p.getId()));
                    post.put(TAG_TITULO, p.getTitulo());
                    post.put(TAG_DATA, p.getData());
                    post.put(TAG_CONTEUDO, p.getConteudo());
                    post.put(TAG_URL_IMAGEM, p.getImagem());

                    // adding post to post list
                    arrayList.add(post);

                }
            } catch (JSONException e) {
                Log.e("tag", "Erro ao processar o JSON", e);
            }

        } else {
            Log.e("Get: ", "Não foi possível obter quaisquer dados do url");
        }
        return null;
    }

It will work correctly, until I need to do an image get, it shows the message in the debug:

Value link at thumbnail of type java.lang.String can not be converted to JSONObject

Can someone give me a light where I'm wrong?

Thank you

    
asked by anonymous 25.09.2016 / 16:51

1 answer

2

The problem is that you are passing a URL and trying to convert it to a JSON object. The% w / o% you must pass% w / o% must be in JSON format. In order for the URL to be converted into a valid JSON object, it should be in the following format, for example:

{
  "url": "http://localhost/teste/imagem.png"
}

In this format you will be able to generate an object that maps the url to its value.

    
25.09.2016 / 18:33