每2秒向网页请求一页网页

问题描述 投票:0回答:1

我想每2秒向网页发送一次请求。我在Google上找到了关于此的教程,并尝试编写示例代码,但不幸的是它不起作用....

[当我尝试发出一个不带循环的简单请求时,它可以完美地工作,但是当我尝试与循环时,它已经写入了错误...。

这是我尝试运行的代码:

 public void run1(){
        try {
            while(true) {

                Log.d(TAG, "test");
                RequestQueue queue = Volley.newRequestQueue(this);
                String url = "https://www.excursii-baile-felix.ro/rezervari/api_sms/sms.php?up=ready";

                StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
                        new Response.Listener<String>() {
                            @Override
                            public void onResponse(String response) {
                                Log.d(TAG, "=============response=============");
                                //check response its is ok
                                if(response.length() > 0){
                                    textView.setText( response);
                                }else{
                                    Log.d(TAG, "NU am mesaj de trimis!");
                                }

                            }
                        }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        textView.setText("IMI PARE RAU CEVA NU A MERS!!");
                    }
                });

                queue.add(stringRequest);
                Thread.sleep(2000);                 //1000 milliseconds is one second.
            }
        } catch(InterruptedException ex) {
            Thread.currentThread().interrupt();
        }
    }



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        number = "0770162059";
        message = "test";
        send = findViewById(R.id.send);
        final TextView textView = (TextView) findViewById(R.id.text);



        send.setEnabled(false);
        if(checkPermission(Manifest.permission.SEND_SMS)){
            send.setEnabled(true);
        }else{
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.SEND_SMS}, SEND_SMS_PERMISSION_REQUEST_CODE);

        }

        if(checkPermission(Manifest.permission.INTERNET)){
            Log.d(TAG, "======Internet permission ready=======");
        }else{
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.INTERNET}, INTERNET_PERMISSION_REQUEST_CODE);

        }


        run1();


    }

而且我认为在LogCat中显示的错误是:

02-02 11:09:26.763 6563-6563 / com.example.send_sm_test D / MainActivity:测试02-02 11:09:26.763 6563-6590 / com.example.send_sm_test I / qtaguid:使用uid -1的标签56062fc900000000(-704237623)标记插座49失败的errno = -2 02-02 11:09:26.763 6563-6590 / com.example.send_sm_testI / NetworkManagementSocketTagger:tagSocketFd(49,-704237623,-1)因errno-2失败02-02 11:09:26.7636563-6590 / com.example.send_sm_test I / qtaguid:使用以下标签标记套接字50uid -1的标签56062fc900000000(-704237623)失败errno = -2 02-0211:09:26.763 6563-6590 / com.example.send_sm_testI / NetworkManagementSocketTagger:tagSocketFd(50,-704237623,-1)失败并显示errno-2 02-02 11:09:26.7936563-6567 / com.example.send_sm_test D / dalvikvm:已释放GC_CONCURRENT361K,免费提供4%18869K / 19463K,暂停1ms + 1ms 02-02 11:09:26.8336563-6590 / com.example.send_sm_test I / qtaguid:取消标记套接字50失败的errno = -2 02-02 11:09:26.833 6563-6590 / com.example.send_sm_testW / NetworkManagementSocketTagger:untagSocket(50)失败,出现errno -2

我想每2秒向网页发送一次请求。我在google上找到了有关此的教程,但我尝试编写示例代码,但不幸的是它不起作用....当我尝试制作简单的代码时...

android loops android-volley
1个回答
0
投票

更新:

© www.soinside.com 2019 - 2024. All rights reserved.