-1

I found online that we can use HttpClient and HttpGet objects to download the source code of a page using java. But, whenever I try to do so, my IDE(Eclipse) asks me to surround some lines of code with a try/catch block and it won't compile unless I do so. But then again, when I surround those lines with a try/catch block some variables go out of scope raising another error. Here's what's happening -

package com.example.prs;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class PrsActivity extends Activity {

private TextView title;
private String url = "http://www.google.com";
private String pageHtml;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_prs); 
    title = (TextView) findViewById(R.id.title); 
    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet(url);
    HttpResponse response = client.execute(request); // asks to add try/catch block here

    String html = "";
    InputStream in = response.getEntity().getContent(); //asks to add try/catch block here
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    StringBuilder str = new StringBuilder();
    String line = null;
    while((line = reader.readLine()) != null)     // asks to add try/catch block here
    {
        str.append(line);
    }
    in.close(); // asks to add try/catch block here
    html = str.toString();
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_prs, menu);
    return true;
    }
  }

and when I add the try/catch block, these errors show up -

package com.example.prs;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class PrsActivity extends Activity {

private TextView title;
private String url = "http://www.google.com";
private String pageHtml;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_prs); 
    title = (TextView) findViewById(R.id.title); 
    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet(url);
    HttpResponse response;
    try {
        response = client.execute(request);
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    String html = "";
    InputStream in;
    try {
        in = response.getEntity().getContent();     //"response" goes out of scope
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    StringBuilder str = new StringBuilder();
    String line = null;
    try {
        while((line = reader.readLine()) != null)
        {
            str.append(line);
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        in.close();     //"in" goes out of scope
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    html = str.toString();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu`enter code here`.activity_prs, menu);
    return true;
   }
 }

Please help me to fix this problem. Thanks in advance.

Swap
  • 480
  • 2
  • 7
  • 21
  • 1
    Besides your compilation error, you will have a `NetworkOnMainThreadException` because you'll do some HTTP stuff on the UI thread. So please look at `AsyncTask`. – Alexis C. Jan 03 '14 at 20:17

1 Answers1

0

Purely as a syntax matter, here is how you fix your Exception problem:

package com.example.prs;

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

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class PrsActivity extends Activity {

    private TextView title;
    private String url = "http://www.google.com";
    private String pageHtml;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_prs);
        title = (TextView) findViewById(R.id.title);
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet(url);

        try {
            HttpResponse response = client.execute(request); // asks to add
                                                                // try/catch
                                                                // block here

            String html = "";
            InputStream in = response.getEntity().getContent(); // asks to add
                                                                // try/catch
                                                                // block here
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(in));
            StringBuilder str = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) // asks to add try/catch
                                                        // block here
            {
                str.append(line);
            }
            in.close(); // asks to add try/catch block here
            html = str.toString();
        } catch (IOException err) {
            Log.e(PrsActivity.class.getSimpleName(), err.toString(), err);
        }
    }
}

However, as ZouZou pointed out in his comment, this still won't work because you are trying to execute a network request in your main thread. You need to create an AsyncTask and execute the network request there.

See this answer on how to create an AsyncTask.

Community
  • 1
  • 1
David S.
  • 6,567
  • 1
  • 25
  • 45