HttpURLConnection,HttpClient,Volley

继承关系:
java.lang.Object
-java.net.URLConnection
-java.net.HttpURLConnection
HttpURLConnection继承自URLConnection,可以使用HTTP协议发送接收网络数据。可以接收发送提前不知道长度的数据。

HttpURLConnection的使用

其使用方式参考如下模式:
1.通过调用 URL.openConnection() 方法获得一个 URLConnection,并将其转为HttpURLConnection对象。
2.准备request请求,request的首要属性是它的URI。request 头还可以包括例如整数,首选内容类型,会话cookies等这些元数据。
3.也可以选择上传request body.(必须设定setDoOutput(true)),然后通过URLConnection.getOutputStream()返回的 OutputStream 发送出去。
4.读取response。response header一般包含一些元数据,比如response body的内容类型和长度,修改时间,会话cookies。response body可以通过URLConnection.getInputStream()方法返回的InputStream中读取。如果response没有body。URLConnection.getInputStream()返回一个空的stream。
5.关闭连接,一旦response body被读取完毕,HttpURLConnection应该通过disconnect()方法来关闭,从而释放连接所持有的资源。

典型示例代码:

URL url = new URL("http://www.Android.com/"); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); try { InputStream in = new BufferedInputStream(urlConnection.getInputStream()); readStream(in); } finally { urlConnection.disconnect(); }

1

代码中并没有步骤3.

通过HttpURLConnection简单实现下载网络图片并展示在ImageView

通过上面的例子可以看出,我们可以通过URI来得到网络上的图片(例如https://www.linuxidc.com/img/bd_logo1.png)。并通过InputStream得到得到图像数据,用BitmapFactory解析。最后在ImageView中绘制图像。

注意:由于网络操作耗时较大,不要放在主线程,这里本例将其放在AsyncTask中来做。

1

示例代码:

package com.example.hurlconnectionpic; import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import android.os.AsyncTask; import android.os.Bundle; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends Activity { ImageView iv; Button bt; String linuxidcImage = "http://www.linuxidc.com/img/bd_logo1.png"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv = (ImageView) findViewById(R.id.imageView1); bt = (Button) findViewById(R.id.button1); bt.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { new BackGroundTast().execute(linuxidcImage); } }); } class BackGroundTast extends AsyncTask<String, Void, Bitmap> { Bitmap bitmap; HttpURLConnection urlConnection; InputStream in; @Override protected Bitmap doInBackground(String... params) { try { URL url = new URL(params[0]); urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setDoInput(true); urlConnection.connect(); in = urlConnection.getInputStream(); bitmap = BitmapFactory.decodeStream(in); in.close(); } catch (IOException e) { e.printStackTrace(); } finally { urlConnection.disconnect(); } return bitmap; } @Override protected void onPostExecute(Bitmap result) { super.onPostExecute(result); if (result != null) { iv.setImageBitmap(result); } } @Override protected void onPreExecute() { // TODO Auto-generated method stub super.onPreExecute(); } } }

1

Layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="@string/hello_world" /> <Button android:id="@+id/button1" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="20dp" android:text="GetImage" /> </RelativeLayout>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/bbb2a0b66b66e00712b3a71d4a856c84.html