Question

I wrote a simple method to get some string data using the apache HTTPClient, however it returns a blank screen when I run it on my phone; I'm trying to display the data in a textarea. The manifest has the internet permission. Can anybody point out what I could be doing wrong?

public String getInternetData() throws Exception
{
    BufferedReader in = null;
    String data = null;
    try
    {
        HttpClient client = new DefaultHttpClient();
        URI website = new URI("http://www.yahoo.com");
        HttpGet request = new HttpGet();
        request.setURI(website);
        HttpResponse response = client.execute(request);
        in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        StringBuffer sb = new StringBuffer("");
        String l = "";
        String nl = System.getProperty("line.separator");
        while((l=in.readLine())!=null){
            sb.append(l+nl);
        }
        in.close();
        data = sb.toString();
        return data;
    }finally{
        if(in !=null){
            try{
                in.close();
                return data;
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}

for completeness, I am adding the code I use to see the data.

public class MainActivity extends Activity {

TextView httpStuff;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    httpStuff = (TextView) findViewById(R.id.tvHttp);
    GetMethodEx test = new GetMethodEx();
    String returned;
    try {
        returned = test.getInternetData();
        httpStuff.setText(returned);

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}
Was it helpful?

Solution

You are probably getting Network on UI thread error, and because it is in a try catch, the app isnt crashing

you are supposed to invoke network operations only in background threads, or AsyncTasks which are great for that

there is nothing wrong with your HTTP client and request data, the problem indeed lies in your onCreate method

try rewriting it this way

   public class MainActivity extends Activity {

TextView httpStuff;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    httpStuff = (TextView) findViewById(R.id.tvHttp);


    AsyncTask<Void,Void,String>getDataTask = new AsyncTask<Void,Void,String>(){
      @Override
      protected String doInBackground(Void... params){
        GetMethodEx test = new GetMethodEx();
        String returned = test.getInternalData();
        return returned;

       }
      @Override
      protected void onPostExecute(String returned){
      httpStuff.setText(returned);
}

}



}
      getDataTask.execute();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top