編輯:Android開發實例
URLConnection是個抽象類,它有兩個直接子類分別是HttpURLConnection和JarURLConnection。來看安卓開發中HttpURLConnection網絡通信的開發實例代碼:
Xml代碼
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/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/hello" />
<Button android:text="直接獲取數據" android:id="@+id/Button01"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<Button android:text="GET方式傳遞" android:id="@+id/Button02"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<Button android:text="POST方式傳遞" android:id="@+id/Button03"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<Button android:text="獲取圖片" android:id="@+id/Button04"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
Java代碼
package com.Aina.Android;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Test extends Activity {
/** Called when the activity is first created. */
private Button btn1 = null;
private Button btn2 = null;
private Button btn3 = null;
private Button btn4 = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn1 = (Button) this.findViewById(R.id.Button01);
btn2 = (Button) this.findViewById(R.id.Button02);
btn3 = (Button) this.findViewById(R.id.Button03);
btn4 = (Button) this.findViewById(R.id.Button04);
btn1.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(Test.this, ShowData.class);
Bundle b = new Bundle();
b.putInt("id", 1);
intent.putExtras(b);
startActivity(intent);
}
});
btn2.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(Test.this, ShowData.class);
Bundle b = new Bundle();
b.putInt("id", 2);
intent.putExtras(b);
startActivity(intent);
}
});
btn3.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(Test.this, ShowData.class);
Bundle b = new Bundle();
b.putInt("id", 3);
intent.putExtras(b);
startActivity(intent);
}
});
btn4.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(Test.this, ShowData.class);
Bundle b = new Bundle();
b.putInt("id", 4);
intent.putExtras(b);
startActivity(intent);
}
});
}
}
Xml代碼
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/TextView_HTTP"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ImageView android:id="@+id/ImageView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageView>
</LinearLayout>
Java代碼
package com.Aina.Android;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
/**
* com.Aina.Android Pro_HttpURLConnection
*/
public class ShowData extends Activity {
private TextView tv = null;
private ImageView iv = null;
private Bitmap mBitmap = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.http);
Intent intent = this.getIntent();
Bundle b = intent.getExtras();
int id = b.getInt("id");
tv = (TextView) this.findViewById(R.id.TextView_HTTP);
iv = (ImageView) this.findViewById(R.id.ImageView01);
if (id == 1) {
String httpUrl = "http://192.168.0.2:8080/Android/http.jsp";
URL url = null;
try {
url = new URL(httpUrl);
} catch (MalformedURLException e) {
e.printStackTrace();
}
if (url != null) {
try {
HttpURLConnection urlConn = (HttpURLConnection) url
.openConnection();// 打開連接,此處只是創建一個實力,並沒有真正的連接
urlConn.connect();// 連接
InputStream input = urlConn.getInputStream();
InputStreamReader inputReader = new InputStreamReader(input);
BufferedReader reader = new BufferedReader(inputReader);
String inputLine = null;
StringBuffer sb = new StringBuffer();
while ((inputLine = reader.readLine()) != null) {
sb.append(inputLine).append("n");
}
reader.close();
inputReader.close();
input.close();
urlConn.disconnect();
if(sb !=null){
tv.setText(sb.toString());
}else{
tv.setText("讀取的內容:NULL");
}
} catch (IOException e) {
e.printStackTrace();
}
}else{
Log.i("TAG", "url is null");
}
}else if(id==2){
String httpUrl = "http://192.168.0.12:8080/Android/httpreq.jsp?par=hk";
URL url = null;
try {
url = new URL(httpUrl);
} catch (MalformedURLException e) {
e.printStackTrace();
}
if (url != null) {
try {
HttpURLConnection urlConn = (HttpURLConnection) url
.openConnection();// 打開連接,此處只是創建一個實力,並沒有真正的連接
urlConn.setDoInput(true);
urlConn.setDoOutput(true);
urlConn.connect();// 連接
InputStream input = urlConn.getInputStream();
InputStreamReader inputReader = new InputStreamReader(input);
BufferedReader reader = new BufferedReader(inputReader);
String inputLine = null;
StringBuffer sb = new StringBuffer();
while ((inputLine = reader.readLine()) != null) {
sb.append(inputLine).append("n");
}
reader.close();
inputReader.close();
input.close();
urlConn.disconnect();
if(sb !=null){
tv.setText(sb.toString());
}else{
tv.setText("讀取的內容:NULL");
}
} catch (IOException e) {
e.printStackTrace();
}
}else{
Log.i("TAG", "url is null");
}
}else if(id==3){
String httpUrl = "http://192.168.0.12:8080/Android/httpreq.jsp";
URL url = null;
try {
url = new URL(httpUrl);
} catch (MalformedURLException e) {
e.printStackTrace();
}
if (url != null) {
try {
HttpURLConnection urlConn = (HttpURLConnection) url
.openConnection();// 打開連接,此處只是創建一個實力,並沒有真正的連接
urlConn.setDoInput(true);
urlConn.setDoOutput(true);
urlConn.setRequestMethod("POST");
urlConn.setUseCaches(false);//post請求不能使用緩存.
urlConn.setInstanceFollowRedirects(true);//是否自動重定向.
urlConn.connect();// 連接
OutputStream out = urlConn.getOutputStream();
DataOutputStream data = new DataOutputStream(out);
data.writeBytes("par="+URLEncoder.encode("hk", "GBK"));
data.flush();
data.close();
out.close();
InputStream input = urlConn.getInputStream();
InputStreamReader inputReader = new InputStreamReader(input);
BufferedReader reader = new BufferedReader(inputReader);
String inputLine = null;
StringBuffer sb = new StringBuffer();
while ((inputLine = reader.readLine()) != null) {
sb.append(inputLine).append("n");
}
reader.close();
inputReader.close();
input.close();
urlConn.disconnect();
if(sb !=null){
tv.setText(sb.toString());
}else{
tv.setText("讀取的內容:NULL");
}
} catch (IOException e) {
e.printStackTrace();
}
}else{
Log.i("TAG", "url is null");
}
}else if(id==4){
String httpUrl = "http://android.tgbus.com/Android/UploadFiles_4504/201204/2013215.gif";
URL url = null;
try {
url = new URL(httpUrl);
} catch (MalformedURLException e) {
e.printStackTrace();
}
if (url != null) {
try {
HttpURLConnection urlConn = (HttpURLConnection) url
.openConnection();// 打開連接,此處只是創建一個實力,並沒有真正的連接
urlConn.connect();// 連接
InputStream input = urlConn.getInputStream();
mBitmap = BitmapFactory.decodeStream(input);
if(mBitmap != null){
iv.setImageBitmap(mBitmap);
}
} catch (IOException e) {
e.printStackTrace();
}
}else{
Log.i("TAG", "url is null");
}
}
}
}
Android通用流行框架大全 1. 緩存 DiskLruCacheJava實現基於LRU的磁盤緩存 2.圖片加載 Android Univer
ListFragment繼承於Fragment。因此它具有Fragment的特性,能夠作為activity中的一部分,目的也是為了使頁面設計更加靈活。 相比Fra
登錄應用程序的屏幕,詢問憑據登錄到一些特定的應用。可能需要登錄到Facebook,微博等本章介紹了,如何創建一個登錄界面,以及如何管理安全問題和錯誤嘗試。首先,必須定義兩
SAX是一個解析速度快並且占用內存少的xml解析器,非常適合用於Android等移動設備。 SAX解析XML文件采用的是事件驅動,也就是說,它並不需要解析完整個文