【Android开发】网络编程及Internet应用-通过HTTP访问网络

来源:互联网 发布:cmd mysql 命令 编辑:程序博客网 时间:2024/05/17 05:59
通有线互联网一样,移动互联网也可以使用HTTP访问网络。在Android中,针对HTTP进行网络通信的方法主要有两种,一种是使用HttpURLConnection实现;另一种是使用HttpClient实现。下面分别进行介绍

1.使用HttpURLConnection访问网络

HttpURLConnection位于java.net包中,用于发送HTTP请求和获取HTTP响应。由于该类是抽象类,不能直接实例化对象,则需要使用URL的openConnection()方法来获得。例如,要创建一个http://www.baidu.com网站对应的HttpURLConnection对象,可以使用下面的代码:
URL url=new URL("http://www.baidu.com");
HttpURLConnection urlConnection=(HttpURLConnection)url.openConnection();

创建了HttpURLConnection对象后,就可以使用该对象发送HTTP请求了。HTTP请求分为GET请求和POST请求。下面分别进行介绍。

a.发送GET请求

使用HttpURLConnection发送请求默认使用GET。使用比较简单,将需要传递的参数通过在url路径后方加入"?参数名=参数值"进行传递(多个参数用"&"或","分隔),然后获取流中的数据,并关闭连接即可。

下面通过一个具体的实例说明如何使用HttpURLConnection发送GET请求
功能:在编辑框中输入信息,点击“send”按钮,就会将信息发到服务器端,且在客户端的文字框控件中查看。

界面:模拟发微博,上面是编辑框和发送按钮,下面是从服务器读取的微博内容
res/layout/main.xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/ll1"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" ><EditText android:id="@+id/content"    android:layout_width="match_parent"    android:layout_height="wrap_content"/><Button android:id="@+id/button"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="send"/><ScrollView android:id="@+id/scrollView1"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:layout_weight="1">    <LinearLayout android:id="@+id/ll2"        android:layout_width="match_parent"        android:layout_height="match_parent">        <TextView android:id="@+id/result"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_weight="1"/>    </LinearLayout></ScrollView></LinearLayout>

界面效果如图



主界面:
MainActivity:
package com.example.test;    import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.UnsupportedEncodingException;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import java.net.URLEncoder;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.util.Base64;import android.view.View;import android.view.View.OnClickListener;import android.view.Window;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;  public class MainActivity extends Activity{  private EditText content;//声明一个输入文本内容的编辑框对象private Button button;//声明一个"发表"按钮对象private Handler handler;//声明一个Handler对象private String result="";//声明一个代表显示内容的字符串private TextView resultTV;//声明一个显示结果的文本框对象    @Override      public void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          requestWindowFeature(Window.FEATURE_NO_TITLE);//设置全屏显示        setContentView(R.layout.main);                content=(EditText)findViewById(R.id.content);        resultTV=(TextView)findViewById(R.id.result);        button=(Button)findViewById(R.id.button);        //为按钮添加单击事件监听        button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) { if("".equals(content.getText().toString())){ Toast.makeText(MainActivity.this, "请输入要发表的内容!", Toast.LENGTH_SHORT).show();//显示消息提示 return; } //创建一个新线程,用于发送并读取微博信息 new Thread(new Runnable() {@Overridepublic void run() {send();//发送文本至web服务器,并读取Message m=handler.obtainMessage();//获取一个Messagehandler.sendMessage(m);//发送消息}}).start();//开启线程}});                handler=new Handler(){@Overridepublic void handleMessage(Message msg) {if(result!=null){resultTV.setText(result);//显示获得的结果content.setText("");//清空编辑框}super.handleMessage(msg);}                };    }        /*     * send()方法用于建立一个HTTP连接,并将输入的内容发送到Web服务器上,     * 再读取服务器的处理结果     * */    public void send() { String target="http://192.168.1.100:8080/WeiXinTest/index.jsp?content=" +base64(content.getText().toString().trim());//要访问的url地址 URL url; try {url=new URL(target);//创建URL对象HttpURLConnection urlConn=(HttpURLConnection)url.openConnection();//创建一个HTTP连接InputStreamReader in=new InputStreamReader(urlConn.getInputStream());//获得读取的内容BufferedReader buffer=new BufferedReader(in);//获得输入流对象String inputLine=null;    //通过循环逐行读取输入流中的内容while((inputLine=buffer.readLine())!=null){result+=inputLine+"\n";}in.close();//关闭字符输入流对象urlConn.disconnect();//断开连接} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}    /*     * 用GET方法传递中文参数时,会产生乱码,     * 需要用Base64编码来解决乱码问题     * */private String base64(String content) {try {//对字符串进行Base64编码content=Base64.encodeToString(content.getBytes("utf-8"),Base64.DEFAULT);content=URLEncoder.encode(content);//对字符串进行URL编码} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch blocke.printStackTrace();}return content;}}  

在安卓的配置文件AndroidManifest.xml中别忘记加入访问网络的权限:
<!-- 添加链接网络的权限 --><uses-permission android:name="android.permission.INTERNET"/>

在JAVAEE工作台(或MyEclipse)中创建一个名为"WenXinTest"的Web应用,在index.jsp中写下如下关键代码:
<%@ page language="java" import="java.util.*,sun.misc.BASE64Decoder" pageEncoding="UTF-8"%><%   String content="";   if(request.getParameter("content")!=null){   content=request.getParameter("content");//获取输入的微博信息   //替换content中的加号,这是由于在进行URL编码时,将"+"号转换为了%2B   content=content.replaceAll("%2B","+");   BASE64Decoder decoder=new BASE64Decoder();   content=new String(decoder.decodeBuffer(content),"utf-8");   } %> <%="发一条微博,内容如下:" %> <%=content %>

接着将应用部署至tomcat,启动tomcat进行测试。

客户端测试结果如图


证明数据已经由客户端发至Web应用,然后经由服务端处理后发回客户端

b.发送POST请求
由于采用GET方式发送请求只适合发送大小在1024个字节以内的数据,所以当要发送的数据较大时,就需要使用POST方式发送请求。
发送POST请求,需要通过其setRequestMethod()方法进行指定。例如,创建一个HTTP连接,并为该连接指定请求发送的方式为POST,可以使用下面的代码:
URL url=new URL("http://www.baidu.com");HttpURLConnection urlConnection=(HttpURLConnection)url.openConnection();urlConnection.setRequestMethod("POST");

发送POST请求要比发送GET请求复杂一些,它通常需要通过HttpURLConnection类以及其父类URLConnection提供的方法设置相关内容,常用的方法如表所示:
setDoInput(boolean newValue)用于设置是否向连接中写入数据,如果参数为true,表示写入数据;否则不写入数据
setDoOutput(boolean newValue)用于设置是否向连接中读取数据,如果参数为true,表示写入数据;否则不读取数据
setUseCaches(boolean newValue)用于设置是否缓存数据,如果参数为true,表示缓存数据;否则不缓存数据
setInstanceFollowRedirects(boolean followRedirects)用于设置是否应该自动执行HTTP重定向,如果参数为true,表示自动执行;否则不自动执行
setRequestProperty(String field,String newVlaue)用于设置一般请求属性,例如要设置内容类型为表单数据,可以进行以下设置:
setRequestProperty("content-Type","applcation/x-www-form-urlencoded");


下面通过一个具体的实例来介绍如何使用HttpURLConnection类发送请求
功能:在编辑框中输入信息和昵称,点击“send”按钮,就会将信息发到服务器端,且在客户端的文字框控件中查看。


界面:模拟发微博,上面是两个编辑框和发送按钮,下面是从服务器读取的微博内容
res/layout/main.xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/ll1"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <TextView android:text="信息:"        android:layout_width="match_parent"        android:layout_height="wrap_content"/><EditText android:id="@+id/content"    android:layout_width="match_parent"    android:layout_height="wrap_content"/><TextView android:text="昵称:"        android:layout_width="match_parent"        android:layout_height="wrap_content"/><EditText android:id="@+id/nickname"    android:layout_width="match_parent"    android:layout_height="wrap_content"/><Button android:id="@+id/button"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="send"/><ScrollView android:id="@+id/scrollView1"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:layout_weight="1">    <LinearLayout android:id="@+id/ll2"        android:layout_width="match_parent"        android:layout_height="match_parent">        <TextView android:id="@+id/result"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_weight="1"/>    </LinearLayout></ScrollView></LinearLayout>

界面效果如图




主界面:
MainActivity:
package com.example.test;    import java.io.BufferedReader;import java.io.DataOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.UnsupportedEncodingException;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import java.net.URLEncoder;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.util.Base64;import android.view.View;import android.view.View.OnClickListener;import android.view.Window;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;  public class MainActivity extends Activity{  private EditText content;//声明一个输入文本内容的编辑框对象private EditText nickname;//声明一个输入昵称的编辑框对象private Button button;//声明一个"发表"按钮对象private Handler handler;//声明一个Handler对象private String result="";//声明一个代表显示内容的字符串private TextView resultTV;//声明一个显示结果的文本框对象    @Override      public void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          requestWindowFeature(Window.FEATURE_NO_TITLE);//设置全屏显示        setContentView(R.layout.main);                content=(EditText)findViewById(R.id.content);        nickname=(EditText)findViewById(R.id.nickname);        resultTV=(TextView)findViewById(R.id.result);        button=(Button)findViewById(R.id.button);        //为按钮添加单击事件监听        button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) { if("".equals(content.getText().toString())){ Toast.makeText(MainActivity.this, "请输入要发表的内容!", Toast.LENGTH_SHORT).show();//显示消息提示 return; } //创建一个新线程,用于发送并读取微博信息 new Thread(new Runnable() {@Overridepublic void run() {send();//发送文本至web服务器,并读取Message m=handler.obtainMessage();//获取一个Messagehandler.sendMessage(m);//发送消息}}).start();//开启线程}});                handler=new Handler(){@Overridepublic void handleMessage(Message msg) {if(result!=null){resultTV.setText(result);//显示获得的结果content.setText("");//清空编辑框nickname.setText("");//清空昵称框}super.handleMessage(msg);}                };    }        /*     * send()方法用于建立一个HTTP连接,并将输入的内容发送到Web服务器上,     * 再读取服务器的处理结果     * */    public void send() { String target="http://192.168.1.113:8080/WeiXinTest/index.jsp";//要访问的url地址 URL url; try {url=new URL(target);//创建URL对象HttpURLConnection urlConn=(HttpURLConnection)url.openConnection();//创建一个HTTP连接urlConn.setRequestMethod("POST");//指定使用POST请求方式urlConn.setDoInput(true);//设置允许向连接中写入数据urlConn.setDoOutput(true);//设置允许从连接中读取数据urlConn.setUseCaches(false);//禁止缓存urlConn.setInstanceFollowRedirects(true);//自动执行HTTP重定向urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");//设置内容类型DataOutputStream out=new DataOutputStream(urlConn.getOutputStream());//获取输出流String param="nickname="+URLEncoder.encode(nickname.getText().toString(),"utf-8")+"&content="+URLEncoder.encode(content.getText().toString(),"utf-8");//连接要提交的数据out.writeBytes(param);//将要传递的数据写入数据输出流out.flush();//输出缓存out.close();//关闭数据输出流//判断相应是否成功if(urlConn.getResponseCode()==HttpURLConnection.HTTP_OK){InputStreamReader in=new InputStreamReader(urlConn.getInputStream());//获得读取的内容BufferedReader buffer=new BufferedReader(in);//获得输入流对象String inputLine=null;    //通过循环逐行读取输入流中的内容while((inputLine=buffer.readLine())!=null){result+=inputLine+"\n";}in.close();//关闭字符输入流对象}urlConn.disconnect();//断开连接} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}    }    

在安卓的配置文件AndroidManifest.xml中别忘记加入访问网络的权限:
<!-- 添加链接网络的权限 --><uses-permission android:name="android.permission.INTERNET"/>

在JAVAEE工作台(或MyEclipse)中创建一个名为"WenXinTest"的Web应用,在index.jsp中写下如下关键代码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%   String content=request.getParameter("content");//获取微博信息   String nickname=request.getParameter("nickname");//获取昵称   if(content!=null&&nickname!=null){       nickname=new String(nickname.getBytes("iso-8859-1"),"utf-8");//转码       content=new String(content.getBytes("iso-8859-1"),"utf-8");//转码       String date=new java.util.Date().toLocaleString();//获取系统时间 %> <%="【"+nickname+"】于"+date+"发表一条微博,内容如下:" %> <%=content %> <% }%>

接着将应用部署至tomcat,启动tomcat进行测试。

客户端测试结果如图


证明数据已经由客户端发至Web应用,然后经由服务端处理后发回客户端


转载请注明出处:http://blog.csdn.net/acmman/article/details/46414315

0 0