-2

when the connection is so low i get an exception " failed to connect to : http ......", this is my code, can any one please helps me to avoid the exception. when the connection is so low i get an exception " failed to connect to : http ......", this is my code, can any one please helps me to avoid the exception

 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("");
} 
fayza
  • 135
  • 1
  • 3
  • 13

1 Answers1

0

Maybe the problem is the BufferedInputStream. I wrote this code (a long time ago) if you want to try it.

Give an input stream to the fonction and let it work.

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();
    }
}
Benoît Verhaeghe
  • 612
  • 1
  • 6
  • 21
  • Maybe this other post can help you. I suppose you only have to use a HttpGet and not a HeetPost : http://stackoverflow.com/questions/6345568/android-using-httpget-to-send-a-url – Benoît Verhaeghe Jan 18 '16 at 20:01