2012-04-06 69 views
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(); 
    } 
} 
+0

請檢查LogCat中是否有任何異常消息 – 2012-04-06 09:53:01

+0

您面臨什麼問題並粘貼錯誤如果有問題 – 2012-04-06 10:01:19

+0

Agrawal先生,沒有錯誤。當我運行我的應用程序時,我看到了我的Activity類的名稱。我看到輸出(html頁面)約一秒鐘,然後立即消失。 我試着增加Toast類的持續時間,但無濟於事。 它在這裏: – Niteesh 2012-04-06 10:13:45

回答

0

你說「輸出消失中的塞康」。但是你在Toast中顯示它。這就是Toast應該做的事情:查看documentation - 「通知自動淡入淡出,不接受交互事件」。這是所需的功能,而不是問題。

吐司的長度是Toast.LENGTH_SHORTToast.LENGTH_LONG。如果想要顯示較長時間的通知,請考慮使用自定義DialogDialogFragment或StatusBarNotification。或者,如果您不希望它消失,只需將其放入視圖中即可TextView