码迷,mamicode.com
首页 > 移动开发 > 详细

android实现 服务器功能

时间:2015-07-21 22:12:09      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:

  1 package com.weijia.tests;
  2 
  3 import java.io.IOException;
  4 import java.net.InetSocketAddress;
  5 import java.net.ServerSocket;
  6 import java.net.Socket;
  7 import java.util.concurrent.Executor;
  8 
  9 import org.apache.http.HttpException;
 10 import org.apache.http.HttpRequest;
 11 import org.apache.http.HttpResponse;
 12 import org.apache.http.HttpResponseInterceptor;
 13 import org.apache.http.impl.DefaultConnectionReuseStrategy;
 14 import org.apache.http.impl.DefaultHttpResponseFactory;
 15 import org.apache.http.impl.DefaultHttpServerConnection;
 16 import org.apache.http.params.BasicHttpParams;
 17 import org.apache.http.params.CoreConnectionPNames;
 18 import org.apache.http.params.CoreProtocolPNames;
 19 import org.apache.http.params.HttpParams;
 20 import org.apache.http.protocol.HttpContext;
 21 import org.apache.http.protocol.HttpProcessor;
 22 import org.apache.http.protocol.HttpRequestHandler;
 23 import org.apache.http.protocol.HttpRequestHandlerRegistry;
 24 import org.apache.http.protocol.HttpService;
 25 import org.apache.http.protocol.ImmutableHttpProcessor;
 26 import org.apache.http.protocol.ResponseConnControl;
 27 import org.apache.http.protocol.ResponseContent;
 28 import org.apache.http.protocol.ResponseDate;
 29 import org.apache.http.protocol.ResponseServer;
 30 
 31 import com.example.cairh_sjkh_it.R;
 32 
 33 import android.app.Activity;
 34 import android.content.Intent;
 35 import android.os.Bundle;
 36 import android.widget.Toast;
 37 
 38 import com.sun.net.httpserver.HttpHandler;
 39 import com.sun.net.httpserver.HttpServer;   
 40  
 41 public class MainActivity extends Activity {
 42     ServerSocket serverSocket;
 43     @Override
 44     protected void onCreate(Bundle savedInstanceState) {
 45         super.onCreate(savedInstanceState);
 46         setContentView(R.layout.res);
 47 //        startService(new Intent(this, SocketService.class));
 48         try {
 49             serverSocket=new ServerSocket(8080);
 50         } catch (IOException e) {
 51             // TODO Auto-generated catch block
 52             e.printStackTrace();
 53         }
 54         HttpParams params = new BasicHttpParams();  
 55          params  
 56                  .setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 5000)  
 57                  .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE,8 * 1024)  
 58                  .setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false)  
 59                  .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true)  
 60                  .setParameter(CoreProtocolPNames.ORIGIN_SERVER,"HttpComponents/1.1");  
 61 
 62         HttpProcessor httpproc = new ImmutableHttpProcessor(  
 63                 new HttpResponseInterceptor[] {  
 64                         new ResponseDate(), new ResponseServer(),  
 65                         new ResponseContent(), new ResponseConnControl() });  
 66         HttpService httpService=new HttpService(httpproc,  
 67                 new DefaultConnectionReuseStrategy(),  
 68                 new DefaultHttpResponseFactory());  
 69         httpService.setParams(params);
 70         // 创建HTTP请求执行器注册表
 71         HttpRequestHandlerRegistry reqistry = new HttpRequestHandlerRegistry();
 72         // 增加HTTP请求执行器
 73         reqistry.register("*" , new HttpRequestHandler() {
 74             @Override
 75             public void handle(HttpRequest httprequest, HttpResponse httpresponse,
 76                     HttpContext httpcontext) throws HttpException, IOException {
 77                 Toast.makeText(MainActivity.this, "good", Toast.LENGTH_LONG);
 78             }
 79         });
 80         // 设置HTTP请求执行器
 81         httpService.setHandlerResolver(reqistry);
 82         new Thread(runnable).start();
 83 //        Socket socket=new Socket();
 84 //        socket.bind(new InetSocketAddress(port))
 85     }
 86     Runnable runnable=new Runnable() {
 87         
 88         @Override
 89         public void run() {
 90             // TODO Auto-generated method stub
 91             /* 循环接收各客户端 */
 92             boolean isLoop = true;
 93             while (isLoop && !Thread.interrupted()) {
 94             // 接收客户端套接字
 95             try {
 96                 Socket socket = serverSocket.accept();
 97             } catch (IOException e) {
 98                 // TODO Auto-generated catch block
 99                 e.printStackTrace();
100             }
101             // 绑定至服务器端HTTP连接
102             //DefaultHttpServerConnection conn = new DefaultHttpServerConnection();
103             //conn.bind(socket, params);
104             // 派送至WorkerThread处理请求
105             //Thread t = new WorkerThread(httpService, conn);
106             //t.setDaemon(true); // 设为守护线程
107             //t.start();
108             }
109         }
110     };
111 }

 

android实现 服务器功能

标签:

原文地址:http://www.cnblogs.com/friends-wf/p/4665696.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!