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();
}
}
請檢查LogCat中是否有任何異常消息 – 2012-04-06 09:53:01
您面臨什麼問題並粘貼錯誤如果有問題 – 2012-04-06 10:01:19
Agrawal先生,沒有錯誤。當我運行我的應用程序時,我看到了我的Activity類的名稱。我看到輸出(html頁面)約一秒鐘,然後立即消失。 我試着增加Toast類的持續時間,但無濟於事。 它在這裏: – Niteesh 2012-04-06 10:13:45