简体   繁体   中英

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. 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. I have included all internet permissions in my manifest and my XML file is just a textview in a linear layout. The app only has one class called "scorereader" and that's about it. When I run it, it just shows the blank XML with the textview. It does not display the text from the HttpClient.

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.

<!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 will perform a GET request instead of a POST request, which should download the web page properly.

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);
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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