我看到很多这些问题的遗嘱(这里,这里,这里和这里),但我没有得到解决我的问题.
这是我的应用程序的想法
从URL中获取内容并在每次单击按钮时使用textview显示它.
(URL内容将为每次刷新随机生成不同的文本)
这是我的应用程序的旧代码完美地工作但StackOverflow人员建议我使用AsyncTask
我在doInBackground()方法中指定了获取URL内容操作,并在onCreate的 Button Click Event中调用了new myAsyncTask.execute().
它只适用于第一次点击,然后我的应用程序崩溃了.它说AsyncTask应该只调用一次.但我希望我的TextView在每个按钮点击事件上更新.
那么,我现在应该怎么做
我是否要取消之前的AsyncTask并调用新的(或)我想为AsyncTask初始化新对象并再次调用它(这是最糟糕的方法)
如何使用AsyncTask或任何Alternate在我的应用程序中实现我的想法?
这是我的代码:
public class MainActivity extends AppCompatActivity { TextView resultView;String TAG="MainActivity kbt"; SmoothProgressBar mProgressBar; Context context; MyAsyncTask myAsyncTask; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // StrictMode.enableDefaults(); //Used to executes URL connection on MainThread without getting Exception setContentView(R.layout.activity_main); mProgressBar = (SmoothProgressBar) findViewById(R.id.progressBar); StrictMode.enableDefaults(); resultView = (TextView) findViewById(R.id.result); myAsyncTask=new MyAsyncTask(); // mProgressBar.setVisibility(SmoothProgressBar.INVISIBLE); Button insert=(Button) findViewById(R.id.button); insert.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // TODO Auto-generated method stub myAsyncTask.execute(); } }); } private class MyAsyncTask extends AsyncTask{ String mTAG = "myAsyncTask"; @Override protected void onPreExecute() { mProgressBar.progressiveStart(); } @Override protected Void doInBackground(Void... arg) { try { getData(); } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(Void a) { mProgressBar.progressiveStop(); } } public void getData() throws IOException { String result = ""; InputStream isr = null; URLConnection urlConnection = null; URL url = new URL("http://kbtganesh.16mb.com/index.php"); Log.d(TAG,"After URL NOT WORKING"); //AFTER THIS.... NEXT LOG (ie GOT URL CONNETION) IS NOT PRINTED urlConnection = url.openConnection(); Log.d(TAG, "GOT URL CONNETION"); Log.d(TAG, "GOT URL CONNECTED"); isr =urlConnection.getInputStream(); Log.d(TAG,"AFTER ISR EQ"); //convert response to string try { // BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8); BufferedReader reader = new BufferedReader(new InputStreamReader(isr)); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } isr.close(); result=sb.toString(); } catch(Exception e){ Log.e("log_tag", "Error converting result "+e.toString()); } //parse json data try { resultView.setText(result); } catch(Exception e) { Log.e("log_tag", "Couldn't set text, damnit."); } } }
提前致谢... (:
最后,我在StackOverflow朋友的帮助下解决了我的问题.
移动myAsyncTask=new MyAsyncTask();
内部onClick()
方法解决了我的问题.这是多次执行AsyncTask的最佳方法.
AsyncTask实例在执行完后最终将被垃圾收集.因此我们不需要担心创建多个实例.它们将自动进入.
MyAsyncTask myAsyncTask; insert.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // TODO Auto-generated method stub myAsyncTask=new MyAsyncTask(); // TODO Auto-generated method stub myAsyncTask.execute(); } });