gpt4 book ai didi

android - android程序的意外行为

转载 作者:行者123 更新时间:2023-11-29 16:24:09 25 4
gpt4 key购买 nike

我想在谷歌搜索按钮后面运行脚本,这意味着...将表单发布到谷歌服务器并以 html 字符串的形式获取响应,最后我将该字符串放在 webview 上以显示结果... .然后我运行代码...有时它会显示正确的结果(比如有人点击谷歌搜索按钮)但有时它会向我显示“强制关闭”的消息,而我对代码没有任何更改......这意味着关于输出的预测是意外的......我的代码是这样的……

public class url extends Activity
{
HttpResponse end = null;
String endResult = null;
WebView mWebView;
Intent myWebViewIntent;
public static final int TIMEOUT_MS=10000;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
HttpClient client = new DefaultHttpClient();

HttpConnectionParams.setConnectionTimeout(client.getParams(), TIMEOUT_MS);
HttpConnectionParams.setSoTimeout(client.getParams(), TIMEOUT_MS);
HttpPost post = new HttpPost("http://www.google.com/m");
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("hl", "en"));
pairs.add(new BasicNameValuePair("gl", "us"));
pairs.add(new BasicNameValuePair("source", "android-launcher-widget"));
pairs.add(new BasicNameValuePair("q", "persistent"));

try {
post.setEntity(new UrlEncodedFormEntity(pairs));
/* Uri uri = Uri.parse(post.toString());
TextView t1=new TextView(this);
t1.setText(post.getRequestLine().getUri());
this.setContentView(t1);*/

} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Toast msg = Toast.makeText(url.this, "Message", Toast.LENGTH_LONG);

msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2, msg.getYOffset() / 2);

msg.show();

HttpResponse response = client.execute(post);

end = response;


} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
//Toast.makeText(url.this, "problem in execute....", 20).show();
// TODO Auto-generated catch block
//put the balance is empty dialog box here.
e.printStackTrace();
}
BasicResponseHandler myHandler = new BasicResponseHandler();
try {

// Log.i("file tag","we are out of url");
endResult = myHandler.handleResponse(end);

} catch (HttpResponseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();

}

WebView engine = (WebView)findViewById(R.id.webview);
engine.loadDataWithBaseURL(post.getRequestLine().getUri(), endResult, "text/html", "UTF-8", null);
//engine.loadData(endResult, "text/html", "UTF-8");
engine.requestFocus(View.FOCUS_DOWN);


//engine.loadUrl(endResult);

}
}

所以模拟器或安装 SDK 或程序本身有什么问题....?因为它似乎与互联网的连接已经完成,但我使用了适当的许可..谢谢..

最佳答案

您收到的 ANR 是因为您在 UI 线程上进行阻塞调用。要在不阻塞 UI 线程的情况下执行阻塞操作,您必须使用线程和处理程序,或者(推荐)AsyncTask

参见 Painless Threading .

关于android - android程序的意外行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5532419/

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