在android模拟器中解析不到tomcat中的xml文件

nikeUN 2013-06-27 02:30:48
在android模拟器中解析不到tomcat中的xml文件,但是在浏览器中使用相同的地址,却可以访问。
package xtm.mp3player;

import xtm.download.HttpDownloader;
import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;







public class MainActivity extends ListActivity {
private static final int UPDATE=1;
private static final int ABOUT=2;
/**
*当用户点击MENU按钮之后,我们可以在这个方法中加入自己的按钮控件
*/
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, UPDATE, 1, R.string.mp3List_update);
menu.add(0, ABOUT, 2, R.string.mp3List_about);
return super.onCreateOptionsMenu(menu);
}
/**
* 用来响应menu菜单中的按键,把索引值设置为静态变量,以免混淆。用静态变量也可以在以后容易修改。
*/
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==UPDATE){
String xml=downloadXMl("http://192.168.10.194:8080/mp3/resources.xml");
Log.d("xtmm", xml);


}else if(item.getItemId()==ABOUT){

}

return super.onOptionsItemSelected(item);
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private String downloadXMl(String urlStr){
HttpDownloader httpDownloader=new HttpDownloader();
String result=httpDownloader.download("urlStr");
Log.d("xtmm", result);
return result;
}


}

这是下载的类
package xtm.download;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;




public class HttpDownloader {

/**
* 根据URL下载文件,前提是这个文件当中的内容是文本,函数的返回值就是文件当中的内容
* 1.创建一个URL对象
* 2.通过URL对象,创建一个HttpURLConnection对象
* 3.得到InputStram
* 4.从InputStream当中读取数据
* @param urlStr
* @return
*/
public String download(String urlStr) {
StringBuffer sb = new StringBuffer();
String line = null;
BufferedReader buffer = null;
try {
// 创建一个URL对象
URL url = new URL(urlStr);
// 创建一个Http连接
HttpURLConnection urlConn = (HttpURLConnection) url
.openConnection();
// 使用IO流读取数据
buffer = new BufferedReader(new InputStreamReader(urlConn
.getInputStream()));
while ((line = buffer.readLine()) != null) {
sb.append(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
buffer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
...全文
399 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
Mars老师的代码啊,null求指导,打不开啊
乀Lan 2015-02-03
  • 打赏
  • 举报
回复
解决了么?出现一样的问题..都是和视频一样的..但是总报空指针
qq_18178309 2014-10-26
  • 打赏
  • 举报
回复
心好酸。楼主我的代码跟你的一模一样,还是不行
木星没有木 2014-05-20
  • 打赏
  • 举报
回复
楼主,我的和你的是一样的问题,程序也都差不多,我传入的url现在改成了http://10.0.2.2:8080/mp3/resources.xml,但是还是不能传入,提示我是null,是什么原因呢?
nikeUN 2014-03-19
  • 打赏
  • 举报
回复
引用 2 楼 u011429003 的回复:
我在自己的手机浏览器中输入网址http://10.0.2.2:8080/,还是没有显示出相应网页???
首先 你要建立一个Tomcatt服务器 你建立了吗
ling-er 2014-01-20
  • 打赏
  • 举报
回复
我在自己的手机浏览器中输入网址http://10.0.2.2:8080/,还是没有显示出相应网页???
nikeUN 2013-06-27
  • 打赏
  • 举报
回复
经过我艰辛的寻找,终于找到一篇文章,希望新手们可以少走一些弯路。
Android模拟器访问本地Web应用

一个早上就是想让android的模拟器可以访问到web的应用程序,但是一直是不可以,弄的不知所措。

On Windows, open the Control Panel, choose Programs, and click Turn Windows Features On or Off. Check the box labeled Internet Information Services and then click the + to its left; continue to drill down into World Wide Web Services→Application Development Features, then check the box to enable CGI (you’ll need the CGI feature to install PHP in Chapter 6, Going Offline). Click OK. After you’ve done this, you can put your web documents in your IIS document root, which is typically located at C:\inetpub\wwwroot. Try this with the test.html file you created in Chapter 1, Getting Started; you should be able to load that file by going to http://localhost/test.html in your browser.

You’ll probably need to authenticate each time you put files into that folder. To work around this problem, you can either use the IIS Manager (Start→Control Panel→System and Security→Administrative Tools) to add a new virtual directory in a folder you have permissions to modify, or you can give yourself control of C:\inetpub\wwwroot (right-click on the folder, choose Properties→Security, and then click Edit→Add, type your username, click OK, then allow Full Control and click OK). If you want to connect to your Windows web server, even over your local network, you’ll need to go into the Control Panel→System and Security→Windows Firewall→Allow A Program or Feature Through Windows Firewall and enable World Wide Web Services.

有一段资料是配置自己的电脑,就像上面说的那样:打开控制面板--选择程序和功能--点击左边的开启或关闭windows功能--然后选则CGI,等等...试了很久还是不可以。

后来在上面查的时候看到的是有一段资料的介绍:

如何在Android模拟器上访问本地的Web应用? 例如,在你的开发机器上启动一个Tomcat服务,接着打开电脑上的浏览器,默认情况下输入http://localhost:8080/就能够访问到Tomcat的主页面;那么,如果我们想在Android模拟器上来访问,可以吗?答案是肯定的,但是访问的URL会有所变化,这又是为什么呢?请继续往下看。
在一般的Java Web程序开发中,我们通常使用localhost或者127.0.0.1来访问本机的Web服务,但是如果我们在Android模拟器中也采用同样的地址来访问,Android模拟器将无法正常访问到我们的服务,这是为什么呢?我们可以这样来理解:Android的底层是Linux kernel,包括Android本身就是一个操作系统,因此,这时我们在模拟器的浏览器中输入的localhost或127.0.0.1所代表的是Android模拟器(Android虚拟机),而不是你的电脑,明白了吗?这就是为什么你在模拟器中使用localhost时会报“Web page not available”的原因。

那到底要如何才能访问到本地电脑上的Web应用呢?在Android中,将我们本地电脑的地址映射为10.0.2.2,因此,只需要将原先的localhost或者127.0.0.1换成10.0.2.2,就可以在模拟器上访问本地计算机上的Web资源了。
那下面我们就启动电脑上的Tomcat,然后运行Android模拟器,打开模拟器上的浏览器,然后访问:http://10.0.2.2:8888 是不是已经看到那只可爱的Tomcat猫了呢?访问截图如下图所示。(说明:在我的电脑上,Tomcat服务使用的端口是8888,默认情况是8080)

图:

试用了,很受用。O(∩_∩)O~呵呵
引用
http://www.cnblogs.com/YOUCAN/archive/2012/09/14/2684691.html

80,353

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧