在Eclipse中新建一个android程序 主界面是一个简单的按钮
MainActivity.java
<span style="font-size:18px;"><strong>
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void myClick (View view){
switch(view.getId()){
case R.id.btn1:
showText();
break;
}
}
//显示文本信息,来自服务器
public void showText(){
new AsyncTask() {
@Override
protected Object doInBackground(Object... params) {
String msg="";
try{
//创建HttpURLConnection
//事实上HttpURLConnection并不是new出来的,而是通过要访问的地址所生成的
//URL是统一资源定位符,表示一个有效的网络路径(URI是统一资源标识符,标识一个有效的网络资源)
URL url=new URL(params[0].toString());
HttpURLConnection con=(HttpURLConnection)url.openConnection();
//设置相关的参数,最基本的有提交方式和连接超时的时间(默认超时是30秒)
con.setRequestMethod("GET");
con.setConnectTimeout(6000);
//获取响应
if(con.getResponseCode()==HttpURLConnection.HTTP_OK){
//getResponseCode表面是获得响应代码来判断是否请求成功
//事实它还有个隐藏作用---提交
//所以在通过HttpURLConnection进行网络操作的时候,getResponseCode不要省略
//开始获取响应内容
InputStream in=con.getInputStream();
byte[] buffer=new byte[1024];
int len=-1;
ByteArrayOutputStream bos=new ByteArrayOutputStream();
while((len=in.read(buffer))>0){
bos.write(buffer, 0, len);
}
return new String(bos.toByteArray());
}
}catch(Exception e){
e.printStackTrace();
}
return msg;
}
@Override
protected void onPostExecute(Object result) {
AlertDialog.Builder dialog=new AlertDialog.Builder(MainActivity.this);
dialog.setTitle("服务器消息");
dialog.setMessage(result.toString());
dialog.setNegativeButton("确定", null);
dialog.show();
}
}.execute("http://172.20.4.56:8088/AndroidWebServer/getText.do");
}
}</strong></span>
在MyEclipse新建一个AndroidWebServer 的web工程
新建一个servlet/GetTextServlet.java ----------getText.do
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/*
* 当Android设备请求这个Servlet后返回一段信息,在Android设备中显示
*/
public class GetTextServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
String msg1=request.getParameter("msg1");
String msg2=request.getParameter("msg2");
System.out.println("msg1="+msg1);
System.out.println("msg2="+msg2);
out.println("你好,这是来自Web服务器的消息");
out.flush();
out.close();
}
}
在Myeclipse中服务器端部署然后开启服务器后再运行Eclipse中的程序