編輯:關於Android編程
本文實例為大家詳細介紹了Android開發之HTTP訪問網絡的相關代碼,供大家參考,具體內容如下

代碼1:
package com.ywhttpurlconnection;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class YwhttpurlconnectionActivity 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);
//GET方式傳遞
btn2 = (Button) this.findViewById(R.id.Button02);
//POST方式傳遞
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(YwhttpurlconnectionActivity.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(YwhttpurlconnectionActivity.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(YwhttpurlconnectionActivity.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(YwhttpurlconnectionActivity.this, showdata.class);
Bundle b = new Bundle();
b.putInt("id", 4);
intent.putExtras(b);
startActivity(intent);
}
});
}
}
代碼2:
package com.ywhttpurlconnection;
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;
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.132:8080/Android/http.jsp";
String httpUrl = "http://www.jb-aptech.com.cn";
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){
//GET方式傳遞
// String httpUrl = "http://192.168.0.132:8080/Android/httpreq.jsp?par=hk";
String httpUrl = "http://liveat.acewill.cn/liveat/?cmd=1&uid=xiaoming";
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){
//POST方式傳遞
// String httpUrl = "http://192.168.0.132:8080/Android/httpreq.jsp";
String httpUrl = "http://www.jb-aptech.com.cn";
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://www.google.com.hk/intl/zh-CN/images/logo_cn.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");
}
}
}
}
代碼3:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ywhttpurlconnection"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".YwhttpurlconnectionActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".showdata"></activity>
</application>
</manifest>
代碼4:http.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/TextView_HTTP" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <ImageView android:id="@+id/ImageView01" android:layout_width="172dp" android:layout_height="307dp" > </ImageView> </LinearLayout>
代碼5.mail.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <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>
6.運行結果
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。
Android下拉刷新完全解析,教你如何一分鐘實現下拉刷新功能(附源碼)
最近項目中需要用到ListView下拉刷新的功能,一開始想圖省事,在網上直接找一個現成的,可是嘗試了網上多個版本的下拉刷新之後發現效果都不怎麼理想。有些是因為功能不完整或
Android4.2添加自己的產品分支及video的拷貝方法
1、rk3168_v4.2\frameworks\base\data\videos下面的mp4的拷貝方法! a、其實在我們的原始情況下這個目錄的東西並沒有拷貝到xxx/s
Android切圖注意事項
Android切圖注意事項 1. 切圖需要兩套分辨率的圖:480*800,720*1280。分被放在不同的文件夾中,同一張圖片,在兩個文件夾中的名字要一致。 2.
Android音頻開發(5):音頻數據的編解碼
前面四篇文章分別介紹了音頻開發必備的基礎知識、如何采集一幀音頻、如何播放一幀音頻、如何存儲和解析wav格式的文件,建議有興趣的小伙伴們先讀一讀,本文則重點關注如何對一幀音