-2

我想知道我的代码在 android 2.2 和 3.2 上运行得非常好,但在 android 4.x 上总是崩溃。

android最新版本如何解决?

search_btn.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                EditText search_text = (EditText) findViewById(R.id.SearchBox);
                String search_txt_enter = search_text.getText().toString();

                if(search_txt_enter.equals(""))
                {
                    Toast.makeText(getBaseContext(),"All field are required",Toast.LENGTH_SHORT).show();
                }
                else
                {
                    try
                    {
                        HttpClient httpclient = new DefaultHttpClient();
                        HttpPost httppost = new HttpPost("http://mobile.xxxxx.com/search.php");

                        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                        nameValuePairs.add(new BasicNameValuePair("search", search_txt_enter.trim()));
                        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                        HttpResponse response = httpclient.execute(httppost);

                        InputStream inputStream = response.getEntity().getContent();
                        inputStream.close();

                    }
                    catch (ClientProtocolException e)
                    {
                     // TODO Auto-generated catch block
                    }
                    catch (IOException e)
                    {
                     // TODO Auto-generated catch block

                    }
                }
            }
        });
4

2 回答 2

1

您必须使用 AsyncTask.. 网络连接必须始终在其他线程中进行..

以下是您的问题的示例:

例如 onCreate:

search_btn.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                EditText search_text = (EditText) findViewById(R.id.SearchBox);
                String search_txt_enter = search_text.getText().toString();
    if(search_txt_enter.equals(""))
                    {
                        Toast.makeText(getBaseContext(),"All field are required",Toast.LENGTH_SHORT).show();
                    }else{
                        new YourTask().execute(search_txt_enter); // start the AsyncTask
                   }


}
}

您的班级中的以下代码:

 private class YourTask extends AsyncTask<String, Void, String> {
            @Override
            protected String doInBackground(String... s) {

                //Here you have to make the loading / parsing tasks
                //Don't call any UI actions here. For example a Toast.show() this will couse Exceptions
                // UI stuff you have to make in onPostExecute method

                    try
                    {
                        HttpClient httpclient = new DefaultHttpClient();
                        HttpPost httppost = new HttpPost("http://mobile.xxxxx.com/search.php");

                        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                        nameValuePairs.add(new BasicNameValuePair("search", s[0].trim())); //The String is in the first index!
                        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                        HttpResponse response = httpclient.execute(httppost);

                        InputStream inputStream = response.getEntity().getContent();
                        inputStream.close();

                    }
                    catch (ClientProtocolException e)
                    {
                     // TODO Auto-generated catch block
                    }
                    catch (IOException e)
                    {
                     // TODO Auto-generated catch block

                    }



            }

            @Override
            protected void onPreExecute() {
                // This method will called during doInBackground is in process
                // Here you can for example show a ProgressDialog
            }

            @Override
            protected void onPostExecute(Long result) {
                // onPostExecute is called when doInBackground finished
                // Here you can for example fill your Listview with the content loaded in doInBackground method

            }

    }
于 2013-05-24T13:29:47.203 回答
0

面向网络的任务必须在 asyncTask 中完成。在以前的版本中,他们只给出建议,Bt 在新版本中会通过错误。尝试在 asyncTask 的 doinBackground 中编写与网络相关的代码

public class TestAsyncTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected Boolean doInBackground(String... params) {
 try
                    {
                        HttpClient httpclient = new DefaultHttpClient();
                        HttpPost httppost = new HttpPost("http://mobile.xxxxx.com/search.php");

                        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                        nameValuePairs.add(new BasicNameValuePair("search", search_txt_enter.trim()));
                        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                        HttpResponse response = httpclient.execute(httppost);

                        InputStream inputStream = response.getEntity().getContent();
                        inputStream.close();

                    }
                    catch (ClientProtocolException e)
                    {
                     // TODO Auto-generated catch block
                    }
}}

这将起作用。从您的 buttonClick 调用此 assyncTask。

button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                        TestAsyncTask testAsyncTask = new TestAsyncTask();
            testAsyncTask.execute();
            }
        });
于 2013-05-24T13:20:54.163 回答