Android App不会在try块中运行代码

时间:2015-01-16 19:50:44

标签: java android android-studio

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块中的代码甚至没有被处理/运行(?)。我做错了什么?

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

}

1 个答案:

答案 0 :(得分:1)

评论提到“问题”而没有真正描述它。您无法在运行httpClient.execute的UI线程上执行onCreate

android httpclient.execute exception

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

编辑:

如果您熟悉多线程和利益/陷阱,也可以使用后台线程。这实际上就是我经常执行的任务,因为AsyncTask有自己的好处/陷阱。