Json returning wrong data - Android

Asked

Viewed 54 times

1

I have an Activity that displays the data of a json coming from a url. The data is displayed, but I need to change the content of this json from time to time (the url does not change). The problem is when I change the data in json it doesn’t update in the first app, I have to close and open the app, then yes, it shows the updated json. Then the second problem appears, when closing Activity and opening it again, the data displayed are the data of before updating the json.

Ex.: json1. Displayed; Updated to json2. You have to restart the app to show updated in the app. Displayed: json2. Closed Activity, opened again, displays json1, even if json2 is in the url.

The Code:

 protected void onCreate(Bundle savedInstanceState) {
    //outros códigos
    new JsonTask().execute("http://pastebin.com/raw/u5hFAx4N");

To recover the json:

 private class JsonTask extends AsyncTask<String, String, String> {


    protected void onPreExecute() {
        super.onPreExecute();
    }

    protected String doInBackground(String... params) {

        HttpURLConnection connection = null;
        BufferedReader reader = null;

        try {
            URL url = new URL(params[0]);
            connection = (HttpURLConnection) url.openConnection();
            connection.connect();


            InputStream stream = connection.getInputStream();

            reader = new BufferedReader(new InputStreamReader(stream));

            StringBuffer buffer = new StringBuffer();
            String line = "";

            while ((line = reader.readLine()) != null) {
                buffer.append(line+"\n");
                Log.d("Response: ", "> " + line);
            }

            return buffer.toString();


        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    protected void onPostExecute(String resultado) {
        super.onPostExecute(resultado);

        String titulo = "";
        String imagem = "";
        String texto = "";

        try {
            JSONObject jsonObject = new JSONObject(resultado);
            JSONArray jsonArray = jsonObject.getJSONArray("item");

            JSONObject jsonArrayJSONObject = jsonArray.getJSONObject(0);
            titulo = jsonArrayJSONObject.getString("titulo");
            imagem = jsonArrayJSONObject.getString("imagem");
            texto = jsonArrayJSONObject.getString("texto");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        txtTitulo.setText(titulo);
        txtTexto.setText(texto);
        Picasso.with(getApplicationContext())
                .load(imagem)
                .placeholder(R.drawable.progress_animation)
                .error(R.drawable.erro)
                .into(imgImagem);

    }
}

It got a little confusing, but I think I figured out what’s going on. How to solve?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.