我用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的文本内容的
求对这方面比较熟悉的大大们指导
上传了一个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的文本内容的
求对这方面比较熟悉的大大们指导
解决方案 »
- android使图片按照指点的点旋转
- 有关ANDROID程序崩溃信息的处理,一起讨论一下吧!
- 关于Android中手机系统清理,手机体检的需求!
- 【高分求助】用NDK开发的程序怎么调试啊?
- Android 可以连接其他类型数据库吗?
- android中使用资源文件的一些疑问
- 用安卓谷歌地图API的疑惑
- WebView 只在屏幕左半边显示网页内容的问题
- Android缓存设计实现思路求大叔大哥阿姨帮助
- 在Android Studio中,怎么把按钮放在底部位置,且不随上面的文本移动
- SQLiteOpenHelper类的onCreate方法什么时候被执行
- 小弟,想要用GPS然后在一个图片上打点记录运行的轨迹
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!
我是用的android4.0模拟器
除了添加网络权限还要作什么配置吗
因为adb shell
ping不通其他网络,只有10.0.2.2 10.0.2.15 127.0.0.1
但是模拟器的浏览器可以访问199.99.99.144
以上设计到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);
}
}
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
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不支持
如果可以的话。你可以参考我的代码。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();
获取流以后的不用我说吧
是不是说明url是可以获得这个文件的
我真怀疑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
甚至我还在自己笔记本4了下
连的外网百度百科
还是浏览器可以打开
代码得不到
而且logcat error没日志
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个星期,求助!!!
发现第一步 url竟然是空的
URL myURL = null;
myURL =new URL("http://199.99.99.144:80//hello.txt");
查看结果如下:
"myURL" null