【问题标题】:failed to connect to http Android无法连接到 http Android
【发布时间】:2016-04-24 00:55:28
【问题描述】:

当连接太低时,我得到一个异常“无法连接到:http ......”,这是我的代码,任何人都可以帮助我避免异常。 当连接太低时,我得到一个异常“无法连接到:http ......”,这是我的代码,任何人都可以帮助我避免异常

 private void parseM3uUrlAndPrepare_new(final String url) {
    AsyncTask<String, Integer, String> asyn = new  AsyncTask<String, Integer, String>(){
        URL the_url;
        HttpURLConnection conn;
        String filePath = "";
        InputStream inputStream;
        HttpGet getRequest;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            try {
                the_url = new URL(url);
                conn = (HttpURLConnection) the_url.openConnection(Proxy.NO_PROXY);
                getRequest = new HttpGet(url);
            }
            catch (MalformedURLException e) {
                e.printStackTrace();
            } 
            catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        protected String doInBackground(String... params) {
            if(conn != null) {
                try {
                    inputStream = new BufferedInputStream(conn.getInputStream());
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                    String line;
                    while ((line = bufferedReader.readLine()) != null) {
                        if (line.startsWith("#")) { 

                        } 
                        else if (line.length() > 0) {
                            filePath = "";
                            if (line.startsWith("http://")) { // Assume it's a full URL
                                filePath = line;
                            } 
                            else { // Assume it's relative
                                try{
                                    filePath = getRequest.getURI().resolve(line).toString();
                                }
                                catch(IllegalArgumentException e){
                                    e.printStackTrace();
                                }
                                catch(Exception e){
                                    e.printStackTrace();
                                }
                            }
                        }
                    }
                } 
                catch (Exception e) {
                    e.printStackTrace();
                }
                try {
                    inputStream.close();
                } 
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return filePath;
        }

        @Override
        protected void onPostExecute(String filePath) {
            try {
                mediaPlayer.setDataSource(filePath);
                DATA_SET = true;
                mediaPlayer.prepareAsync(); //this will prepare file a.k.a buffering

            } 
            catch (IllegalArgumentException e) {
                e.printStackTrace();
            }
            catch (IllegalStateException e) {
                e.printStackTrace();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
    };
    asyn.execute("");
} 

【问题讨论】:

  • 您是否考虑过使用像VolleyRetrofit 这样的网络库?使用这些网络库,网络超时是可配置的。
  • 你为什么不用 AsyncTask 扩展你的功能呢?
  • 发布完整堆栈跟踪异常。

标签: android httpurlconnection httpconnection


【解决方案1】:

也许问题在于 BufferedInputStream。如果你想试试,我写了这段代码(很久以前)。

给函数一个输入流并让它工作。

import java.io.InputStream;
import java.util.Scanner;
/**
 * Created by badetitou.
 */
public class ReadIt {

    public static String ReadIt(InputStream is){
        return new Scanner(is,"UTF-8").useDelimiter("").next();
    }
}

【讨论】:

猜你喜欢
  • 2023-02-07
  • 2018-12-20
  • 2022-06-11
  • 1970-01-01
  • 2018-02-05
  • 2020-09-14
  • 2018-12-26
  • 1970-01-01
  • 2012-07-28
相关资源
最近更新 更多