Json returning wrong data - Android

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 contents of this json from time to time (the url does not change). The problem is when I change the data in json it does not update in the first app, I have to close and open the app, then yes, it shows the json updated. Then the second problem appears, when closing the activity and opening it again, the data displayed are those of before updating json.

Eg json1. Displayed; I upgraded to json2. You have to restart the app to show updated in the app. Displayed: json2. Closed the 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 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 was kind of confusing but I think you can understand what is happening. How to solve?

    
asked by anonymous 18.03.2017 / 18:27

0 answers