android4.0中HttpURLConnection抛异常java.io.FileNotFoundException

原文地址:http://www.mikebai.com/html/2012-03/939.html

最近把机器刷到4.0了
跑了一下自己的app,发现下载xml文件部分抛出异常:java.io.FileNotFoundException: http://www.jpfocus.com/xxxx.xml

可是在2.3的系统却可以正常下载

我的代码:

URL url = new URL(urlstr);
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setRequestMethod("GET");
httpCon.setDoOutput(true);
httpCon.connect();

原因:
4.0中设置httpCon.setDoOutput(true),将导致请求以post方式提交,即使设置了httpCon.setRequestMethod("GET");
将代码中的httpCon.setDoOutput(true);删除即可



关于setDoOutput(true)

网上查到的解释是,设置true,表示你发送的请求,会把body的内容发送至server端,即POST和PUT才需要使用。GET完全可以不用设置。


已标记关键词 清除标记
开发新手,刚接触 望大家指点 代码段 new Thread() { public void run() { String httpURL = "http://115.159.118.230:8080/map/map"; URL url = null; try { url = new URL(httpURL); } catch (MalformedURLException e1) { e1.printStackTrace(); } if (url != null) { try { HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setRequestMethod("POST"); conn.setUseCaches(false); conn.connect(); DataOutputStream dos = new DataOutputStream( conn.getOutputStream()); dos.writeInt(MyProtocal.LOGIN); dos.writeInt(MyProtocal.ID_STUDENT); dos.writeInt(Integer.parseInt(snoEditText.getText().toString())); dos.writeUTF(pwdEditText.getText().toString()); dos.flush(); dos.close(); //程序到这就不运行了 DataInputStream dis = new DataInputStream(conn.getInputStream()); Log.i("tag", "dis"); int b= dis.readInt(); if(b==MyProtocal.OK){ Intent intent1 = new Intent(context, StudentMapActivity.class); startActivity(intent1); } dis.close(); } catch (IOException e) { e.printStackTrace(); } } else { Toast.makeText(context, "url is null", Toast.LENGTH_SHORT).show(); } } }.start(); Logcat: 08-08 20:51:47.454: W/System.err(8915): java.io.FileNotFoundException: http://115.159.118.230:8080/map/map 08-08 20:51:47.454: W/System.err(8915): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177) 08-08 20:51:47.454: W/System.err(8915): at com.hfuu.map_ts.LoginActivity$3.run(LoginActivity.java:187) tomcat配置文件web.xml <?xml version="1.0" encoding="utf-8"?> <web-app> <servlet> <servlet-name>mapServ</servlet-name> <servlet-class>MapServerHttpServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>mapServ</servlet-name> <url-pattern>/map</url-pattern> </servlet-mapping> </web-app> 登录时tomcat界面也没有打印出任何信息 没反应 希望大家帮忙!
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页