哪里可以使用AsyncTask,android网络连接找到http返回的数据(Where to find http returned data using AsyncTask, android net

编程入门 行业动态 更新时间:2024-10-20 17:30:50
哪里可以使用AsyncTask,android网络连接找到http返回的数据(Where to find http returned data using AsyncTask, android network connection)

我正在使用developer.android.com上的代码来阅读网站,但我不了解如何在myClickHandler方法中访问数据。 该程序工作和抓取数据很好,但我只能在DownloadWebpageTask类中看到它。 这是代码部分:

那么在myClickHandler中我可以访问onPostExecute生成的数据吗? 谢谢你的帮助。

public class HttpExampleActivity extends Activity { private static final String DEBUG_TAG = "HttpExample"; private EditText urlText; private TextView textView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); urlText = (EditText) findViewById(R.id.myUrl); textView = (TextView) findViewById(R.id.myText); } public void myClickHandler(View view) { // Gets the URL from the UI's text field. String stringUrl = urlText.getText().toString(); ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); if (networkInfo != null && networkInfo.isConnected()) { new DownloadWebpageTask().execute(stringUrl); } else { textView.setText("No network connection available."); } } private class DownloadWebpageTask extends AsyncTask<String, Void, String> { @Override protected String doInBackground(String... urls) { // params comes from the execute() call: params[0] is the url. try { return downloadUrl(urls[0]); } catch (IOException e) { return "Unable to retrieve web page. URL may be invalid."; } } // onPostExecute displays the results of the AsyncTask. @Override protected void onPostExecute(String result) { textView.setText(result); } } ...

}

I'm using code straight from developer.android.com to read a website but i'm not understanding how to access the data within the myClickHandler method. The program works and grabs the data fine, but I can only see it in the DownloadWebpageTask class. Here is the code section:

So where in myClickHandler can I access the data onPostExecute produces? Thanks for any help.

public class HttpExampleActivity extends Activity { private static final String DEBUG_TAG = "HttpExample"; private EditText urlText; private TextView textView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); urlText = (EditText) findViewById(R.id.myUrl); textView = (TextView) findViewById(R.id.myText); } public void myClickHandler(View view) { // Gets the URL from the UI's text field. String stringUrl = urlText.getText().toString(); ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); if (networkInfo != null && networkInfo.isConnected()) { new DownloadWebpageTask().execute(stringUrl); } else { textView.setText("No network connection available."); } } private class DownloadWebpageTask extends AsyncTask<String, Void, String> { @Override protected String doInBackground(String... urls) { // params comes from the execute() call: params[0] is the url. try { return downloadUrl(urls[0]); } catch (IOException e) { return "Unable to retrieve web page. URL may be invalid."; } } // onPostExecute displays the results of the AsyncTask. @Override protected void onPostExecute(String result) { textView.setText(result); } } ...

}

最满意答案

请访问此处: http : //developer.android.com/reference/android/os/AsyncTask.html以阅读有关Async Task的更多信息。

对于你的问题:我可以访问onPostExecute产生的数据吗? Ans:是的,您可以访问onPostExecute,因为它在doInBackground之后在UI线程上运行。

onPostExecute是否必须等待doInBackground才能完成? 答:是的

AsyncTask有四个步骤:

doInBackground:执行长时间运行操作的代码在此方法中。 当单击按钮执行onClick方法时,它调用execute方法接受参数并自动调用doInBackground方法并传递参数。 onPostExecute:在doInBackground方法完成处理后调用此方法。 来自doInBackground的结果将传递给此方法。

onPreExecute:在调用doInBackground方法之前调用此方法。

onProgressUpdate:通过从doInBackground调用此方法调用publishProgress来调用此方法

Visit here: http://developer.android.com/reference/android/os/AsyncTask.html to read more about Async Task.

For your question: can I access the data onPostExecute produces? Ans: Yes, you can access onPostExecute as it Runs on the UI thread after doInBackground.

Does onPostExecute have to wait for doInBackground to complete? Ans: Yes

AsyncTask has four steps:

doInBackground: Code performing long running operation goes in this method. When onClick method is executed on click of button, it calls execute method which accepts parameters and automatically calls doInBackground method with the parameters passed. onPostExecute: This method is called after doInBackground method completes processing. Result from doInBackground is passed to this method.

onPreExecute: This method is called before doInBackground method is called.

onProgressUpdate: This method is invoked by calling publishProgress anytime from doInBackground call this method

更多推荐

本文发布于:2023-08-01 02:46:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1352859.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:可以使用   数据   网络   android   connection

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!