Update data in a Textview automatically in Android Studio

Asked

Viewed 633 times

0

I’m building a project where I need my app (made in android studio) to pull data from a local server and update automatically without needing a "refresh" button or a "Swipe to refresh".

At the moment I am, I can pull the data through a php script and show in the application, but when I enter a new data, it does not update automatically, only if 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();
            }
        }

    }

} 

Someone could tell me a method of making an autorefresh where the data would update alone in the application every time a data was added in the database?

  • Opa friend, good night, man, with mysql is kind of complicated to do this, because it is not a db in real time, would have to be something like firebase or other type msm, with mysql what you can do is put something to make a new request every x time to check if there is a new data

  • could you tell me how to do that? I’m kind of new in this area yet

  • Dude, you can leave a running service ( What costs a lot of battery) or use a Jobscheduler: https://www.youtube.com/watch?v=m8hbskNrKZU&t=1809s or an Alarmmanager: "https://www.youtube.com/watch?v=OHRKI3B_CwY, while your app is open, and when it closes you make the services command stop

  • You can put for the "stopage" to be called in onDestroy

  • But in your case a service msm would be better because those others I spoke, take time to restart, if I’m not mistaken, the Jobscheduler msm is at least every 15 minutes

  • Service: https://www.youtube.com/watch?v=7EL1gLa4yl0&t=210s

  • I’ll check in, thank you!

  • Dude, I was looking at the video here, and I’m kind of lost... you would know how to help me implement the service in my code?

  • I already Did that

Show 5 more comments
No answers

Browser other questions tagged

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