【问题标题】:how to get http text to textview android如何将http文本获取到textview android
【发布时间】:2014-08-02 09:54:27
【问题描述】:

我是 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

【问题讨论】:

  • 您是否在 UI 线程上连接到网络?你的后台任务在哪里?

标签: android web-services http


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    如果只是将网络上的一些文本显示到你的 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");
    

    【讨论】:

      【解决方案3】:

      在单独的线程中发出 http 请求或使用 AsyncTask(Android 方式)。 我在 thred 中使用它,它会点击 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();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-04-14
        • 2016-06-26
        • 2012-03-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-09
        相关资源
        最近更新 更多