Update data in a TextView automatically in Android Studio

0

I am putting together a project where I need my application (made in android studio) to pull data from a local server and update automatically without needing a refresh or swipe to refresh button.

At the moment I am, I can pull the data through a php script and show it in the application, but when I insert a new data, it does not update automatically, only if I restart the application.

My php code     

$servidor='localhost';
$banco='measurer';
$usuario='root';
$senha='';


    $conn = new mysqli($servidor, $usuario, $senha, $banco);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 

    $sql = "SELECT * FROM medidas ORDER BY id DESC LIMIT 1";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {

        // output data of each row
        while($row[] = $result->fetch_assoc()) {

           $json = json_encode($row);


        }
    } else {
        echo "0 results";
    }
    echo $json;
    $conn->close();
    ?>

The java of my activity in android studio

package com.example.lalam_000.introsliderexemple;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import cz.msebera.android.httpclient.HttpResponse;
import cz.msebera.android.httpclient.client.ClientProtocolException;
import cz.msebera.android.httpclient.client.HttpClient;
import cz.msebera.android.httpclient.client.methods.HttpPost;
import cz.msebera.android.httpclient.impl.client.DefaultHttpClient;
import cz.msebera.android.httpclient.util.EntityUtils;

public class Tab1Fragment extends Fragment {
    private static final String TAG = "Tab1Fragment";

    TextView textView;
    JSONObject json = null;
    String str = "";
    HttpResponse response;
    Context context;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.tab1_fragment, container, false);

    textView = (TextView) view.findViewById(R.id.valor_vazao);

    new GetTextViewData(context).execute();

    return view;
    }

    private class GetTextViewData extends AsyncTask<Void, Void, Void> {
    public Context context;


    public GetTextViewData(Context context) {
        this.context = context;
    }

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

    @Override
    protected Void doInBackground(Void... arg0) {

        HttpClient myClient = new DefaultHttpClient();
        HttpPost myConnection = new 
 HttpPost("http://192.168.0.101/teste.php");

        try {
            response = myClient.execute(myConnection);
            str = EntityUtils.toString(response.getEntity(), "UTF-8");

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            JSONArray jArray = new JSONArray(str);
            json = jArray.getJSONObject(0);


        } catch (JSONException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    protected void onPostExecute(Void result) {

        try {
            textView.setText(json.getString("vazao"));

        } catch (JSONException e) {
            e.printStackTrace();
            }
        }

    }

} 

Could anyone tell me a method of doing an autorefresh where data would update itself in the application every time a data was added to the database?

    
asked by anonymous 22.05.2018 / 00:42

0 answers