首页 > Web开发 > 详细

HttpURLConnection中connection.getInputStream()报异常FileNotFoundException

时间:2014-11-11 16:10:43      阅读:985      评论:0      收藏:0      [点我收藏+]

============问题描述============


在AVD上测试没问题。换到设备上时报FileNotFound的异常,经常报这个异常。各位帮忙看看。
代码如下:

public class HttpAssist {



    

    public static String doPost(JSONObject json) throws IOException{

        URL postUrl = new URL(Config.url);

        HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();

        connection.setDoOutput(true);

        connection.setDoInput(true);

        connection.setRequestMethod("POST");

        connection.setUseCaches(false);



        connection.connect();

        OutputStreamWriter  out = new OutputStreamWriter (connection

                .getOutputStream());

        out.write( Config.getParameterName + "="  + json.toString());

        out.flush();

        out.close(); // flush and close

        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"utf-8"));

        String line="";

        String res = "";

        while ((line = reader.readLine()) != null){

        	res += line;

            System.out.println(line);

        }

        reader.close();

        connection.disconnect();

        

        return res;

    }

}



异常如下:
bubuko.com,布布扣

也不是每次都报,但是经常会报这个异常。查了半天也没查到点有用的东西。各位,就看你们了。。。

============解决方案1============


获取流之前 建议 判断responseCode是200.

============解决方案2============


那你就把返回的状态码判断下看看

HttpURLConnection中connection.getInputStream()报异常FileNotFoundException

原文:http://www.cnblogs.com/yiguobei99/p/4089443.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!