如何重复发布和接收数据但使应用程序仍可响应已编辑

我需要重复将数据发布到 php 并取回它们,但使应用程序仍然可以响应。我的代码有效,但一段时间(大约 1 分钟)后,我的应用程序停止响应。


起初,我尝试将 POST() 函数添加到 onResponse() 函数(onRespose 调用 POST 函数,它一次又一次地发出新请求)但是这冻结了我的应用程序,因此我添加了每 1 毫秒调用一次函数的计时器


.


.


.


编辑后,我的应用程序与以前一样工作(仅在连接到 PC 时才有效),如果我打开应用程序并且我的手机没有连接到 PC,应用程序在 1 分钟后下降,所以我仍然遇到与开始时相同的问题。如果有人发现错误,请告诉我在哪里。.


.


.


我编辑的代码:


.


主活动.java


public class MainActivity extends AppCompatActivity implements Sync.CallBack {


    final String URL = "***";

    final String KEY = "***";

    Data data;

    RequestQueue requestQueue;

    TextView textView;


    @Override

    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        data = new Data();

        data.key = KEY;

        data.pressed = "0";

        textView = findViewById(R.id.status);

        requestQueue = Volley.newRequestQueue(this);

        StartSync();


    }


    public void btnClick(View view) {

        data.pressed = "1";


    }



    @Override

    public void onCallBack(String data) {

        textView.setText(data);

    }

    public void StartSync(){

        Sync thread = new Sync(this,data,this);

        thread.start();

    }

}


慕侠2389804
浏览 149回答 1
1回答

慕码人8056858

好吧,我可以给你一个如何实现的例子,但你真的需要研究我将使用的东西。创建一个线程来发送和接收数据&nbsp;public class myReceiveThread extends Thread{&nbsp; &nbsp; &nbsp; &nbsp; public gotNewData listener;&nbsp; &nbsp; &nbsp; &nbsp; //Create an interface to pass data to Activity&nbsp; &nbsp; &nbsp; &nbsp; public interface gotNewData(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; void gotNewDataToDisplay(String data); //or int or data or what ever.&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //CTor&nbsp; &nbsp; &nbsp; &nbsp; public myThread(gotNewData listener){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.listener = listener;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void run(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while(myAppisRunnung == true){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Receive Data&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; listener.gotNewDataToDisplay("New Data");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }在主要活动中://In Main activitypublic class MainActivity extends AppCompactActiity implements gotNewData{//what everpublic void startThread(){&nbsp; &nbsp; &nbsp;myReceiveThread thread = new myReceiveThread(this);&nbsp; &nbsp; &nbsp;thread.start();}@Overridepublic void gotNewDataToDisplay(String data){&nbsp; &nbsp; someTextView.setText(data);&nbsp; }}创建发送线程public class mySendingThread extends Thread{private BlockingQueue<String> toSendMessages= new BlockingQueue<>();Public mySendingThread (BlockingQueue<String> toSendMessages){this.toSendMessages = toSendMessages;}@Override&nbsp; &nbsp; &nbsp; &nbsp; public void run(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while(myAppisRunnung == true){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;String message= toSendMessages.take();//Send message}}}在主活动中public void startSendThread(){&nbsp; &nbsp; &nbsp;mySendingThread threadSend = new mySendingThread(MessageQueue);&nbsp; &nbsp; &nbsp;thread.start();}在 mainActivity 中,您需要一个BlockingQueue<String> MessageQueue 并且可以添加您想要的每条消息。
打开App,查看更多内容
随时随地看视频慕课网APP