我用IIS建了一个web,用的本机ip(199.99.99.144)
上传了一个hello.txt
我想用android 模拟器通过url获取这个文本
通过下面的代码
package org.loulijun.HttpGet;import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;import org.apache.http.util.ByteArrayBuffer;
import org.apache.http.util.EncodingUtils;import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;public class HttpGet extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView tv=new TextView(this);
        String myString=null;
        try
        {
         //定义获取文件内容的URL
         URL myURL=new URL("http://199.99.99.144:80/hello.txt");
         //打开URL链接
         URLConnection ucon=myURL.openConnection();
         //使用InputStream,从URLConnection读取数据
         InputStream is=ucon.getInputStream();
         BufferedInputStream bis=new BufferedInputStream(is);
         //用ByteArrayBuffer缓存
         ByteArrayBuffer baf=new ByteArrayBuffer(50);
         int current=0;
         while((current=bis.read())!=-1)
         {
          baf.append((byte)current);
         }
         //将缓存的内容转化为String,用UTF-8编码
         myString=EncodingUtils.getString(baf.toByteArray(), "UTF-8");
        }catch(Exception e)
        {
         myString=e.getMessage();
        }
        //设置屏幕显示
        tv.setText(myString);
        this.setContentView(tv);
    }
}
为什么模拟器屏幕没显示啊,网络权限也加了,我看别人都是这么做的啊
而且模拟器的浏览器输入这个地址是可以看到hello.txt的文本内容的
求对这方面比较熟悉的大大们指导

解决方案 »

  1.   

    logcat中有这么个错误,不知道啥意思
    12-21 01:32:19.655: E/InputDispatcher(89): channel '41620af8 com.web.test/com.web.test.WebTest (server)' ~ Channel is unrecoverably broken and will be disposed!
      

  2.   

    是没有取到东西
    我是用的android4.0模拟器
    除了添加网络权限还要作什么配置吗
    因为adb shell 
    ping不通其他网络,只有10.0.2.2 10.0.2.15 127.0.0.1
    但是模拟器的浏览器可以访问199.99.99.144
      

  3.   

    打算学android  进来了解了解   呵呵呵 
      

  4.   

    我也是刚接触java和android
      

  5.   

    流出现问题了,意思是:渠道出现了不可恢复的断裂,将会被中断
    以上设计到java的io流问题,你试下下面的代码package org.loulijun.HttpGet;import java.io.BufferedInputStream;
    import java.io.InputStream;
    import java.net.URL;
    import java.net.URLConnection;import org.apache.http.util.ByteArrayBuffer;
    import org.apache.http.util.EncodingUtils;import android.app.Activity;
    import android.os.Bundle;
    import android.widget.TextView;public class HttpGet extends Activity {
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      TextView tv=new TextView(this);
      String myString=null;
      URLConnection ucon=null;
      InputStream is=null;
      BufferedInputStream bis=null;
      ByteArrayBuffer baf=null;
      try
      {
      //定义获取文件内容的URL
      URL myURL=new URL("http://199.99.99.144:80/hello.txt");
      //打开URL链接
      ucon=myURL.openConnection();
      //使用InputStream,从URLConnection读取数据
      is=ucon.getInputStream();
      bis=new BufferedInputStream(is);
      //用ByteArrayBuffer缓存
      baf=new ByteArrayBuffer(50);
      int current=0;
      while((current=bis.read())!=-1)
      {
      baf.append((byte)current);
      }
      //将缓存的内容转化为String,用UTF-8编码
      myString=EncodingUtils.getString(baf.toByteArray(), "UTF-8");
      }catch(Exception e)
      {
      myString=e.getMessage();
      }finally{
        if(is!=null){
           is.close();
           is=null;
        }
    //其实的ucon,bis baf记得后面都要关闭,ucon要最后断开连接
      }
      //设置屏幕显示
      tv.setText(myString);
      this.setContentView(tv);
      }
    }
      

  6.   

    is.close();这一句有错
    Unhandled exception type IOException
    还有我想问下是不是android 4.0模拟器有问题
    我看到logcat里有这么几句日志
    12-21 06:04:40.020: E/Netd(31): Unable to bind netlink socket: No such file or directory
    12-21 06:04:40.020: E/Netd(31): Unable to open quota2 logging socket
    12-21 06:04:50.021: E/PhonePolicy(35): Could not preload class for phone policy: com.android.internal.policy.impl.PhoneWindow$ContextMenuCallback
    12-21 06:05:13.959: E/BatteryService(88): usbOnlinePath not found
    12-21 06:05:13.959: E/BatteryService(88): batteryVoltagePath not found
    12-21 06:05:13.979: E/BatteryService(88): batteryTemperaturePath not found
      

  7.   

    我写的finally里的代码没有处理异常,你加上去
      

  8.   

    加了
           try {
    is.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    运行还是错误12-21 01:32:19.655: E/InputDispatcher(89): channel '41620af8 com.web.test/com.web.test.WebTest (server)' ~ Channel is unrecoverably broken and will be disposed!
    是不是要设置什么啊,还是4.0不支持
      

  9.   

    首先要确定你的url可以获取到那个文件吗???
    如果可以的话。你可以参考我的代码。public InputStream getInputStream() {
    InputStream result = null;
    try {
    httpRequest = new HttpPost(url);
    httpResponse = httpClient.execute(httpRequest);
    if (200 == httpResponse.getStatusLine().getStatusCode()) {
    result = httpResponse.getEntity().getContent();
    }
    } catch (IOException e) {
    System.out.println("没有下载到文件");
    return result;
    }
    return result;
    }
    public HttpConnect(String url, Context context) {
    this.url = url;
    httpParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParams, 15000);
    HttpConnectionParams.setSoTimeout(httpParams, 15000);
    httpClient = new DefaultHttpClient(httpParams);
    }String url = "http://10.1.49.137:8080/Good/GetMettingServlet";
    HttpConnect httpConnect = new HttpConnect(url, context);
    result = httpConnect.getInputStream();
    获取流以后的不用我说吧
      

  10.   

    怎么才能知道url能不能获得那个文件
      

  11.   

    我模拟器的浏览器可以打开
    是不是说明url是可以获得这个文件的
      

  12.   

    楼上的方法跟你写的是一样的,没什么区别,你试试把URLConnection 改成HttpURLConnection
      

  13.   

    你有没有用4.0模拟器做过
    我真怀疑4.0有BUG
    这是其中一部分日志
    12-21 06:04:40.020: E/Netd(31): Unable to bind netlink socket: No such file or directory
    12-21 06:04:40.020: E/Netd(31): Unable to open quota2 logging socket
    12-21 06:04:50.021: E/PhonePolicy(35): Could not preload class for phone policy: com.android.internal.policy.impl.PhoneWindow$ContextMenuCallback
    12-21 06:05:13.959: E/BatteryService(88): usbOnlinePath not found
    12-21 06:05:13.959: E/BatteryService(88): batteryVoltagePath not found
    12-21 06:05:13.979: E/BatteryService(88): batteryTemperaturePath not found
      

  14.   

    httpUrlconn.setDoInput(true);   设置允许输入
      

  15.   

    HTTP GET 方式我4了,不行
    甚至我还在自己笔记本4了下
    连的外网百度百科
    还是浏览器可以打开
    代码得不到
    而且logcat error没日志
      

  16.   

    12-22 02:42:04.472: E/Netd(31): Unable to bind netlink socket: No such file or directory
    12-22 02:42:04.472: E/Netd(31): Unable to open quota2 logging socket
    12-22 02:42:12.651: E/PhonePolicy(35): Could not preload class for phone policy: com.android.internal.policy.impl.PhoneWindow$ContextMenuCallback
    12-22 02:42:32.772: E/BatteryService(88): usbOnlinePath not found
    12-22 02:42:32.772: E/BatteryService(88): batteryVoltagePath not found
    12-22 02:42:32.772: E/BatteryService(88): batteryTemperaturePath not found
    这些日志是什么意思
    是不是说我搭建的android 4.0环境没有socket相关文件和目录,
    听别人说无论是什么浏览器,访问任何URL,底层都是通过socket来实现的
    所以我得不到网络资源,可是如果是这样,那浏览器为什么可以
    困恼了2个星期,求助!!!
      

  17.   

    设置断点作了单步调试
    发现第一步 url竟然是空的
    URL myURL = null;
    myURL =new URL("http://199.99.99.144:80//hello.txt");
    查看结果如下:
    "myURL" null