我正在关注该教程 http://www.integratingstuff.com/2011/10/24/adding-a-webserver-to-an-android-app/
我做了一些复制粘贴,我尝试在平板电脑上的浏览器中查看我的页面。我有一个简单的活动,有2个按钮用于启动和停止Http的serice。这是我的服务代码:
package com.example.httpserver;
import java.io.IOException;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class WebService extends Service {
private WebServer server = null;
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "The new Service was Created", Toast.LENGTH_LONG).show();
server = new WebServer(this);
server.startServer();
}
@Override
public void onDestroy() {
server.stopServer();
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
对于WebServer类:
package com.example.httpserver;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import org.apache.http.HttpException;
import org.apache.http.impl.DefaultConnectionReuseStrategy;
import org.apache.http.impl.DefaultHttpResponseFactory;
import org.apache.http.impl.DefaultHttpServerConnection;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.BasicHttpProcessor;
import org.apache.http.protocol.HttpRequestHandlerRegistry;
import org.apache.http.protocol.HttpService;
import org.apache.http.protocol.ResponseConnControl;
import org.apache.http.protocol.ResponseContent;
import org.apache.http.protocol.ResponseDate;
import org.apache.http.protocol.ResponseServer;
import android.content.Context;
public class WebServer {
public static int PORT = 8080;
public static boolean RUNNING = false;
private static final String ALL_PATTERN = "*";
private static final String EXCEL_PATTERN = "/*.xls";
private static final String HOME_PATTERN = "/home.html";
private Context context = null;
private ServerSocket serverSocket;
private Socket clientSocket;
private PrintWriter out;
private BufferedReader in;
private BasicHttpProcessor httpproc = null;
private BasicHttpContext httpContext = null;
private HttpService httpService = null;
private HttpRequestHandlerRegistry registry = null;
public WebServer(Context context) {
setContext(context);
httpproc = new BasicHttpProcessor();
httpContext = new BasicHttpContext();
httpproc.addInterceptor(new ResponseDate());
httpproc.addInterceptor(new ResponseServer());
httpproc.addInterceptor(new ResponseContent());
httpproc.addInterceptor(new ResponseConnControl());
httpService = new HttpService(httpproc, new DefaultConnectionReuseStrategy(), new DefaultHttpResponseFactory());
registry = new HttpRequestHandlerRegistry();
registry.register(HOME_PATTERN, new HomeCommandHandler(context));
httpService.setHandlerResolver(registry);
}
public void runServer() {
try {
serverSocket = new ServerSocket(PORT);
serverSocket.setReuseAddress(true);
while (RUNNING) {
try {
final Socket socket = serverSocket.accept();
DefaultHttpServerConnection serverConnection = new DefaultHttpServerConnection();
serverConnection.bind(socket, new BasicHttpParams());
httpService.handleRequest(serverConnection, httpContext);
serverConnection.shutdown();
} catch (IOException e) {
e.printStackTrace();
} catch (HttpException e) {
e.printStackTrace();
}
}
serverSocket.close();
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
RUNNING = false;
}
public synchronized void startServer() {
RUNNING = true;
runServer();
}
public synchronized void stopServer() {
RUNNING = false;
if (serverSocket != null) {
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public Context getContext() {
return context;
}
public void setContext(Context context) {
this.context = context;
}
}
对于HomeCommandHandler:
package com.example.httpserver;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.entity.ContentProducer;
import org.apache.http.entity.EntityTemplate;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpRequestHandler;
import android.content.Context;
public class HomeCommandHandler implements HttpRequestHandler {
private Context context = null;
public HomeCommandHandler(Context context) {
this.context = context;
}
@Override
public void handle(HttpRequest request, HttpResponse response,
HttpContext httpContext) throws HttpException, IOException {
HttpEntity entity = new EntityTemplate(new ContentProducer() {
public void writeTo(final OutputStream outstream) throws IOException {
OutputStreamWriter writer = new OutputStreamWriter(outstream, "UTF-8");
String resp = "<html><head></head><body><h1>Home<h1><p>This is the homepage.</p></body></html>";
writer.write(resp);
writer.flush();
}
});
response.setHeader("Content-Type", "text/html");
response.setEntity(entity);
}
public Context getContext() {
return context;
}
}
我在平板电脑上安装了此应用程序,启动了该服务,然后我转到浏览器并键入http://127.0.0.1:8080/home.html
但未找到页面。我在哪里以及如何做错?