0

你能告诉为什么输出消失得这么快吗?

如果要运行代码,您需要在 Androidmanifest.xml 文件中添加以下内容

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

下面是代码:

package prototype.networking.textfiles;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends Activity 
{
    //-------------------------------------------------------------- OpenHttpConnection()------------------------------------------------//
    private InputStream OpenHttpConnection(String urlString) throws IOException
    {
            InputStream in = null;
            int response = -1;
            URL url = new URL(
                    urlString);
            URLConnection conn = url.openConnection();
            if (!(conn instanceof HttpURLConnection))
            throw new IOException("Not an HTTP connection");
            try
            {
                HttpURLConnection httpConn = (HttpURLConnection) conn;
                httpConn.setAllowUserInteraction(false);
                httpConn.setInstanceFollowRedirects(true);
                httpConn.setRequestMethod("GET");
                httpConn.connect();
                response = httpConn.getResponseCode();
                if(response == HttpURLConnection.HTTP_OK) 
                {
                    in = httpConn.getInputStream();
                }
            }
            catch (Exception ex)
            {
                throw new IOException("Error connecting");
            }
            return in;
            } 
            //--------------------------------------------------OpenHttpConnection ends here-------------------------------------------------------------//
            //--------------------------------------------------Download Plain Text Files (RSS) --------------------------------------------------------------//
    private String DownloadText(String URL)
    {
        int BUFFER_SIZE = 2000;
        InputStream in = null;
        try 
        {
            in = OpenHttpConnection(URL);
        } 
        catch (IOException e1) 
        {
            Toast.makeText(this, e1.getLocalizedMessage(), Toast.LENGTH_LONG)       .show();
            e1.printStackTrace();
            return "";
        }
        InputStreamReader isr = new InputStreamReader(in);
        int charRead;
        String str = "";
        char[] inputBuffer = new char[BUFFER_SIZE];
        try 
        {
            while ((charRead = isr.read(inputBuffer))>0)
            {
                //---convert the chars to a String---
                String readString = String.copyValueOf(inputBuffer, 0, charRead);
                str += readString;
                inputBuffer = new char[BUFFER_SIZE];
            }
            in.close();
        } 
        catch (IOException e) 
        {
            Toast.makeText(this, e.getLocalizedMessage(),    Toast.LENGTH_LONG) .show();
            e.printStackTrace();            
            return "";
        }
        return str;
    }
    //-------------------------------------------------DownloadText() ends here--------------------------------------------------------------------------//
    //-------------------------This method downloads "PLAIN TEXT FILES"-------------------------------------------------------------------------//
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        String str = DownloadText("http://www.appleinsider.com/appleinsider.rss");
                Toast.makeText(getBaseContext(), str,   Toast.LENGTH_LONG)  .show();
    }
}
4

1 回答 1

0

你说“输出在一秒钟内消失”。但是你把它显示在一个Toast. 这就是 Toast 应该做的:查看文档- “通知自动淡入淡出,并且不接受交互事件”。这是所需的功能,而不是问题。

Toast 的长度是Toast.LENGTH_SHORTToast.LENGTH_LONG。如果您想在较长时间内显示通知,请考虑使用自定义DialogDialogFragment或 StatusBarNotification。或者,如果您不希望它消失,只需将其放在 View 中,例如TextView.

于 2012-04-06T10:50:01.560 回答