Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7195355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:33:47+00:00 2026-05-28T20:33:47+00:00

i hope you can help me with my problem. I have to instantiate an

  • 0

i hope you can help me with my problem. I have to instantiate an http connection and do a get call to the server.
After this call i have to check periodically if the server sent me some data (the connection must not be closed after the first data received). If it does, then i have to parse this data and pass it to an activity and wait again.

My problem is to understand if i’m doing correctly this thing. Here my code

try {
        URL url = getUrl();
        URLConnection urlConn = url.openConnection();
        HttpURLConnection httpConnection = (HttpURLConnection) urlConn;
        httpConnection.setRequestMethod("GET");
        httpConnection.connect();

        while(true) {
            sleep(5000);
            int responseCode = httpConnection.getResponseCode();

            if(responseCode == HttpURLConnection.HTTP_OK) {
                InputStream inputStream = httpConnection.getInputStream();
                String response = null;

                if(inputStream.available() > 0) {
                    long length = inputStream.available();
                    byte[] bytes = new byte[(int) length];
                    inputStream.read(bytes);
                    response = new String(bytes, "UTF-8");
                }
            }
        }                   
    } catch(Exception e) {
        e.printStackTrace();
    }

getUrl() function gives me the complete url that i have to call.
Then i connect through httpConnection.connect();.

while i’m iterating is the connection already opend and capable of receiving incoming data?

I apologize for my english. Thanks a lot

Francesco

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T20:33:48+00:00Added an answer on May 28, 2026 at 8:33 pm

    From the official android documentation

    Uses of this class follow a pattern:

    1. Obtain a new HttpURLConnection by calling URL.openConnection() and
      casting the result to HttpURLConnection.

    2. Prepare the request. The
      primary property of a request is its URI. Request headers may also
      include metadata such as credentials, preferred content types, and
      session cookies.

    3. Optionally upload a request body. Instances must be
      configured with setDoOutput(true) if they include a request body.
      Transmit data by writing to the stream returned by getOutputStream().

    4. Read the response. Response headers typically include metadata such as
      the response body’s content type and length, modified dates and
      session cookies. The response body may be read from the stream
      returned by getInputStream(). If the response has no body, that method
      returns an empty stream.

    5. Disconnect. Once the response body has been
      read, the HttpURLConnection should be closed by calling disconnect().
      Disconnecting releases the resources held by a connection so they may
      be closed or reused.

    As you see from this quote, if you need to send some data to a host you just need to call setDoOutput(true) and use OutputStream of your connection to send data to the host. In this case you use "POST" request method after you called setDoOutput(true). If you don’t need to send data, but just to connect to the host and retrieve data, you also don’t need to call setRequestMethod("GET"), because it’s default request method.

    When you connect to the host, just use InputStream to get data from it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I hope some of you guys can help me with this problem.... I have
i have a problem with jquery/visible. Hope somebody can help me. This code works,
I really hope someone can help with this problem. I have an ajax pagination
I have a problem with a UIView, hope you can help me. I have
Hope someone can help me out with this problem I am having. I just
Hope someone can help i cant seem to get my head around this, i
I hope someone can help me. My Problem: I have 2 sortable UL's. <ul
I have a problem using Digester and I hope you can help me. I
HI everyone I hope you can help me, I have a problem with a
I have a problem with programming jQuery and I hope that you can help

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.