android程序的意外行为

时间:2011-04-03 20:50:29

标签: android webview

我想在Google搜索按钮后面运行脚本,这意味着...将表单发布到Google服务器并以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或程序本身有什么问题吗? becoz它似乎是与互联网的连接完成但我使用适当的许可.. 谢谢..

1 个答案:

答案 0 :(得分:4)

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

请参阅Painless Threading

相关问题