Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 661|回复: 0

Android 网络通信-  Android学习

[复制链接]

该用户从未签到

发表于 2011-10-24 14:32:09 | 显示全部楼层 |阅读模式
我们今天来说的就是android的网络通信,这个其实在我们开发android应用的时候是非常重要的,那么我们今天就来简单的说一说吧,我们先在xml里写上4个button,分别是直接获取数据 GET方式传递 POST方式传递 获取图片。这样我们就能来实现了。那么就不多说了,还是上代码比较直接。

java代码: <?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

androidrientation="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=&quotOST方式传递" 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 eoe.demo;



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);

}



});

}

}
复制代码
java代码:

<?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>
复制代码package eoe.demo;



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 = "这个就是要连接的网站";

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.132: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.132: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 = "自定义的图片";

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");

}

}

}

}
复制代码现在从表面上看,我们就算做完了,但是要是现在做完了的话,那么我们在运行的时候会发现什么效果也没有。而且还会出错,那么是因为什么才出现这个毛病的呀。其实就是没有注册。现在我们就来看看它的代码吧:

java代码:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="eoe.demo" android:versionCode="1"

android:versionName="1.0">

<application android:icon="@drawable/icon"

android:label="@string/app_name">

<activity android:name=".Test"

android:label="@string/app_name">

<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>

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



</manifest>
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

GMT+8, 2025-1-11 11:10 , Processed in 0.412542 second(s), 48 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表