0
try {
        HttpClient httpClient = new DefaultHttpClient();
        HttpContext localContext = new BasicHttpContext();
        HttpGet httpGet = new HttpGet(wyvernBones); //"http://forums.zybez.net/runescape-2007-prices/3104-wyvern-bones"
        HttpResponse response = httpClient.execute(httpGet, localContext);
        String result = "";
        BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        fetched = false;
        progBox.show();
        while (!fetched) {
            line = br.readLine();
            if (line.contains("Average Sell Offer")) {
                Toast.makeText(this, line, Toast.LENGTH_LONG).show();
                progBox.dismiss();
                fetched = true;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

Above is the first approach I used.

public void getItem() throws Exception
{
    HttpClient httpClient = new DefaultHttpClient();
    HttpContext localContext = new BasicHttpContext();
    HttpGet httpGet = new HttpGet(wyvernBones); //"http://forums.zybez.net/runescape-2007-prices/3104-wyvern-bones"
    HttpResponse response = httpClient.execute(httpGet, localContext);
    String result = "";
    BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
    fetched = false;
    progBox.show();
    while (!fetched) {
        line = br.readLine();
        if (line.contains("Average Sell Offer")) {
            Toast.makeText(this, line, Toast.LENGTH_LONG).show();
            progBox.dismiss();
            fetched = true;
        }
    }
}

After using the following code inside of a try catch block with no avail, I instead put it into a method. During debugging, I realized the code inside the try/catch block wasn't even being processed/ran(?). What have I done wrong?

e: the first sample was attempted in the onCreate method, the second sample was called when a button was pressed.

Entire Code;

public class MainActivity extends ActionBarActivity implements OnClickListener{

Button btn1;

ProgressDialog progBox;
Boolean fetched;
String line;

URL url;

String wyvernBones = "http://forums.zybez.net/runescape-2007-prices/3104-wyvern-bones";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    progBox = new ProgressDialog(this);
    progBox.setIndeterminate(true);
    progBox.setTitle("Fetching Data..");

    btn1 = (Button) findViewById(R.id.btn1);
    btn1.setOnClickListener(this);

    try {
        HttpClient httpClient = new DefaultHttpClient();
        HttpContext localContext = new BasicHttpContext();
        HttpGet httpGet = new HttpGet(wyvernBones); //"http://forums.zybez.net/runescape-2007-prices/3104-wyvern-bones"
        HttpResponse response = httpClient.execute(httpGet, localContext);
        String result = "";
        BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        fetched = false;
        progBox.show();
        while (!fetched) {
            line = br.readLine();
            if (line.contains("Average Sell Offer")) {
                Toast.makeText(this, line, Toast.LENGTH_LONG).show();
                progBox.dismiss();
                fetched = true;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

}
miaz
  • 45
  • 1
  • 6

1 Answers1

1

A comment mentioned the "problem" without really describing it. You can't do httpClient.execute on the UI thread, which is where onCreate runs.

android httpclient.execute exception

The reason you think the code is not executing but it appears to be doing so is that you catch the exception. Check your logcat, then read up on AsyncTask

EDIT:

You can also use a background thread if you are familiar with multi-threading and the benefits/pitfalls. That is actually how I usually do tasks because AsyncTask has it's own benefits/pitfalls.

Community
  • 1
  • 1
Jim
  • 10,172
  • 1
  • 27
  • 36