Taking a quick look at some of my more recent code, I actually use a combination of DefaultHttpClient and HttpGet. See:

https://developer.android.com/reference/org/apache/http/impl/client/DefaultHttpClient.html

https://developer.android.com/reference/org/apache/http/client/methods/HttpGet.html


This is an example to use a GET request to receive data, but that doesn't mean this is a perfect example:

        byte[] data = null;
        DefaultHttpClient client = new DefaultHttpClient();
        HttpGet gReq = new HttpGet("http://www.whatever.url";);
        gReq.setHeader("User-Agent", "My App");
        try {
            HttpResponse a = client.execute(gReq);
            if (a.getStatusLine().getStatusCode() == 200) {
                HttpEntity entity = a.getEntity();
                long contentLength = entity.getContentLength();
                BufferedInputStream bis = new BufferedInputStream(
                        entity.getContent());
                ByteArrayOutputStream out = new ByteArrayOutputStream(
                        (int) contentLength);
                long bytesReceived = 0;
                int currentReceived = 0;
                byte[] buf = new byte[10000];

                do {
                    currentReceived = bis.read(buf, 0, 10000);
                    bytesReceived += currentReceived;
                    out.write(buf, 0, currentReceived);
                } while (bytesReceived < contentLength);
                data = out.toByteArray();
                out.close();
                out = null;
                entity = null;
                bis.close();
                bis = null;
            }
        } catch (ClientProtocolException e) {
            Debug.showStackTrace(e);
        } catch (IOException e) {
            Debug.showStackTrace(e);
        }

At the end of this, you have a byte array in data that can be converted to a String and manipulated that way, or you can plug it into other objects and use it there. For example, if you convert it to a string, you can output the received data to android.util.Log so you can see it in your development environment or write it to a log file rather than building a WebView. If you use that code in a function, there's no reason you couldn't pass in a URL and call the same code as many times as you want or need, whereas I think WebView caches the results of requests, but I've never used it personally.

Hope that helps.

On 11/04/2016 04:14 AM, [email protected] wrote:
Could you give me a direction on how to use HTTP connection? I'm pretty new to Andoid programming

Op dinsdag 1 november 2016 16:27:20 UTC+1 schreef Raymond Rodgers:

    I'm not familiar with the software you're using (on the server
    side), but if the WebView is going to be invisible, why are you
    using it? Does the triggering web page/API use some Javascript or
    something to accomplish the task? If not, I would use a different
    method. Forgive me, it's been a while since I've done any active
    coding on Android, but I'm pretty sure there are HTTPConnection
    classes or something to that effect that take a Url object (or
    string) that can make the requests much more efficiently if all
    you need to do is send and receive some data.


    On 11/01/2016 08:36 AM, [email protected] <javascript:> wrote:
    *KICK*

    I need help

    Op woensdag 26 oktober 2016 08:58:33 UTC+2 schreef
    [email protected]:

        Hello,

        I wrote an (really basic) application to control access for a
        building. This application exists of multiple buttons (10 in
        total).
        Per door there are 2 buttons. One button acts as an pulse
        buton. When clicking the pulse button a timer function will
        be enabled and after 5 seconds the function will be disabled.
        The other button enables or disables the function manualy
        (without a timer).

        When the function is enabled a URL request is send out to
        load within a WebView. The URL that is requested is an URL
        trigger for a webrelay which will trgger the door to unlock.
        When the function is disabled a URL request is send out to
        load within a WebView. The URL that is requested is an URL
        trigger for a webrelay which will trgger the door to lock.
        For testing the URL request i used URL's like:
        http://www.cisco.com
        http://www, nu.nl <http://nu.nl>

        The URL's requested will appear in a Webview (which will be
        invisible in the final version).
        Each door has it's own door WebView.

        So for short:
        One door has:
        2 buttons;
        1 timer function;
        1 WebView.

        There are 5 doors in total.

        Now the problem coms that after running this error will occure:
        A/libc: Fatal signal 11 (SIGSEGV) at 0x0000001c (code=1),
        thread 25881 (WebViewCoreThre)

        After searching on the internet, i learned this could be a
        memory issue.
        But i don't have a clue to how address this, or what the
        problem could be. I tried to eleminate the images that i
        used, but that didn't work.

        The application is develloped for Android 4.1 (Jellybean).

        How can i resolve this problem ??

        Regards.

        Dennie

-- You received this message because you are subscribed to the
    Google Groups "Android Developers" group.
    To unsubscribe from this group and stop receiving emails from it,
    send an email to [email protected]
    <javascript:>.
    To post to this group, send email to
    [email protected] <javascript:>.
    Visit this group at
    https://groups.google.com/group/android-developers
    <https://groups.google.com/group/android-developers>.
    To view this discussion on the web visit
    
https://groups.google.com/d/msgid/android-developers/4a4616a3-fa73-4cd5-af80-d8c99baf6e97%40googlegroups.com
    
<https://groups.google.com/d/msgid/android-developers/4a4616a3-fa73-4cd5-af80-d8c99baf6e97%40googlegroups.com?utm_medium=email&utm_source=footer>.
    For more options, visit https://groups.google.com/d/optout
    <https://groups.google.com/d/optout>.

--
You received this message because you are subscribed to the Google Groups "Android Developers" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected] <mailto:[email protected]>. To post to this group, send email to [email protected] <mailto:[email protected]>.
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/android-developers/913efec0-b551-49f2-bd61-68a72ba69c0c%40googlegroups.com <https://groups.google.com/d/msgid/android-developers/913efec0-b551-49f2-bd61-68a72ba69c0c%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Android 
Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/android-developers/776a280d-24cc-0632-e2c7-5e13a73cd7d8%40raymondrodgers.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to