如何在 HttpURLConnection 中添加 AsyncTask?

编程入门 行业动态 更新时间:2024-10-18 06:00:29
本文介绍了如何在 HttpURLConnection 中添加 AsyncTask?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在建立服务器连接,我的问题是我需要在我的代码上放置一个 AsyncTask,因为它在 sdk 版本 10 上不起作用.我不想使用 StrictMode.ThreadPolicy.

I'm establishing a server connection, my problem is that I need to put an AsyncTask on my code, because its not working in sdk version 10 up. I don't want to use the StrictMode.ThreadPolicy.

public class TestConnection extends Activity { @Override public void onCreate(Bundle cbundle) { super.onCreate(cbundle); ConnectivityManager aConnectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo aNetworkInfo = aConnectivityManager.getActiveNetworkInfo(); if (aNetworkInfo != null && aNetworkInfo.isConnected()){ Toast.makeText(this, "Internet Connected", Toast.LENGTH_LONG).show(); }else{ Toast.makeText(this, "Internet Connection Timeout", Toast.LENGTH_LONG).show(); } URL aURL; /* Will be filled and displayed later. */ String aString = null; /* We will show the data we read in a TextView. */ TextView aTextView = new TextView(this); try { /* Define the URL we want to load data from. */ aURL = new URL( "url"); /* Open a connection to that URL. */ final HttpURLConnection aHttpURLConnection = (HttpURLConnection) aURL.openConnection(); /* Define InputStreams to read from the URLConnection. */ InputStream aInputStream = aHttpURLConnection.getInputStream(); BufferedInputStream aBufferedInputStream = new BufferedInputStream( aInputStream); /* Read bytes to the Buffer until there is nothing more to read(-1) */ ByteArrayBuffer aByteArrayBuffer = new ByteArrayBuffer(50); int current = 0; while ((current = aBufferedInputStream.read()) != -1) { aByteArrayBuffer.append((byte) current); } /* Convert the Bytes read to a String. */ aString = new String(aByteArrayBuffer.toByteArray()); } catch (Exception aException) { /* On any Error we want to display it. */ aString = aException.getMessage(); } /* Show the String on the GUI. */ aTextView.setText(aString); this.setContentView(aTextView); } }

推荐答案

private class ConnectionTask extends AsyncTask<String, Void, String>{ @Override protected byte[] doInBackground(String... urls) { try { aURL = new URL( urls[0]); /* Open a connection to that URL. */ final HttpURLConnection aHttpURLConnection = (HttpURLConnection) aURL.openConnection(); /* Define InputStreams to read from the URLConnection. */ InputStream aInputStream = aHttpURLConnection.getInputStream(); BufferedInputStream aBufferedInputStream = new BufferedInputStream( aInputStream); /* Read bytes to the Buffer until there is nothing more to read(-1) */ ByteArrayBuffer aByteArrayBuffer = new ByteArrayBuffer(50); int current = 0; while ((current = aBufferedInputStream.read()) != -1) { aByteArrayBuffer.append((byte) current); } /* Convert the Bytes read to a String. */ aString = new String(aByteArrayBuffer.toByteArray()); } catch (IOException e) { Log.d(TAG, e.toString()); } return aString; } @Override protected void onPostExecute(String result) { // result is what you got from your connection aTextView.setText(result); } }

如何称呼:

ConnectionTask task = new ConnectionTask(); String[] params = new String[2]; params[0] = url; params[1] = somethingelseifneeded; task.execute(params);

更多推荐

如何在 HttpURLConnection 中添加 AsyncTask?

本文发布于:2023-07-14 19:54:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1106669.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   HttpURLConnection   AsyncTask

发布评论

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

>www.elefans.com

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