简体   繁体   English

使用InputStream从网页获取数据?

[英]Getting Data from a WebPage using InputStream?

I've read through a lot of instructionals and tried really hard but somehow, still can't manage to get my InputStream working right. 我已经阅读了许多说明,并做了非常努力的尝试,但是仍然无法使我的InputStream正常工作。 So I'm turning to you. 所以我转向你。 Thank you in advance for your help! 预先感谢您的帮助!

My objective is to get data from a blank HTML webpage and display it on a TextView in my android app. 我的目标是从空白的HTML网页获取数据,并将其显示在我的android应用中的TextView上。 I have included all internet permissions in my manifest and my XML file is just a textview in a linear layout. 我已在清单中包含所有Internet权限,并且我的XML文件只是线性布局中的textview。 The app only has one class called "scorereader" and that's about it. 该应用程序只有一个称为“ scorereader”的类,仅此而已。 When I run it, it just shows the blank XML with the textview. 当我运行它时,它只显示带有textview的空白XML。 It does not display the text from the HttpClient. 它不显示来自HttpClient的文本。

JAVA: scorereader.java JAVA:scorereader.java

public class scorereader extends Activity {

    public void onCreate(Bundle savedInstanceState) {
    try
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainactivity); 

        TextView tv = (TextView)findViewById(R.id.resultbox);
        try{
            HttpClient httpclient = new DefaultHttpClient(); 
            HttpPost httppost = new HttpPost("http://greentapcore.tumblr.com/");
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            InputStream webs = entity.getContent();
            try{

                BufferedReader reader = new BufferedReader(new InputStreamReader(webs,"iso-8859-1"),8);
                tv.setText(reader.readLine()); 
                webs.close();

            }catch(Exception e){
                Log.e("log_tag", "Error converting result "+e.toString()); 
            }
        }catch(Exception e){
            Log.e("log_tag", "Error in HTTP connection "+e.toString());

        }

    }
    catch (Exception e)
    { 
        Log.e("ERROR", "ERROR IN CODE: " + e.toString()); 
        e.printStackTrace();

    }

} 

}

HTML from a Tumblr Site: -Entirely blank page with "hi" at the top. 来自Tumblr网站的HTML:-完全空白的页面,顶部带有“ hi”。

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>scoretrack</title>
    <link rel="stylesheet" href="style.css">
    <script src="script.js"></script>
  </head>
  <body>
    hi
  </body>
</html>

Thank you so much in advance! 提前非常感谢您! Highly appreciate it. 高度赞赏。

Use HttpGet instead of HttpPost . 使用HttpGet而不是HttpPost HttpGet will perform a GET request instead of a POST request, which should download the web page properly. HttpGet将执行GET请求而不是POST请求,后者应正确下载网页。

Pay attention to the message in the logs 注意日志中的消息

Error in HTTP connection android.os.NetworkOnMainThreadException

follow in another thread a request to the server 在另一个线程中跟随对服务器的请求

private TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainactivity);
        tv = (TextView)findViewById(R.id.resultbox);
        new Task().execute();

    }

    class Task extends AsyncTask<Void, Void, String>{
        @Override
        protected String doInBackground(Void... params) {
            try{
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://greentapcore.tumblr.com/");
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                InputStream webs = entity.getContent();
                try{
                    StringBuilder result = new StringBuilder();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(webs,"iso-8859-1"),8);
                    String line;
                    while( (line = reader.readLine()) != null) {
                        result.append(line);
                    }
                    webs.close();
                    return result.toString();

                }catch(Exception e){
                    Log.e("log_tag", "Error converting result "+e.toString());
                }
            }catch(Exception e){
                Log.e("log_tag", "Error in HTTP connection "+e.toString());

            }
            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            if(s != null){
                tv.setText(s);
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM