如何识别使用Android执行不同操作的不同URL请求

时间:2013-08-13 05:00:20

标签: android webserver

我正在尝试实现可以​​识别不同网址请求的代码,并针对每个请求执行不同的操作,例如,通过访问http://192.168.0.120/pic来拍照,并通过http://192.168.0.120/email访问来发送电子邮件 我已经构建了拍照和发送电子邮件的代码,但不确定如何将它们分配给不同的URL请求? 我发现一个代码可以运行Web服务器只识别一个IP地址,我想修改它以识别多个IP地址并对每个请求执行不同的操作: 守则:

import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.LinkedList;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

public class Server extends Thread {
    private ServerSocket listener = null;
    private static Handler mHandler;
    private boolean running = true;

    public static LinkedList<Socket> clientList = new LinkedList<Socket>();

    public Server(String ip, int port, Handler handler) throws IOException {
        super();
        mHandler = handler;
        InetAddress ipadr = InetAddress.getByName(ip);
        listener = new ServerSocket(port,0,ipadr);
    }

    private static void send(String s) {
        Message msg = new Message();
        Bundle b = new Bundle();
        b.putString("msg", s);
        msg.setData(b);
        mHandler.sendMessage(msg);
    }

    @Override
    public void run() {
        while( running ) {
            try {

                Socket client = listener.accept();


                new ServerHandler(client).start();
                LockStatus.getInstance().setMyVar(true);


                clientList.add(client);

            } catch (IOException e) {

            }
        }
    }

    public void stopServer() {
        running = false;
        LockStatus.getInstance().setMyVar(false);
        try {
            listener.close();
        } catch (IOException e) {

        }
    }

非常感谢

1 个答案:

答案 0 :(得分:0)

以下是代码的修改,但仍然无法识别IP地址:

public void run() {
        try {
            serverSocket = new ServerSocket(SERVERPORT);

            while (running) {
                // LISTEN FOR INCOMING CLIENTS
                Socket client = serverSocket.accept();

                try {
                    BufferedReader in = new BufferedReader(
                            new InputStreamReader(client.getInputStream()));
                    String line = null;
                    String IP = "192.168.0.111";
                    line = in.readLine();
                    if (line.equals(IP)) {
                        new ServerHandler(client).start();
                        LockStatus.getInstance().setMyVar(true);
                        Log.i(TAG, "IP Receive=" + line);
                        // Toast.makeText(getContext(), "Matches",
                        // Toast.LENGTH_SHORT).show();
                    } else {
                        Log.i(TAG, " IP not received :=" + line);
                        // Toast.makeText(getApplicationContext(), line +" != "+
                        // IP, Toast.LENGTH_SHORT).show();

                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }