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

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

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

.

.

.

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

.

.

我编辑的代码:

.

MainActivity.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();

    }

}

Sync.java


public class Sync extends Thread{

    private Context context;

    boolean wait = false;

    final String URL = "***";

    public CallBack listener;

    RequestQueue requestQueue;

    public Data data;


    public interface CallBack{

        void onCallBack(String data);

    }

    public Sync(CallBack listener,Data data,Context context){

        this.listener = listener;

        this.data = data;

        this.context = context;

        requestQueue = Volley.newRequestQueue(context);

    };


    @Override

    public void run() {

        while(true){

            wait = true;

            POST();

            while(wait){}

        }

    }



MM们
浏览 112回答 1
1回答

RISEBY

好吧,我可以给你一个如何实现这一点的例子,但你确实需要研究我将使用的东西。创建线程来发送和接收数据&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

相关分类

Java