简单地发出HTTP请求并接收内容 [英] Make simply HTTP request and receive the content

查看:140
本文介绍了简单地发出HTTP请求并接收内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里找到了一个简单的HTTP请求的工作代码,来自如何在MainActivity.java中创建简单的HTTP请求? (Android Studio),我将在下面发布(有一些更改,如果我没有错,现在需要使用 try {} catch {} )。但我想问一下我如何收到内容?我按以下方式处理代码:

I have found a working code for makeing simply HTTP requests here, from How can I make a simple HTTP request in MainActivity.java? (Android Studio) and I am going to post it below (with some changes, if I am not wrong it is now necessery to use try{} catch{}). But I would like to ask how I can receive the content? I work with the code in the following way:

GetUrlContentTask req = new GetUrlContentTask();
req.execute("http://192.168.1.10/?pin=OFF1");
textView3.setText(req.doInBackground("http://192.168.1.10/?pin=OFF1")); 

GetUrlContentTask

private class GetUrlContentTask extends AsyncTask<String, Integer, String> {
    protected String doInBackground(String... urls) {
        // String content1 = "";
        try {
            URL url = new URL(urls[0]);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setDoOutput(true);
            connection.connect();


            BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String content = "", line;
            while ((line = rd.readLine()) != null) {
                content += line + "\n";
            }
            // content1 = content;
        }
        catch (Exception e) {
            e.printStackTrace();
        }

        // return content1; - returns "", wrong
        return "aaa";
        //does not work    return content;
    }

    protected void onProgressUpdate(Integer... progress) {
    }

    protected void onPostExecute(String result) {
        // this is executed on the main thread after the process is over
        // update your UI here    
    }
}


推荐答案

你到底在哪里?您的代码大多是正确的,尽管您可能需要稍微重新排列它。你的范围有点偏,你的注释代码几乎到了那里。见下文

Where exactly are you stuck? Your code mostly correct although you may need to rearrange it slightly. Your scope is a bit off and your commented code almost gets there. See below

protected String doInBackground(String... urls) {
    String content = "", line = "";
    HttpURLConnection httpURLConnection;
    BufferedReader bufferedReader;
    URL url;

    try {
        for(String uri : urls) {
            url = new URL(uri);
            url = new URI(url.toURI().getScheme(), url.getAuthority(), url.getPath(), "pin=" + URLEncoder.encode("&OFF1", "UTF-8"), null).toURL();

            httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("GET");
            httpURLConnection.setDoOutput(true);
            httpURLConnection.connect();


            bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
            while ((line = bufferedReader.readLine()) != null) {
                content += line + System.lineSeparator();
            }
        }
     } catch (Exception e) {
         e.printStackTrace();
     } finally {
         try {
            bufferedReader.close();
         } catch(Exception e) {
             e.printStackTrace();
         }
     }

     return content;

}

这将返回页面的字符串表示形式。如果页面包含HTML,它将返回text / html,如果它包含文本,则只返回文本。

That will return a String representation of your page. If the page contains HTML it will return text/html, if it contains text it will return just text.

然后,如前一个用户声明的那样,您可以设置回复GUI

Then as a previous user stated you can set the response on your GUI

protected void onPostExecute(String result) {
    textView3.setText(result);
}

以上示例在技术上有效。但是,我强烈建议使用 http://loopj.com/android-async-http/ over HttpURLConnection。你的代码会更好,这就行了。

The above example will technically work. However, I would highly recommend using http://loopj.com/android-async-http/ over HttpURLConnection. Your code will be much nicer which will matter down the line.

我发给你的代码假设参数总是pin = OFF1,因为它更像是一个概念验证

The code I sent you assumes that the parameter is always pin=OFF1 because it's more a proof of concept

这篇关于简单地发出HTTP请求并接收内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆