转载 Android之网络与通信

摘要:
示例代码如下:Java代码收藏代码//创建HttpClient//这里使用DefaultHttpClient表示默认属性HttpClienthc=newDefaultHttpClient();//HttpGet实例HttpGetget=newHttpGet;//连接try{HttpResponserp=hc.execute;if{InputStreamis=rp.getEntity().getContent();//处理数据}}catch{//TODOAuto-generatedcatchblocke.printStackTrace();}catch{//TODOAuto-generatedcatchblocke.printStackTrace();}2.3Android网络接口android.net.*包实际上是通过对Apache中HttpClient的封装来实现的一个HTTP编程接口,同时还提供了HTTP请求队列管理以及HTTP连接池管理,以提高并发请求情况下的处理效率,除此之外还有网络状态监视等接口、网络访问的Socket、常用的Uri类以及有关Wifi相关的类等等。
2.三种网络接口简述
2.1标准Java接口
java.net.
*提供与联网有关的类,包括流和数据包套接字、Internet协议、常见HTTP处理。
使用java.net.
*包连接网络代码:
Java代码 收藏代码
try{
//定义地址 URL url=newURL("http://www.google.com");
//打开连接 HttpURLConnection http=(HttpURLConnection)url.openConnection();
//得到连接状态 intnRC=http.getResponseCode();
if(nRC==HttpURLConnection.HTTP_OK)
{
//取得数据 InputStream is =http.getInputStream();
//处理数据
}
}
catch(Exception e) {
//TODO Auto-generated catch block e.printStackTrace();
}
2.2Apache接口
org. apache.http.
*提供的HttpClient对HTTP的请求做了比较好的封装。示例代码如下:
Java代码 收藏代码
//创建HttpClient
//这里使用DefaultHttpClient表示默认属性 HttpClient hc =newDefaultHttpClient();
//HttpGet实例 HttpGet get =newHttpGet("http://www.google.com");
//连接 try{
HttpResponse rp
=hc.execute(get);
if(rp.getStatusLine().getStatusCode()==HttpStatus.SC_OK)
{
InputStream is
=rp.getEntity().getContent();
//处理数据 }
}
catch(ClientProtocolException e) {
//TODO Auto-generated catch block e.printStackTrace();
}
catch(IOException e) {
//TODO Auto-generated catch block e.printStackTrace();
}
2.3Android 网络接口
android.net.
*包实际上是通过对Apache中HttpClient的封装来实现的一个HTTP编程接口,同时还提供了HTTP请求队列管理以及HTTP连接池管理,以提高并发请求情况下的处理效率,除此之外还有网络状态监视等接口、网络访问的Socket、常用的Uri类以及有关Wifi相关的类等等。
3. HTTP通信
在上面的介绍中我们可以看出http的通信方式,有2种方式可以实现HttpURLConnection和HttpClient。
3.1HttpURLConnection实现HTTP通信
我们知道在http的请求中主要有2种方式,GET 和 POST。下面我们通过代码看看如何实现的。
Get方式的HTTP请求,代码如下:
Java代码 收藏代码
//GET获取数据 publicclassHttpGetActivity extendsActivity
{
privatefinalString DEBUG_TAG ="Activity02";
/**Called when the activity is first created. */
@Override
publicvoidonCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.http);
TextView mTextView
=(TextView)this.findViewById(R.id.TextView_HTTP);
//http地址 String httpUrl ="http://192.168.1.110:8080/http1.jsp?name=bigboy";
//获得的数据 String resultData ="";
URL url
=null;
try
{
//构造一个URL对象 url =newURL(httpUrl);
}
catch(MalformedURLException e)
{
Log.e(DEBUG_TAG,
"MalformedURLException");
}
if(url !=null)
{
try
{
//使用HttpURLConnection打开连接 HttpURLConnection urlConn =(HttpURLConnection) url.openConnection();
//得到读取的内容(流) InputStreamReader in =newInputStreamReader(urlConn.getInputStream());
//为输出创建BufferedReader BufferedReader buffer =newBufferedReader(in);
String inputLine
=null;
//使用循环来读取获得的数据 while(((inputLine =buffer.readLine()) !=null))
{
//我们在每一行后面加上一个"\n"来换行 resultData +=inputLine +"\n";
}
//关闭InputStreamReader in.close();
//关闭http连接 urlConn.disconnect();
//设置显示取得的内容 if( resultData !=null)
{
mTextView.setText(resultData);
}
else
{
mTextView.setText(
"读取的内容为NULL");
}
}
catch(IOException e)
{
Log.e(DEBUG_TAG,
"IOException");
}
}
else
{
Log.e(DEBUG_TAG,
"Url NULL");
}
//设置按键事件监听 Button button_Back =(Button) findViewById(R.id.Button_Back);
/*监听button的事件信息 */
button_Back.setOnClickListener(
newButton.OnClickListener()
{
publicvoidonClick(View v)
{
/*新建一个Intent对象 */
Intent intent
=newIntent();
/*指定intent要启动的类 */
intent.setClass(Activity02.
this, Activity01.class);
/*启动一个新的Activity */
startActivity(intent);
/*关闭当前的Activity */
Activity02.
this.finish();
}
});
}
}
POST请求的方式稍有不同,代码如下:
Java代码 收藏代码
//以post方式上传参数 publicclassHttpPOSTActivity extendsActivity
{
privatefinalString DEBUG_TAG ="Activity04";
/**Called when the activity is first created. */
@Override
publicvoidonCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.http);
TextView mTextView
=(TextView)this.findViewById(R.id.TextView_HTTP);
//http地址"?par=abcdefg"是我们上传的参数 String httpUrl ="http://192.168.1.110:8080/httpget.jsp";
//获得的数据 String resultData ="";
URL url
=null;
try
{
//构造一个URL对象 url =newURL(httpUrl);
}
catch(MalformedURLException e)
{
Log.e(DEBUG_TAG,
"MalformedURLException");
}
if(url !=null)
{
try
{
//使用HttpURLConnection打开连接 HttpURLConnection urlConn =(HttpURLConnection) url.openConnection();
//因为这个是post请求,设立需要设置为true urlConn.setDoOutput(true);
urlConn.setDoInput(
true);
//设置以POST方式 urlConn.setRequestMethod("POST");
//Post 请求不能使用缓存 urlConn.setUseCaches(false);
urlConn.setInstanceFollowRedirects(
true);
//配置本次连接的Content-type,配置为application/x-www-form-urlencoded的 urlConn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
//连接,从postUrl.openConnection()至此的配置必须要在connect之前完成,
//要注意的是connection.getOutputStream会隐含的进行connect。 urlConn.connect();
//DataOutputStream流 DataOutputStream out =newDataOutputStream(urlConn.getOutputStream());
//要上传的参数 String content ="par="+URLEncoder.encode("ABCDEFG", "gb2312");
//将要上传的内容写入流中 out.writeBytes(content);
//刷新、关闭 out.flush();
out.close();
//获取数据 BufferedReader reader =newBufferedReader(newInputStreamReader(urlConn.getInputStream()));
String inputLine
=null;
//使用循环来读取获得的数据 while(((inputLine =reader.readLine()) !=null))
{
//我们在每一行后面加上一个"\n"来换行 resultData +=inputLine +"\n";
}
reader.close();
//关闭http连接 urlConn.disconnect();
//设置显示取得的内容 if( resultData !=null)
{
mTextView.setText(resultData);
}
else
{
mTextView.setText(
"读取的内容为NULL");
}
}
catch(IOException e)
{
Log.e(DEBUG_TAG,
"IOException");
}
}
else
{
Log.e(DEBUG_TAG,
"Url NULL");
}
Button button_Back
=(Button) findViewById(R.id.Button_Back);
/*监听button的事件信息 */
button_Back.setOnClickListener(
newButton.OnClickListener()
{
publicvoidonClick(View v)
{
/*新建一个Intent对象 */
Intent intent
=newIntent();
/*指定intent要启动的类 */
intent.setClass(Activity04.
this, Activity01.class);
/*启动一个新的Activity */
startActivity(intent);
/*关闭当前的Activity */
Activity04.
this.finish();
}
});
}
}
默认是使用GET方式。
3.2HttpClient实现HTTP通信
HttpClient实现GET请求方式,代码如下:
Java代码 收藏代码
publicclassClientGETActivity extendsActivity
{
/**Called when the activity is first created. */
@Override
publicvoidonCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.http);
TextView mTextView
=(TextView) this.findViewById(R.id.TextView_HTTP);
//http地址 String httpUrl ="http://192.168.1.110:8080/httpget.jsp?par=HttpClient_android_Get";
//HttpGet连接对象 HttpGet httpRequest =newHttpGet(httpUrl);
try
{
//取得HttpClient对象 HttpClient httpclient =newDefaultHttpClient();
//请求HttpClient,取得HttpResponse HttpResponse httpResponse =httpclient.execute(httpRequest);
//请求成功 if(httpResponse.getStatusLine().getStatusCode() ==HttpStatus.SC_OK)
{
//取得返回的字符串 String strResult =EntityUtils.toString(httpResponse.getEntity());
mTextView.setText(strResult);
}
else
{
mTextView.setText(
"请求错误!");
}
}
catch(ClientProtocolException e)
{
mTextView.setText(e.getMessage().toString());
}
catch(IOException e)
{
mTextView.setText(e.getMessage().toString());
}
catch(Exception e)
{
mTextView.setText(e.getMessage().toString());
}
//设置按键事件监听 Button button_Back =(Button) findViewById(R.id.Button_Back);
/*监听button的事件信息 */
button_Back.setOnClickListener(
newButton.OnClickListener()
{
publicvoidonClick(View v)
{
/*新建一个Intent对象 */
Intent intent
=newIntent();
/*指定intent要启动的类 */
intent.setClass(Activity02.
this, Activity01.class);
/*启动一个新的Activity */
startActivity(intent);
/*关闭当前的Activity */
Activity02.
this.finish();
}
});
}
}
HttpClient实现POST请求方式稍有复杂,要求使用NameValuePair保存传递参数,代码如下:
Java代码 收藏代码
publicclassClientPOSTActivity extendsActivity
{
/**Called when the activity is first created. */
@Override
publicvoidonCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.http);
TextView mTextView
=(TextView) this.findViewById(R.id.TextView_HTTP);
//http地址 String httpUrl ="http://192.168.1.110:8080/httpget.jsp";
//HttpPost连接对象 HttpPost httpRequest =newHttpPost(httpUrl);
//使用NameValuePair来保存要传递的Post参数 List<NameValuePair>params =newArrayList<NameValuePair>();
//添加要传递的参数 params.add(newBasicNameValuePair("par", "HttpClient_android_Post"));
try
{
//设置字符集 HttpEntity httpentity =newUrlEncodedFormEntity(params, "gb2312");
//请求httpRequest httpRequest.setEntity(httpentity);
//取得默认的HttpClient HttpClient httpclient =newDefaultHttpClient();
//取得HttpResponse HttpResponse httpResponse =httpclient.execute(httpRequest);
//HttpStatus.SC_OK表示连接成功 if(httpResponse.getStatusLine().getStatusCode() ==HttpStatus.SC_OK)
{
//取得返回的字符串 String strResult =EntityUtils.toString(httpResponse.getEntity());
mTextView.setText(strResult);
}
else
{
mTextView.setText(
"请求错误!");
}
}
catch(ClientProtocolException e)
{
mTextView.setText(e.getMessage().toString());
}
catch(IOException e)
{
mTextView.setText(e.getMessage().toString());
}
catch(Exception e)
{
mTextView.setText(e.getMessage().toString());
}
//设置按键事件监听 Button button_Back =(Button) findViewById(R.id.Button_Back);
/*监听button的事件信息 */
button_Back.setOnClickListener(
newButton.OnClickListener()
{
publicvoidonClick(View v)
{
/*新建一个Intent对象 */
Intent intent
=newIntent();
/*指定intent要启动的类 */
intent.setClass(Activity03.
this, Activity01.class);
/*启动一个新的Activity */
startActivity(intent);
/*关闭当前的Activity */
Activity03.
this.finish();
}
});
}
}
4.Socket通信
在Android中完全可以使用Java标准APilai开发Socket程序。
下面是一个服务器和客户端通信的例子。
服务器断代码:
Java代码 收藏代码
publicclassServer implementsRunnable
{
publicvoidrun()
{
try
{
//创建ServerSocket ServerSocket serverSocket =newServerSocket(54321);
while(true)
{
//接受客户端请求 Socket client =serverSocket.accept();
System.out.println(
"accept");
try
{
//接收客户端消息 BufferedReader in =newBufferedReader(newInputStreamReader(client.getInputStream()));
String str
=in.readLine();
System.out.println(
"read:"+str);
//向服务器发送消息 PrintWriter out =newPrintWriter( newBufferedWriter( newOutputStreamWriter(client.getOutputStream())),true);
out.println(
"server message");
//关闭流 out.close();
in.close();
}
catch(Exception e)
{
System.out.println(e.getMessage());
e.printStackTrace();
}
finally
{
//关闭 client.close();
System.out.println(
"close");
}
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
//main函数,开启服务器 publicstaticvoidmain(String a[])
{
Thread desktopServerThread
=newThread(newServer());
desktopServerThread.start();
}
}
客户端代码:
Java代码 收藏代码
publicclassClientActivity extendsActivity
{
privatefinalString DEBUG_TAG ="Activity01";
privateTextView mTextView=null;
privateEditText mEditText=null;
privateButton mButton=null;
/**Called when the activity is first created. */
@Override
publicvoidonCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButton
=(Button)findViewById(R.id.Button01);
mTextView
=(TextView)findViewById(R.id.TextView01);
mEditText
=(EditText)findViewById(R.id.EditText01);
//登陆 mButton.setOnClickListener(newOnClickListener()
{
publicvoidonClick(View v)
{
Socket socket
=null;
String message
=mEditText.getText().toString() +"\r\n";
try
{
//创建Socket socket =newSocket("192.168.1.110",54321);
//向服务器发送消息 PrintWriter out =newPrintWriter( newBufferedWriter( newOutputStreamWriter(socket.getOutputStream())),true);
out.println(message);
//接收来自服务器的消息 BufferedReader br =newBufferedReader(newInputStreamReader(socket.getInputStream()));
String msg
=br.readLine();
if( msg !=null)
{
mTextView.setText(msg);
}
else
{
mTextView.setText(
"数据错误!");
}
//关闭流 out.close();
br.close();
//关闭Socket socket.close();
}
catch(Exception e)
{
//TODO: handle exception Log.e(DEBUG_TAG, e.toString());
}
}
});
}
}
5.乱码问题
网络通信中,产生乱码的主要原因是通信过程中使用了不同的编码方式:服务器中的编码方式,传输过程中的编码方式,传输到达终端设备的编码方式。
解决中文乱码的两个步骤:
1.使用getBytes("编码方式")来对汉字进行重编码,得到它的字节数组。
2.再使用new String(Bytes[],"解码方式")来对字节数组进行相应的解码。
至此Android的网络通信篇基本结束了。

免责声明:文章转载自《转载 Android之网络与通信》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Egret Engine(白鹭引擎)介绍及windows下安装SpringBoot核心简述下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

智能打印SDK-源码剖析

 演示站点+源码 均在github  迁徙至github: https://github.com/micro-chen/WebPrinter  智能打印SDK-官方群 547747202 -----------------过时--------------- 开源地址:http://www.dnnode.com/ 软件下载地址:http://www.dnno...

natapp自动获取免费的动态端口域名

前段时间,因为客户有个项目要求跨局域网进行远程控制桌面,想知道能不能实现。于是查询了许多资料,了解到需要有公网服务器作为中介才能够实现,但是公司又没有公网服务器,于是只有利用花生壳、natapp服务器实现内网穿透服务。但是这些都要收费,免费的域名和端口是随机变动的,对于测试相当不方便。于是就想设计一款工具能够自动获取natapp产生的动态域名与端口。申请n...

Java高并发网络编程(四)Netty

在网络应用开发的过程中,直接使用JDK提供的NIO的API,比较繁琐,而且想要进行性能提升,还需要结合多线程技术。 由于网络编程本身的复杂性,以及JDK API开发的使用难度较高,所以在开源社区中,涌现出来了很多对JDK NIO进行封装、增强的网络编程框架,比如Netty、Mina等。 一、Netty简介 https://netty.io/官网 Netty...

JSP上传视频等大文件

前言:因自己负责的项目(jetty内嵌启动的SpringMvc)中需要实现文件上传,而自己对java文件上传这一块未接触过,且对 Http 协议较模糊,故这次采用渐进的方式来学习文件上传的原理与实践。该博客重在实践。 一. Http协议原理简介      HTTP是一个属于应用层的面向对象的协议,由于其简捷、快速的方式,适用于分布式超媒体信息系统。它于19...

关于HTTP协议,一篇就够了

HTTP简介 HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文本到本地浏览器的传送协议。 HTTP是一个基于TCP/IP通信协议来传递数据(HTML 文件, 图片文件, 查询结果等)。 HTTP是一个属于应用层的面向对象的协议,由于其简捷...

简单的多线程通信实例(用委托事件实现)

 1using System; 2using System.Threading; 3 4namespace ifan 5{ 6    //自定义委托声明 7    public delegate void childExitDelegate(object sender, ChildExitEventArgs e); 8 9    public class...