你能说出为什么输出消失这么快吗?
如果要运行代码,则需要在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 :(得分:0)
你说“输出在secon中消失”。但是你在Toast
中显示它。这就是Toast应该做的事情:查看documentation - “通知自动淡入和淡出,并且不接受交互事件”。这是所需的功能,而不是问题。
Toast的长度为Toast.LENGTH_SHORT
或Toast.LENGTH_LONG
。如果您想要在更长的时间段内显示通知,请考虑使用自定义Dialog
或DialogFragment
或StatusBarNotification。或者,如果您不希望它消失,只需将其放入视图中,例如TextView
。