FORUMS
Remove All Ads from XDA

Request the web page one page every 2 seconds Volley Android

1 posts
Thanks Meter: 0
 
By all3ss1o, Junior Member on 2nd February 2020, 02:29 PM
Post Reply Email Thread
I want to make a request to a web page every 2 seconds. I found a tutorial on google about this and I tried to make an example code but unfortunately it doesn't work ....

When I try to make a simple request without loop then it works perfectly but when I try with loop then it already writes error ....

Here is the code I try to run:

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

Log.d(TAG, "test");
RequestQueue queue = Volley.newRequestQueue(this);
String url = "xxxx";

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


}
And the error of what I think it shows in LogCat is:

02-02 11:09:26.763 6563-6563/com.example.send_sm_test D/MainActivity: test 02-02 11:09:26.763 6563-6590/com.example.send_sm_test I/qtaguid: Tagging socket 49 with tag 56062fc900000000(-704237623) for uid -1 failed errno=-2 02-02 11:09:26.763 6563-6590/com.example.send_sm_test I/NetworkManagementSocketTagger: tagSocketFd(49, -704237623, -1) failed with errno-2 02-02 11:09:26.763 6563-6590/com.example.send_sm_test I/qtaguid: Tagging socket 50 with tag 56062fc900000000(-704237623) for uid -1 failed errno=-2 02-02 11:09:26.763 6563-6590/com.example.send_sm_test I/NetworkManagementSocketTagger: tagSocketFd(50, -704237623, -1) failed with errno-2 02-02 11:09:26.793 6563-6567/com.example.send_sm_test D/dalvikvm: GC_CONCURRENT freed 361K, 4% free 18869K/19463K, paused 1ms+1ms 02-02 11:09:26.833 6563-6590/com.example.send_sm_test I/qtaguid: Untagging socket 50 failed errno=-2 02-02 11:09:26.833 6563-6590/com.example.send_sm_test W/NetworkManagementSocketTagger: untagSocket(50) failed with errno -2


UPDATE:

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

                Log.d(TAG, "test");
                RequestQueue queue = Volley.newRequestQueue(this);
                String url = "xxx";

                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!!");
                    }
                });
                stringRequest.setTag(RequestTAG);
                queue.add(stringRequest);

                if(queue != null){
                    queue.cancelAll(RequestTAG);
                }

                Thread.sleep(9000);                 //1000 milliseconds is one second.
            }
        } catch(InterruptedException ex) {
            Thread.currentThread().interrupt();
        }
    }
I introduced TAG to close each previous connection but it seems that this solved only the errors that were created but did not solve my request to the site. Next, the request submission does not work, but it does not cause any error ...
Post Reply Subscribe to Thread

Guest Quick Reply (no urls or BBcode)
Message:
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes