0
なぜ出力がすばやく消えるのか教えてください。出力時間がそれほど短くないのはなぜですか?
あなたがコードを実行したい場合は、下記
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
あなたのAndroidManifest.xmlファイルに次のように必要になりますコードです:あなたは出力が内消える」と言う
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に例外メッセージがあるかどうか確認してください。 –
問題があればエラーを貼り付けてください。 –
Mr. Agrawal、エラーはありません。私は私のアプリを実行すると、私は私のアクティビティクラスの名前が表示されます。出力(HTMLページ)が約1秒間表示され、すぐに消えます。 私はToastクラスの継続時間を長くしようとしましたが、役に立たなかった。 ここにある: – Niteesh