首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何解决AndroidJava-HttpURLConnection失败而没有引发异常?

如何解决AndroidJava-HttpURLConnection失败而没有引发异常?
EN

Stack Overflow用户
提问于 2018-10-10 05:09:59
回答 1查看 0关注 0票数 0

以下是我使用的代码:

代码语言:txt
复制
public static String getResponseFromHttpUrl(URL url) throws IOException {
    HttpURLConnection urlConnection = null;
    try {
        urlConnection = (HttpURLConnection) url.openConnection();

        urlConnection.setConnectTimeout(500);
        urlConnection.setReadTimeout(500);

        int statusCode = urlConnection.getResponseCode();

        InputStream in = urlConnection.getInputStream();

        Scanner scanner = new Scanner(in);

        scanner.useDelimiter("\\A");

        boolean hasInput = scanner.hasNext();

        if (hasInput) {
            return scanner.next();
        } else {
            return null;
        }
    }
    catch(Exception ex){
        Log.d("NetworkUtils", "error in HTTP request");
        return null;
    }
    finally {
        Log.d("NetworkUtils", "before disconnect");
        if(urlConnection != null) {
            urlConnection.disconnect();
        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2018-10-10 14:23:24

所以这里有个函数AsyncTask,它不会抛出NetworkOnMainThreadException:

代码语言:txt
复制
public class UrlConnectionTask extends AsyncTask<Bundle, Integer, String> {

    private HttpURLConnection urlConnection = null;
    private URL url = null;

    public UrlConnectionTask(String spec) {
        try {
            this.url = new URL(spec);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onPreExecute() {

    }

    @Override
    protected String doInBackground(Bundle... bundles) {
        StringBuilder result = new StringBuilder();
        if(this.url != null) {
            try {

                this.urlConnection = (HttpURLConnection) url.openConnection();
                this.urlConnection.setConnectTimeout(5000);
                this.urlConnection.setReadTimeout(5000);

                if(this.urlConnection.getResponseCode() == 200) {
                    InputStream is = this.urlConnection.getInputStream();
                    BufferedReader r = new BufferedReader(new InputStreamReader(is));
                    String inputLine;
                    while ((inputLine = r.readLine()) != null) {
                        result.append(inputLine);
                    }
                    is.close();
                    t.close();
                }

            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
        return result.toString();
    }

    @Override
    protected void onPostExecute(String result) {
        System.out.println(result);
        if (this.urlConnection != null) {
            this.urlConnection.disconnect();
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100005061

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档