Android 应用程序不会 运行 try 块中的代码

Android App won't run code inside of try block

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();
    }

以上是我使用的第一种方法。

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;
        }
    }
}

在 try catch 块中使用以下代码无济于事后,我将其放入方法中。 在调试期间,我意识到 try/catch 块中的代码甚至不是 processed/ran(?)。我做错了什么?

e: 第一个示例是在 onCreate 方法中尝试的,第二个示例是在按下按钮时调用的。

完整代码;

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();
    }

}

一条评论提到了 "problem" 但没有真正描述它。您不能在运行 onCreate 的 UI 线程上执行 httpClient.execute

您认为代码未执行但它似乎正在执行的原因是您 catch 异常。检查您的 logcat,然后继续阅读 AsyncTask

编辑:

如果您熟悉多线程和 benefits/pitfalls,您也可以使用后台线程。这实际上是我通常做任务的方式,因为 AsyncTask 有自己的 benefits/pitfalls.