Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

179
Views
Send data with URL get in android in while loop

I want to send a and b with get URL to example.com when I run it, it work one time.

I want send a and b during while condition is true. I use sleep so send data after 5 second continuously.

public class MapsActivity extends FragmentActivity 
      implements OnMapReadyCallback {
public a=0,b=0;
public btn=2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        connect();
    } //end of onCreate
     public void connect() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                communication();
            }
        }).start();
    }
    public void communication() {

        while(btn >0){

            try {
                btn++;
                a++;
                b++;
                URL url = new URL("example.com"+ "?a=" +a+"&b="+b);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.connect();
                InputStream inputStream = conn.getInputStream();
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                String data = "", line = "";
                while ((line = bufferedReader.readLine()) != null) {
                    data += line + "\n";
                }

                sleep(5000);

            } catch (MalformedURLException e) {
                Log.e("error", e.toString());
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }//end of while   
    } //end of communication
    }//end of class
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

I think Timer is better than sleep. You can try this:

Handler handler = new Handler();

    Runnable sendRequest = new Runnable() {
        public void run() {
            // your code to send request 
        }
    };


    new Timer().schedule(new TimerTask() {

        @Override
        public void run() {
            handler.post(sendRequest);
        }
    }, 100, 5000);
over 4 years ago · Santiago Trujillo Report

0

I will recommend not to do it in the Activity class .

Create a Back ground service class which runs every n seconds , in your case lets say 5 .

Now create a Queue . In this Queue push the data , whenever the condition is true .

Once you background service starts running it will read from the queue and make the api calls with the data

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!