gpt4 book ai didi

java - Android 应用程序不会在 try block 内运行代码

转载 作者:行者123 更新时间:2023-12-01 12:00:47 25 4
gpt4 key购买 nike

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 block 中使用以下代码无济于事后,我将其放入方法中。在调试过程中,我意识到 try/catch block 内的代码甚至没有被处理/运行(?)。我做错了什么?

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

}

最佳答案

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

android httpclient.execute exception

您认为代码没有执行但它似乎正在执行的原因是您捕获异常。检查您的 logcat,然后阅读 AsyncTask

编辑:

如果您熟悉多线程及其优点/缺点,也可以使用后台线程。这实际上就是我通常执行任务的方式,因为 AsyncTask 有它自己的优点/陷阱。

关于java - Android 应用程序不会在 try block 内运行代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27991714/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com