EOFException类和FileNotFoundException异常的HttpURLConnection类的getInputStream() [英] EOFException and FileNotFoundException in HttpURLConnection getInputStream()

查看:281
本文介绍了EOFException类和FileNotFoundException异常的HttpURLConnection类的getInputStream()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试与后连接到 http://ws.audioscrobbler.com/2.0/ 使用数据 HttpURLConnection类,我得到(随机)

  EOFException类

  FileNotFoundException异常:http://ws.audioscrobbler.com/2.0/

在我的Nexus 4运行Android 4.2.2。

任何人都可以帮忙吗?

修改

我升级到4.3:同样的问题

 公众的InputStream的getData(字符串URL_STR,列表与LT;&的NameValuePair GT; POSTDATA){    地图<弦乐,对象>响应=新的HashMap<弦乐,对象>();    InputStream为= NULL;
    HttpURLConnection的康恩= NULL;    尝试{        网址URL =新的URL(URL_STR);        康恩=(HttpURLConnection类)url.openConnection();
        conn.setReadTimeout(CONNECTION_TIMEOUT);
        conn.setConnectTimeout(READ_TIMEOUT);
        conn.setRequestMethod(POST);
        conn.setDoInput(真);        如果(POSTDATA!= NULL){            conn.setDoOutput(真);            OutputStream的OS = conn.getOutputStream();
            但是BufferedWriter作家=新的BufferedWriter(新OutputStreamWriter(OS,UTF-8));
            writer.write(getQuery(POSTDATA));
            writer.close();
            os.close();
        }
        //开始查询        conn.connect();
        //获取并返回响应        是= conn.getInputStream();        回报;    }赶上(IOException异常五){        // ...    } {最后        如果(参数conn!= NULL){
            conn.disconnect();
        }        如果(是!= NULL){
            尝试{
                is.close();
            }赶上(IOException异常五){
                e.printStackTrace();
            }
        }
    }
}
私有String getQuery(列表<&的NameValuePair GT; PARAMS)抛出UnsupportedEncodingException
{
    StringBuilder的结果=新的StringBuilder();
    布尔第一= TRUE;    对(对的NameValuePair:PARAMS)
    {
        如果(第一)
            第一= FALSE;
        其他
            result.append(与&);        result.append(URLEn coder.en code(pair.getName(),UTF-8));
        result.append(=);
        result.append(URLEn coder.en code(pair.getValue(),UTF-8));
    }    返回result.toString();
}


解决方案

我检查 conn.getResponse code()并获得了403例外可能是由于这一点。

When trying to connect to http://ws.audioscrobbler.com/2.0/ with post data using HttpURLConnection, I get (randomly)

EOFException

or

FileNotFoundException: http://ws.audioscrobbler.com/2.0/

on my Nexus 4 running Android 4.2.2.

Anybody can help?

EDIT

I upgraded to 4.3: same issue.

public InputStream getData(String url_str, List<NameValuePair> postData) {

    Map<String, Object> response = new HashMap<String, Object>();

    InputStream is = null;
    HttpURLConnection conn = null;

    try {

        URL url = new URL(url_str);

        conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(CONNECTION_TIMEOUT);
        conn.setConnectTimeout(READ_TIMEOUT);
        conn.setRequestMethod("POST");
        conn.setDoInput(true);                      

        if(postData != null){

            conn.setDoOutput(true);

            OutputStream os = conn.getOutputStream();
            BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(os, "UTF-8"));
            writer.write(getQuery(postData));
            writer.close();
            os.close();
        }


        // Starts the query

        conn.connect();


        // Get and return response

        is = conn.getInputStream();             

        return is;

    } catch (IOException e) {

        // ...

    } finally {

        if(conn != null){
            conn.disconnect();
        }

        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } 
    }
}


private String getQuery(List<NameValuePair> params) throws UnsupportedEncodingException
{
    StringBuilder result = new StringBuilder();
    boolean first = true;

    for (NameValuePair pair : params)
    {
        if (first)
            first = false;
        else
            result.append("&");

        result.append(URLEncoder.encode(pair.getName(), "UTF-8"));
        result.append("=");
        result.append(URLEncoder.encode(pair.getValue(), "UTF-8"));
    }

    return result.toString();
}

解决方案

I checked conn.getResponseCode() and get a 403. The exception might be due to that.

这篇关于EOFException类和FileNotFoundException异常的HttpURLConnection类的getInputStream()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆