• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps InputStream text file from internet

HeavensSentSword

Well-Known Member
Jan 19, 2016
134
36
I have been working on getting a text file from the internet and so far ok. But I hit a bit of a wall. The code runs all the way to the point where it is looking at the inputStream and start processing it. It doesn't enter into the try statement at all. The value is right when I looked at the param[0] so I am a bit lost, have I forgot something here to let if run?

Here is the class that does all the downloading and processing.

Java:
import android.os.AsyncTask;
import android.util.Log;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class DownloadFile extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {
        Log.d("Params " , params[0].toString());
        readFromFile(params[0]);
        return null;
    }

    private String readFromFile(String myWebpage) {
        Log.d("ReadFrom File ", " Method Start");
        String ret = "";
        HttpURLConnection urlConnection = null;

        try {
            //Get the url connection
            URL url = new URL(myWebpage);
            urlConnection = (HttpURLConnection) url.openConnection();
            InputStream inputStream = urlConnection.getInputStream();
            Log.d("InputStrream ", inputStream.toString());

            if (inputStream != null) {
                Log.d("Reading the String" , "Now");
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                String receiveString = "";
                StringBuilder stringBuilder = new StringBuilder();
                while ((receiveString = bufferedReader.readLine()) != null) {
                    stringBuilder.append(receiveString);
                }
                inputStream.close();
                ret = stringBuilder.toString();
                Log.d("Final String", ret);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (urlConnection != null) {
                urlConnection.disconnect();
            }
            return ret;

        }
    }
}

And This is the main activity in case this is the problem but I dont think it is.
Java:
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends AppCompatActivity {

    final String myWebpage = "http://sites.google.com/site/androidersite/text.txt";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String finalString;
        Log.d("Starting App: " , "Now");
        DownloadFile df = new DownloadFile();
        Log.d("Running Method " , "1");
        df.execute(myWebpage);



    }




}


Thank you for any help with this.
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones