出错部分的源代码:
public String download(String sUrl){
StringBuffer sb = new StringBuffer();
String line = null;
BufferedReader buffer = null;
String errinfoString=null;
try{
url = new URL(sUrl);
//创建Http对象
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
//urlConn.connect();
//使用IO流读取数据
buffer = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
while( (line = buffer.readLine()) != null ){
sb.append(line);
}
}catch(Exception e){
e.printStackTrace();
errinfoString="http_download_err_need_check_server_function";
}finally{
try{
buffer.close();
}catch(Exception e){
e.printStackTrace();
Log.v("httpdownload错误",e.toString());
return "http_download_err_need_check_server_function";
}
}
return sb.toString();
}
这个是源代码需要用到的url
surl=“http://xxxxxx:80/op.asp?op=CreatPurchaseOrder&str=2014-09-04,Mobile,1036,3653,%E8%BF%9B%E8%B4%A7%E4%BB%B7,44,1000,0, :MCG20140904000023886||P2300055,%E7%81%B0%E8%89%B2J,%E4%BB%B6,1,55,1,MCG20140904000023886$$P2600128,%E7%99%BD%E8%89%B2J,%E4%BB%B6,2,31,2,MCG20140904000023886$$P2400073,%E8%93%9D%E8%89%B2J,%E4%BB%B6,2,58,3,MCG20140904000023886$$P2400070,%E5%8D%A1%E5%85%B6%E8%89%B2J,%E4%BB%B6,3,50,4,MCG20140904000023886$$P2400070,%E8%97%8F%E8%93%9D%E8%89%B2J,%E4%BB%B6,1,50,5,MCG20140904000023886$$P2400070,%E6%B5%85%E7%81%B0J,%E4%BB%B6,64,50,6,MCG20140904000023886$$P3700010,%E7%B2%89%E8%89%B2J,%E4%BB%B6,1,20,7,MCG20140904000023886”
这个url直接在电脑上浏览器是可以打开的,放到程序里面就提示错误了
错误内容:
09-05 10:31:39.749: W/System.err(21771): java.io.FileNotFoundException: http://121.199.166.127:80/op.asp?op=CreatPurchaseOrder&str=2014-09-04,Mobile,1036,3653,%E8%BF%9B%E8%B4%A7%E4%BB%B7,44,1000,0, :MCG20140904000023886||P2300055,%E7%81%B0%E8%89%B2J,%E4%BB%B6,1,55,1,MCG20140904000023886$$P2600128,%E7%99%BD%E8%89%B2J,%E4%BB%B6,2,31,2,MCG20140904000023886$$P2400073,%E8%93%9D%E8%89%B2J,%E4%BB%B6,2,58,3,MCG20140904000023886$$P2400070,%E5%8D%A1%E5%85%B6%E8%89%B2J,%E4%BB%B6,3,50,4,MCG20140904000023886$$P2400070,%E8%97%8F%E8%93%9D%E8%89%B2J,%E4%BB%B6,1,50,5,MCG20140904000023886$$P2400070,%E6%B5%85%E7%81%B0J,%E4%BB%B6,64,50,6,MCG20140904000023886$$P3700010,%E7%B2%89%E8%89%B2J,%E4%BB%B6,1,20,7,MCG20140904000023886
09-05 10:31:39.759: W/System.err(21771): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:521)
09-05 10:31:39.759: W/System.err(21771): at com.teddy.xstore.HttpDownloader.download(HttpDownloader.java:43)
09-05 10:31:39.759: W/System.err(21771): at com.teddy.xstore.PublicFucntion.upload_purchase_order(PublicFucntion.java:466)
09-05 10:31:39.759: W/System.err(21771): at com.teddy.xstore.buy_purchase_stockinandpay.up_pay(buy_purchase_stockinandpay.java:736)
09-05 10:31:39.759: W/System.err(21771): at com.teddy.xstore.buy_purchase_stockinandpay$1.run(buy_purchase_stockinandpay.java:700)
09-05 10:31:39.759: W/System.err(21771): at java.lang.Thread.run(Thread.java:1019)
09-05 10:31:39.759: W/System.err(21771): java.lang.NullPointerException
09-05 10:31:39.759: W/System.err(21771): at com.teddy.xstore.HttpDownloader.download(HttpDownloader.java:52)
09-05 10:31:39.759: W/System.err(21771): at com.teddy.xstore.PublicFucntion.upload_purchase_order(PublicFucntion.java:466)
09-05 10:31:39.759: W/System.err(21771): at com.teddy.xstore.buy_purchase_stockinandpay.up_pay(buy_purchase_stockinandpay.java:736)
09-05 10:31:39.759: W/System.err(21771): at com.teddy.xstore.buy_purchase_stockinandpay$1.run(buy_purchase_stockinandpay.java:700)
09-05 10:31:39.759: W/System.err(21771): at java.lang.Thread.run(Thread.java:1019)
上面这个错误提示就是提示这里错误:
buffer = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
这里 new 应该是不成功的,最后捕捉到的错误是java.lang.NullPointerException
实在是搞不明白,哪里问题
程序已经用了一段时间了,只碰到这一个url有此问题,其他的url都没有提示这个错误,找了很久也没看出哪里问题,求大侠们指点一下
8 个解决方案
读取流之前 选判断responseCode是否为200,如果为200再读取InputStream
估计是你这个链接里包含了些特殊字符,被过滤掉了,然后就直接访问不到了,获取不到数据了
public String encode(String str){
try {
str=URLEncoder.encode(str, "UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return str;
}
public InputStream getInputStreamFromUrl(String sUrl) throws MalformedURLException, IOException{
url = new URL(sUrl);
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
InputStream inputStream = urlConn.getInputStream();
return inputStream;
}
问题解决了。。有个地方没有urlencode,sUrl字符串里有个空格