无法连接到http Android

无法连接到http Android,android,httpurlconnection,httpconnection,Android,Httpurlconnection,Httpconnection,当连接太低时,我得到一个异常“未能连接到:http……”,这是我的代码,请任何人帮助我避免异常。 当连接太低时,我得到一个异常“未能连接到:http……”,这是我的代码,请任何人帮助我避免异常 private void parseM3uUrlAndPrepare_new(final String url) { AsyncTask<String, Integer, String> asyn = new AsyncTask<String, Integer, String

当连接太低时,我得到一个异常“未能连接到: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("");
} 
private void parseM3uUrlAndPrepare\u new(最终字符串url){
AsyncTask asyn=新建AsyncTask(){
URL这个URL;
httpurl连接连接;
字符串filePath=“”;
输入流输入流;
HttpGet-getRequest;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
试一试{
_url=新url(url);
conn=(HttpURLConnection)url.openConnection(Proxy.NO\u Proxy);
getRequest=新的HttpGet(url);
}
捕获(格式错误){
e、 printStackTrace();
} 
捕获(IOE异常){
e、 printStackTrace();
}
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
如果(conn!=null){
试一试{
inputStream=新的BufferedInputStream(conn.getInputStream());
BufferedReader BufferedReader=新的BufferedReader(新的InputStreamReader(inputStream));
弦线;
而((line=bufferedReader.readLine())!=null){
如果(第行开始带(“#”){
} 
else if(line.length()>0){
filePath=“”;
如果(line.startsWith(“http://”)为{//,则假定它是一个完整的URL
filePath=line;
} 
否则{//假设它是相对的
试一试{
filePath=getRequest.getURI().resolve(line.toString();
}
捕获(IllegalArgumentException e){
e、 printStackTrace();
}
捕获(例外e){
e、 printStackTrace();
}
}
}
}
} 
捕获(例外e){
e、 printStackTrace();
}
试一试{
inputStream.close();
} 
捕获(IOE异常){
e、 printStackTrace();
}
}
返回文件路径;
}
@凌驾
受保护的void onPostExecute(字符串文件路径){
试一试{
mediaPlayer.setDataSource(文件路径);
数据集=真;
mediaPlayer.prepareAsync();//这将准备文件a.k.a缓冲区
} 
捕获(IllegalArgumentException e){
e、 printStackTrace();
}
捕获(非法状态){
e、 printStackTrace();
}
捕获(IOE异常){
e、 printStackTrace();
}
}
};
自动执行(“”);
} 

问题可能在于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();
    }
}

你有没有想过使用像或这样的网络库?使用这些网络库,可以配置网络超时。为什么不使用AsyncTask扩展功能?发布完整堆栈跟踪异常。也许这篇文章可以帮助您。我想您只需要使用HttpGet,而不需要使用HeetPost: