Android访问的网络上的txt文件地址

<uses-permission android:name="android.permission.INTERNET" />
TextView tv = new TextView(this);
  
  String myString = null;
  
  try
  {
   /* 定义我们要访问的地址url */
   URL uri = new URL("http://192.***.*.***:8080/android.txt");
   
   /* 打开这个url连接 */
   URLConnection ucon = uri.openConnection();
   
   /* 从上面的链接中取得InputStream */
   InputStream is = ucon.getInputStream();
   
   BufferedInputStream bis = new BufferedInputStream(is);
   ByteArrayBuffer baf = new ByteArrayBuffer(100);
   int current = 0;
   /* 一直读到文件结束 */
   while ((current = bis.read()) != -1)
   {
    baf.append((byte) current);
   }
   myString = new String(baf.toByteArray());
  }
  catch (Exception e)
  {

   myString = e.getMessage();
  }
  /* 将信息设置到TextView */
  tv.setText(myString);
  
  /* 将TextView显示到屏幕上 */
  this.setContentView(tv);

猜你喜欢

转载自yebingzi.iteye.com/blog/1667899
今日推荐