Android应用程序和服务器进行信息交换,本质上和浏览器与服务器交互是一样的,都是客户端向服务器发出一个请求,服务器对请求进行处理,最后向客户端相应请求。
请求和回应都是以流(stream)的形式进行传递的。
客户端和服务器要对流分别进行加工处理。
使用java.net(标准的java接口)来实现Android应用程序同服务器之间的信息交互。
请求流:DataOutputStream
URL----àHttpURLConnection--à DataOutputStream
响应流:InputStream
URL----àHttpURLConnection--à InputStream
详细步骤如下:
1、 连接字符串
String httpURLString ="http://172.16.5.36:8080/AndroidConnectionTest/servlet/g1";
2、 创建URL
url=new URL(httpURLString);
3、 创建连接
HttpURLConnection urlConn =(HttpURLConnection)url.openConnection();
4、 设置连接(post请求需要这样设置,get不需要,即此步可省略)
//因为这个是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();
5、 创建请求输出流
DataOutputStream out=new DataOutputStream(urlConn.getOutputStream());
6、 在请求输出流中添加数据
//要上传的参数
String content="id="+URLEncoder.encode(id,"gb2312");
//上可以简写为:String content = "name=gyf" ;
//将要上传的内容写入流中
out.writeBytes(content);
content ="&name="+URLEncoder.encode(name,"gb2312");
out.writeBytes(content);
//刷新、关闭
out.flush();//此步非常中,流是以块的形式传递的,每个块有固定的大小,只有当块满时,此块才会发送出去,采用flush()时,不论块是否满都发送出去
out.close();
7、 对响应输入流进行处理
//获取数据
BufferedReader reader = new BufferedReader(
new InputStreamReader(urlConn.getInputStream()));
String inputLine = null;
//使用循环来读取获得的数据
while (((inputLine = reader.readLine()) != null))
{
//我们在每一行后面加上一个"/n"来换行
result += inputLine + "/n";
}
reader.close();
//关闭http连接
urlConn.disconnect();
8、 设置Android的权限
在AndoridManifest.xml中设置
添加的标签如下:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
其他权限设置详见我的csdn博客gyflyx
9、 OK
代码完整如下:
public class g3 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.g2);
textView=(TextView)findViewById(R.id.g2_textView1);
String httpUri="http://172.16.5.36:8080/AndroidConnectionTest/servlet/g1";
String result="";
URL url=null;
try {
url=new URL(httpUri);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
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 = new DataOutputStream(urlConn.getOutputStream());
//要上传的参数
String content = "name=gyf" ;
//将要上传的内容写入流中
out.writeBytes(content);
content="&wife="+URLEncoder.encode("lyx","gb2312");
out.writeBytes(content);
//刷新、关闭
out.flush();
out.close();
//获取数据
BufferedReader reader = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
String inputLine = null;
//使用循环来读取获得的数据
while (((inputLine = reader.readLine()) != null))
{
//我们在每一行后面加上一个"/n"来换行
result += inputLine + "/n";
}
reader.close();
//关闭http连接
urlConn.disconnect();
//设置显示取得的内容
if ( result != null )
{
textView.setText(result);
}
else
{
textView.setText("读取的内容为NULL");
}
} catch (Exception e) {
// TODO: handle exception
}
}
}
private TextView textView;
}