cannot fetch text from a file on a website android

56 views Asked by At

I am trying to fetch text from a simple text file uploaded on a website accessible directly with the link i've used in my code but for some reason its not working. Using debug tool i found out that its throwing an exception after HttpResponse line. I have no idea how to solve this problem.(sorry for my long question, i'm new to android and stackoverflow)

package com.example.trial;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends Activity {

    DefaultHttpClient httpclient = new DefaultHttpClient();
    TextView TextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try {
            HttpGet httppost = new HttpGet(
                    "http://www.androidworkshopmit.webs.com/abc.txt");
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity ht = response.getEntity();

            BufferedHttpEntity buf = new BufferedHttpEntity(ht);

            InputStream is = buf.getContent();

            BufferedReader r = new BufferedReader(new InputStreamReader(is));

            StringBuilder total = new StringBuilder();
            String line;
            while ((line = r.readLine()) != null) {
                total.append(line + "\n");
            }
            TextView.setText(total);
        } catch (Exception e) {
            System.out.print("hi, there is some error!");
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
1

There are 1 answers

0
Shobhit Puri On BEST ANSWER

My code but for some reason its not working. Using debug tool i found out that its throwing an exception after HttpResponse line.

You are doing your the Network Operations (HTTP GET calls) on your main UI thread. Do all the network calls in the background thread using AsyncTask. Here is basic example of how to use it: How to use AsyncTask correctly in Android