在Android中,使用http协议的编程工作是比较简单的,androidSDK中已经集成了Apache的HttpClient模块。使用HttpClient可以快速开发出功能强大的Http程序。不过一般说来,要开发与Internet连接的程序,最基础的还是使用HttpURLConnection。
安卓intent用法下面是一个使用HttpURLConnection对象从互联网上读取数据的Demo:
第一个Demo是从互联网上读取某个图片:
首先在Eclipse中新建一个工程ImageView。而后新建一个Activity名称为MainActivity:
q.image;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.HttpURLConnection;
import java.URL;
import android.app.Activity;
aphics.Bitmap;
aphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
private EditText pathText;
private ImageView imageView;
@Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.main);
pathText = (EditText) this.findViewById(R.id.urlpath);
imageView = (ImageView) this.findViewById(R.id.imageView);
Button button = (Button) this.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String path = Text().toString();
try {
byte[] data = getImage(path);
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0,
data.length);
imageView.setImageBitmap(bitmap);
} catch (Exception e) {
Toast.makeText(MainActivity.this, , 1).show();
Log.e(TAG, e.toString());
}
}
});
}
public static byte[] getImage(String path) throws Exception {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5 * 1000);
InputStream inStream = InputStream();
return readFromInput(inStream);
}
public static byte[] readFromInput(InputStream inStream) throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = ad(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
inStream.close();
ByteArray();
}
}
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="schemas.android/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/urlpath" />
<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="www.baidu/img/baidu_sylogo1.gif"
android:id="@+id/urlpath" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="@string/button"
android:id="@+id/button" />
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/imageView" />
</LinearLayout>
最后一项要注意的地方是,要运行该工程,必须在l文件中加上访问网络的权限:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="schemas.android/apk/res/android"
package="q.image" android:versionCode="1" android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="8" />
<!-- 访问网络的权限 -->
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
结果:
第二个例子是一个Html代码查看器,主要为演示如何从互联网上获取网页数据:
同样,在Eclipse中新建一个工程,创建启动Activity,命名为MainActivity:
q.htmlcode;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.HttpURLConnection;
import java.URL;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.main);
TextView textView = (TextView) this.findViewById(View);
try {
textView.setText(getHtmlCode("www.sohu"));
} catch (Exception e) {
Log.e("MainActivity", e.toString());
Toast.makeText(MainActivity.this, "网络连接失败", 1).show();
}
}
public static String getHtmlCode(String path) throws Exception {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5 * 1000);
InputStream inStream = InputStream();
byte[] data = readFromInput(inStream);
String html = new String(data, "gbk");
return html;
}
public static byte[] readFromInput(InputStream inStream) throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = ad(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
inStream.close();
ByteArray();
}
}
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="schemas.android/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/textView"
/>
</ScrollView>
</LinearLayout>
结果如图:
要注意加上ScrollView标签,否则,当页面数据比较大时,不能出现滚动条,影响使用。另外,同样要在l文件中添加访问网络的权限,见上例。
这两个例子本身并没有实际的应用价值,只是展示了如何使用HttpURLConnection对象实现联网,并从互联网上读取图片、网页文件等数据。使用HttpURLConnection对象进行网络编程的核心过程也就是如此了。当然,HttpURLConnection对象还有很多方法,可以设置访问访问请求头字段等信息。感兴趣的朋友可以发送邮件到我的邮箱,共同探讨。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论