1. Are you ready for the Galaxy S20? Here is everything we know so far!

InputStream text file from internet

Discussion in 'Android Development' started by HeavensSentSword, Feb 3, 2016.

  1. HeavensSentSword

    HeavensSentSword Well-Known Member
    Thread Starter

    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.

    Code (Java):
    1.  
    2.  
    3. import android.os.AsyncTask;
    4. import android.util.Log;
    5.  
    6. import java.io.BufferedReader;
    7. import java.io.FileNotFoundException;
    8. import java.io.IOException;
    9. import java.io.InputStream;
    10. import java.io.InputStreamReader;
    11. import java.net.HttpURLConnection;
    12. import java.net.URL;
    13.  
    14. public class DownloadFile extends AsyncTask<String, Void, String> {
    15.  
    16.     @Override
    17.     protected String doInBackground(String... params) {
    18.         Log.d("Params " , params[0].toString());
    19.         readFromFile(params[0]);
    20.         return null;
    21.     }
    22.  
    23.     private String readFromFile(String myWebpage) {
    24.         Log.d("ReadFrom File ", " Method Start");
    25.         String ret = "";
    26.         HttpURLConnection urlConnection = null;
    27.  
    28.         try {
    29.             //Get the url connection
    30.             URL url = new URL(myWebpage);
    31.             urlConnection = (HttpURLConnection) url.openConnection();
    32.             InputStream inputStream = urlConnection.getInputStream();
    33.             Log.d("InputStrream ", inputStream.toString());
    34.  
    35.             if (inputStream != null) {
    36.                 Log.d("Reading the String" , "Now");
    37.                 InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
    38.                 BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
    39.                 String receiveString = "";
    40.                 StringBuilder stringBuilder = new StringBuilder();
    41.                 while ((receiveString = bufferedReader.readLine()) != null) {
    42.                     stringBuilder.append(receiveString);
    43.                 }
    44.                 inputStream.close();
    45.                 ret = stringBuilder.toString();
    46.                 Log.d("Final String", ret);
    47.             }
    48.  
    49.         } catch (FileNotFoundException e) {
    50.             e.printStackTrace();
    51.         } catch (IOException e) {
    52.             e.printStackTrace();
    53.         } finally {
    54.             if (urlConnection != null) {
    55.                 urlConnection.disconnect();
    56.             }
    57.             return ret;
    58.  
    59.         }
    60.     }
    61. }
    62.  
    And This is the main activity in case this is the problem but I dont think it is.
    Code (Java):
    1.  
    2. import android.os.Bundle;
    3. import android.support.design.widget.FloatingActionButton;
    4. import android.support.design.widget.Snackbar;
    5. import android.support.v7.app.AppCompatActivity;
    6. import android.support.v7.widget.Toolbar;
    7. import android.util.Log;
    8. import android.view.View;
    9. import android.view.Menu;
    10. import android.view.MenuItem;
    11.  
    12. import java.io.BufferedReader;
    13. import java.io.File;
    14. import java.io.FileNotFoundException;
    15. import java.io.IOException;
    16. import java.io.InputStream;
    17. import java.io.InputStreamReader;
    18. import java.net.HttpURLConnection;
    19. import java.net.URL;
    20.  
    21. public class MainActivity extends AppCompatActivity {
    22.  
    23.     final String myWebpage = "http://sites.google.com/site/androidersite/text.txt";
    24.  
    25.     @Override
    26.     protected void onCreate(Bundle savedInstanceState) {
    27.         super.onCreate(savedInstanceState);
    28.         setContentView(R.layout.activity_main);
    29.         String finalString;
    30.         Log.d("Starting App: " , "Now");
    31.         DownloadFile df = new DownloadFile();
    32.         Log.d("Running Method " , "1");
    33.         df.execute(myWebpage);
    34.  
    35.  
    36.  
    37.     }
    38.  
    39.  
    40.  
    41.  
    42. }
    43.  

    Thank you for any help with this.
     


  2. Best Answer:
    Post #2 by Deleted User, Feb 3, 2016 (3 points)

    1. Download the Forums for Android™ app!


      Download

       
  3. HeavensSentSword

    HeavensSentSword Well-Known Member
    Thread Starter

    Awesome @LV426 , thank you I think I just had the input stream running around to much and it lost it some where bu now its working and the code you pointed me to cleaned up my code a lot.
     
Loading...
Similar Threads - InputStream text file
  1. SN95GT50
    Replies:
    5
    Views:
    90
  2. Nikolai Savulkin
    Replies:
    0
    Views:
    142
  3. Luke Aiden
    Replies:
    10
    Views:
    249
  4. chaidragonfire
    Replies:
    9
    Views:
    324
  5. dacks8989
    Replies:
    0
    Views:
    268
  6. rupalyakterbd
    Replies:
    0
    Views:
    167
  7. Wuxin
    Replies:
    12
    Views:
    462
  8. App Update
    Replies:
    0
    Views:
    192
  9. Kodesense
    Replies:
    1
    Views:
    240
  10. Lsepolis123
    Replies:
    1
    Views:
    1,290

Share This Page

Loading...