How to create Http Connection using AsyncTask class?
2011-12-06 18:40
429 查看
问:
I am trying to create HTTP connection using AsyncTask class.
Is it possible to create HTTP connection ?
Can you suggest sample source code ?
Thanks in advance.
回答:
As an inner class inside your activity :
Then somewhere in your activity :
I am trying to create HTTP connection using AsyncTask class.
Is it possible to create HTTP connection ?
Can you suggest sample source code ?
Thanks in advance.
回答:
As an inner class inside your activity :
public final class HttpTask extends AsyncTask<String/* Param */, Boolean /* Progress */, String /* Result */> { private HttpClient mHc = new DefaultHttpClient(); @Override protected String doInBackground(String... params) { publishProgress(true); // Do the usual httpclient thing to get the result return result; } @Override protected void onProgressUpdate(Boolean... progress) { // line below coupled with // getWindow().requestFeature(Window.FEATURE_INDETERMINATE_PROGRESS) // before setContentView // will show the wait animation on the top-right corner MyActivity.this.setProgressBarIndeterminateVisibility(progress[0]); } @Override protected void onPostExecute(String result) { publishProgress(false); // Do something with result in your activity } }
Then somewhere in your activity :
new HttpTask().execute(someParams...);
相关文章推荐
- How To Create an ASP.NET HTTP Handler by Using Visual C# .NET
- How to Make an HTTP Connection Using TCP/IP with RSocket
- Java Tutorial: How to Create RESTful Java Client using Apache HttpClient – Example
- How to Make an HTTP Connection Using TCP/IP with RSocket
- How to Make an HTTP Connection Using TCP/IP with RSocket
- How to Make an HTTP Connection Using TCP/IP with RSocket
- How to create a SharePoint List using Excel 2003(转贴)
- [Ruby]How to create singleton class ?
- how to create colored 3D Objects using the OpenGL® ES cross-platform API.
- How to enable wire logging for a java HttpURLConnection traffic?
- how to create images using gdal
- How-to create a Calculator Using Google Android: Part II
- How to create a UIView using a Xib
- [转]How to create a Windows service by using Sc.exe
- How to load a local .CSS file & JavaScript resources using iPhone UIWebView Class
- How to cancel a persistent connection using NSURLConnection?
- How to Create Modifiers Using the API QP_MODIFIERS_PUB.PROCESS_MODIFIERS
- How to Create WPF Docking Windows Using Code Only
- HowToCreateTollFreeBridgedClass
- java.lang.ClassCastException: libcore.net.http.HttpURLConnectionImpl cannot be cast to javax.net.ssl