开发者

Accessing the web WITHOUT a webview

开发者 https://www.devze.com 2023-03-14 13:15 出处:网络
I\'d like to know if there\'s any way to access a web page from an Android phone without using a WebView component. I\'ve been following up some tutorials on how to program for Android and the ones th

I'd like to know if there's any way to access a web page from an Android phone without using a WebView component. I've been following up some tutorials on how to program for Android and the ones that mention web access always use this component to load a URL, and then it shows the resulting page on the screen. However, I'm developing an application that will need to pass some data to a website (id, name, points,开发者_如何转开发 etc.) and I don't want to show the response on the screen - I'll still want to record the response on a log file, I just don't want to show the page onscreen. Is there a class or method that allows this to be done? If not, is there a way to hide this component from the screen somehow? I tried to tag it as 'invisible' in main.xml but with no success.

Thanks in advance for your help!


Your looking for HttpGet,HttpResponse, httpRequest and DefaultHttpClient objects that allow you to send web requests and get back the result to use with web services.

See : http://developer.android.com/reference/org/apache/http/HttpRequest.html

protected InputStream callSynchronousUrl(final String url) {

    try {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        httpClient.getParams().setParameter("http.socket.timeout",
                3000);

        URI uri = new URI(url);
        HttpGet requestObject = new HttpGet();
        requestObject.setURI(uri);

        HttpResponse response = httpClient.execute(requestObject);

        if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
            return(null);
        } 
        else {  
            //We successfully got a response from the server
            return(response.getEntity().getContent());
        }

    } catch (IOException e) {
        Log.e("CallUrl", "I/O error");
        return(null);

    } catch (URISyntaxException e) {
        Log.e("CallUrl", "URI syntax is invalid");
        return(null);
    } 
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号