如何获取http文本到textview android

时间:2014-08-02 09:54:28

标签: android web-services http

我是Android开发新手我希望将源html文本转换为textview或字符串。我还添加了权限互联网清单,但仍然无法将文本添加到textview。请帮帮我。

我的代码是

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final TextView tv =(TextView)findViewById(R.id.textView1);

    ////////////////////////
    HttpURLConnection connection = null;
    InputStream is = null;

    try{
        connection = (HttpURLConnection) (new URL("http://sinhaladic.com/" )).openConnection();
        connection.setRequestMethod("GET");
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.connect();

        // Read the response
        StringBuffer buffer = new StringBuffer( );
        is = connection.getInputStream();

        BufferedReader br = new BufferedReader( new InputStreamReader( is ) );
        String line = null;

        while((line = br.readLine()) != null) {
            tv.append(new String(line + "\r\n"));
        }

        is.close();
        connection.disconnect();
    } catch ( Exception e ){
        e.printStackTrace();
    } finally {
        try { 
            is.close(); 
        } catch (Throwable t) {

        }
        try { 
            connection.disconnect(); 
        } catch (Throwable t) {

        };
    }
}//on create end

3 个答案:

答案 0 :(得分:0)

您无法在UI线程上执行任何类型的HttpRequests。为此,您需要使用单独的线程。尝试使用 AsyncTask 并在asynctask的 doInBackground 函数中编写所有网络操作代码。

答案 1 :(得分:0)

如果只是将一些文字从网页显示到你的android视图中,你可以使用webview而不是textview ..

<WebView 
    android:id="@+id/webView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
/>


webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://sinhaladic.com");

答案 2 :(得分:0)

在单独的线程中发出http请求或使用AsyncTask(Android Way)。 我正在使用它,它会点击url并以字符串的形式将http文本返回给你。

Thread t = new Thread(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub

            InputStream is = null;
            String json;
            try {

                DefaultHttpClient httpClient = new DefaultHttpClient();

                HttpGet httpGet = new HttpGet("http://sinhaladic.com/");
                HttpResponse httpResponse = httpClient.execute(httpGet);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
            }

            catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(is, "iso-8859-1"), 8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
                is.close();
                json = sb.toString();


    //json String is returnd Html text

                System.out.print("JSSSSSSSSSSSSSS"+json);
            } catch (Exception e) {
                Log.e("Buffer Error",
                        "Error converting result " + e.toString());
            }

        }
    }); t .start();