android的联网操作 HttpURLConnection 和 Apache HttpClient

泡在网上的日子 / 文 发表于2013-07-11 23:58 次阅读 HttpURLConne

本文介绍android中如何编写一个进行联网操作的简单应用,我们应该遵循的其中的基本步骤,这是谷歌工程司提倡的最佳实践方法。

联网操作需要在应用的manifest 文件中添加如下权限:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

选择一个http client

绝大多数与网络有关的app都是通过http协议来获得和发送数据的。android中包含两种http client:HttpURLConnection和Apache HttpClient两者皆支持https,上传和下载,IPv6,以及连接池。我们推荐使用HttpURLConnection

检查网络连接状态

在你打算连接网络之前,需要通过 getActiveNetworkInfo()isConnected()来检查网络的连接是否可用,因为用户可能会远离网络环境,或者同时关闭了wifi和移动数据连接。

public void myClickHandler(View view) {
    ...
    ConnectivityManager connMgr = (ConnectivityManager)
        getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
    if (networkInfo != null && networkInfo.isConnected()) {
        // fetch data
    } else {
        // display error
    }
    ...
}

将网络操作放在单独的线程中处理

网络操作一般会有延迟现象,为了防止延迟造成糟糕的用户体验,我们总是把联网的操作放在UI线程之外的独立线程中。AsyncTask 类提供了一种建立独立任务的最简单的方式。

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);
    }
    // When user clicks button, calls AsyncTask.
    // Before attempting to fetch the URL, makes sure that there is a network connection.
    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.");
        }
    }
     // Uses AsyncTask to create a task away from the main UI thread. This task takes a
     // URL string and uses it to create an HttpUrlConnection. Once the connection
     // has been established, the AsyncTask downloads the contents of the webpage as
     // an InputStream. Finally, the InputStream is converted into a string, which is
     // displayed in the UI by the AsyncTask's onPostExecute method.
     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);
       }
    }
    ...
}

连接并下载数据

在你处理网络数据的线程中,你可以使用HttpURLConnection来请求和下载数据。在调用了connect()方法之后,你就能通过getInputStream()方法获得数据流InputStream


// Given a URL, establishes an HttpUrlConnection and retrieves
// the web page content as a InputStream, which it returns as
// a string.
private String downloadUrl(String myurl) throws IOException {
    InputStream is = null;
    // Only display the first 500 characters of the retrieved
    // web page content.
    int len = 500;
                                                                                                                                                                             
    try {
        URL url = new URL(myurl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(10000 /* milliseconds */);
        conn.setConnectTimeout(15000 /* milliseconds */);
        conn.setRequestMethod("GET");
        conn.setDoInput(true);
        // Starts the query
        conn.connect();
        int response = conn.getResponseCode();
        Log.d(DEBUG_TAG, "The response is: " + response);
        is = conn.getInputStream();
        // Convert the InputStream into a string
        String contentAsString = readIt(is, len);
        return contentAsString;
                                                                                                                                                                             
    // Makes sure that the InputStream is closed after the app is
    // finished using it.
    } finally {
        if (is != null) {
            is.close();
        }
    }
}

将InputStream转换成String

InputStream是一个可读的字节数据源,一旦你获得了一个InputStream,通常你会将其转换成一种目标字节数据。比如你在下载一张图片,你可能会将其解码然后这样展示:

InputStream is = null;
...
Bitmap bitmap = BitmapFactory.decodeStream(is);
ImageView imageView = (ImageView) findViewById(R.id.image_view);
imageView.setImageBitmap(bitmap);

在本文的例子中,InputStream代表的是一个网页的文本数据,因此我们是将其转换成String,以显示在activity的UI界面上。

// Reads an InputStream and converts it to a String.
public String readIt(InputStream stream, int len) throws IOException, UnsupportedEncodingException {
    Reader reader = null;
    reader = new InputStreamReader(stream, "UTF-8");       
    char[] buffer = new char[len];
    reader.read(buffer);
    return new String(buffer);
}



收藏 赞 (0) 踩 (0)
上一篇:android拍照的简单方式:调用已存在的camera应用
如果你的app中有要拍照的需求,有两种方式可以实现,一是直接调用已安装的app,二是自己写一个拍照界面。这篇文章讲解最简单的方式,直接调用已有的camera应用。 获取camera权限 如果你的应用必须要用到拍照的功能,那么你需要在google play上将可以下载该应
下一篇:如何让android的actionbar浮动且透明
如上图所示,谷歌地图的actionbar是透明的,且浮动在整个布局之上,没有占用布局空间。其实要做到这样的效果,我们首先想到的是两个方面: 1.将让actionbar浮动起来。 2.给actionbar一个背景,可以为颜色也可以为图片。 下面我以背景色为颜色举个例子。 getW