客户端访问服务器的一个例子

来源:互联网 发布:网络招生哪家强 编辑:程序博客网 时间:2024/04/30 10:12

客户端的程序要访问服务器,
第一,加权限<uses-permission android:name="android.permission.INTERNET"/>
第二步:继承activity实现runnable接口
public class MainActivity extends Activity implements Runnable{

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
     String strVer = android.os.Build.VERSION.RELEASE;
     System.out.println(strVer);
     strVer = strVer.substring(0, 3).trim();
     float fv = Float.valueOf(strVer);
     if (fv > 2.3) {
       StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
           .detectDiskReads().detectDiskWrites().detectNetwork()  //  这里可以替换为 detectAll()
                                       // 就包括了磁盘读写和网络I/O
           .penaltyLog() // 打印 logcat,当然也可以定位到dropbox,通过 文件保存相应的 log
           .build());
       StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
         .detectLeakedSqlLiteObjects() // 探测 SQLite数据库操作
         .penaltyLog() // 打印 logcat
         .penaltyDeath().build());
     }
  new Thread(this).start();
 }

 @Override
 public void run() {
  try {
   Log.i("content---1", "content1");
   Socket s = new Socket("192.168.1.101",8080);
   //字符串--是调post还是get---服务端项目名称---发送字符串的长度---发送文本类型---主机ip和端口---发送的字符串
   //POST后有个空格,Host:冒号后面有个空格
   String str = "POST /FristWebP/myServlet HTTP/1.1\n";
   str +="Content-Length:25\n";
   str +="Content-Type:application/x-www-form-urlencoded\n";
   str += "Host: 192.168.1.101:8080\n\n";
   str +="name=liuping&pwd=12345678";
   //获得的一个输出流,往服务器端发送一串文本str
   OutputStream out = s.getOutputStream();
   PrintWriter ps = new PrintWriter(out);
   ps.println(str);
   //刷新流,把文件发出
   ps.flush();
   Log.i("content---2", "content");
   //从socket中获取流(从服务器端获得的流)获取字节流
   InputStream in = s.getInputStream();
   //将服务器端返回的字节流转化成字符流输出
   BufferedReader br = new BufferedReader(new InputStreamReader(in));
   String content = null;
   while((content = br.readLine()) != null){
    Log.i("content---3", content);
   }
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
 }

}

 

第三步 :服务器端接收程序的写法,myServlet,从发送的字符串中可知连接的页面是myServlet,(每一个servlet是一个页面),客户端连接哪个,服务端哪个页面就会响应。
                从上面的字符中可知,连接的是"POST/FristWebP/myServlet, -----FristWebP项目-->myServlet-->post方法
public class MyServlet extends HttpServlet {

 //销毁
 public void destroy() {
  super.destroy(); // Just puts "destroy" string in log
  // Put your code here
 }
 //request 表示客户端的请求
 //response 表示服务端的响应
// public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
//  doPost(request, response);
  response.setContentType("text/html");
  //获得从客户端传过来的参数
  String name = request.getParameter("name");
  String pwd = request.getParameter("pwd");
  //从服务器端获得字符流,输出到客户端那边。
  PrintWriter out = response.getWriter();
  out.println("Hello WorldEEEEEEEEEEE");
  out.println(name+"    "+pwd);
  out.flush();
  out.close();
 }

 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
//  doGet(request, response);
    response.setContentType("text/html");
  String name = request.getParameter("name");
  String pwd = request.getParameter("pwd");
  //从服务器端获得字符流
  PrintWriter out = response.getWriter();
  out.println("World HelloRRRRRRRRRRRRRRRR");
  out.println(name+"    "+pwd);
  out.flush();
  out.close();
 }

 //初始化方法
 public void init() throws ServletException {
  System.out.println("init()");
 }

}

 

 

原创粉丝点击