2016-04-05 9 views
0

Ben android telefon olarak sunucu olması gereken bir senaryo var ve bir modül bana bağlanır ve GET yöntemi (TCP/IP bağlantısı) üzerinden veri talep ediyor. Bağlantının bir parçası olmak çok kolay, benim sorunum şu GET yöntemini nasıl kullanmalıyım?Bir android sunucu olarak GET yanıtı ile veri göndermek için nasıl

class AsyncServerThread extends AsyncTask<Void, Void, Void> { 

    @Override 
    protected Void doInBackground(Void... voids) { 
     Socket socket = null; 
     try { 
      serverSocket = new ServerSocket(SERVERPORT); 
      Log.e("ServerThread", "Connected"); 
     } catch (final IOException e) { 
      Log.e("serverSocketExc", "?" + e.getMessage()); 
      runOnUiThread(new Runnable() { 
       @Override 
       public void run() { 
        Toast.makeText(getApplicationContext(), "serverSocketExc: " + 
            "?" + e.getMessage(), 
          Toast.LENGTH_LONG).show(); 
       } 
      }); 
      e.printStackTrace(); 
     } 
     Log.e("ServerThread", "listening for new connections"); 
     runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 
       Toast.makeText(getApplicationContext(), "ServerThread: listening for new connections.", 
         Toast.LENGTH_LONG).show(); 
      } 
     }); 
     while (shallContinue) { 

      try { 

       final Socket tempSocket = socket = serverSocket.accept(); 

       if (tempSocket.isConnected()) { 
        Log.e("socket", "accepted new Connection"); 
        runOnUiThread(new Runnable() { 
         @Override 
         public void run() { 
          Toast.makeText(getApplicationContext(), "accepted new Connection.", 
            Toast.LENGTH_LONG).show(); 
         } 
        }); 

        /*CommunicationThread commThread = new CommunicationThread(socket); 
        new Thread(commThread).start();*/ 
        Log.e("AsyncServerThread", "going to create client thread"); 
        runOnUiThread(new Runnable() { 
         @Override 
         public void run() { 
          new AsyncClientThread().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, tempSocket); 
         } 
        }); 

        out = new PrintWriter(new BufferedWriter(
          new OutputStreamWriter(socket.getOutputStream())), 
          true); 
       } else { 
        Log.e("socket", "no new Connection"); 
       } 

      } catch (IOException e) { 
       Log.e("accept", "?" + e.getMessage()); 
       e.printStackTrace(); 
      } 
     } 
     Log.e("ServerThread", "ended listening for new connection"); 
     runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 
       Toast.makeText(getApplicationContext(), "ServerThread: ended listening for new connection.", 
         Toast.LENGTH_LONG).show(); 
      } 
     }); 
     try { 
      serverSocket.close(); 
     } catch (IOException e) { 
      Log.e("close", "?" + e.getMessage()); 
      e.printStackTrace(); 
     } 
     return null; 
    } 
} 
+0

Kullanım URL parametreleri –

+0

net değil. Bir isteğin GET isteği olup olmadığını nasıl öğreneceğinizi mi soruyorsunuz? – dsharew

+0

@DegenSharew evet, soruyu düzenledim lütfen ona bakın ve evet, bir GET isteği olup olmadığını tespit etmek istiyorum. –

cevap

2

kelime, POST, HTTP Protokol dünya üzerinde bulunan ..etc PUT GET .
Ancak, http sunucusu olmayan bir sunucu soketi oluşturdunuz (Düşük düzeyli bir sunucu soketi ve http sunucu yuvası oluşturma arasında fark var).

Birkaç gün önce benzer bir senaryoya sahiptim ve nanohttpd'u kullandım. Android cihazınızda küçük httpd web sunucusu çalıştırmak için izin veren zarif bir kütüphanedir.

örnek kod:

public class HttpWebServer extends NanoHTTPD { 

    int port; 

    public HttpWebServer(int port) throws IOException { 
     super(port); 
     this.port = port; 
    } 

    public void startServer() throws IOException{ 

     start(); 
     Log.d(LOG_TAG, "visit http://localhost:" + port); 

    } 

    @Override 
    public Response serve(IHTTPSession session) { 


     Log.d(LOG_TAG, "Got http request."); 

     //gets method type; GET, POST, PUT..etc 
     Method method = ((HTTPSession) session).method 


    } 


} 

nasıl bağımlılık eklenir? GET isteğinde

dependencies { 
    compile 'com.nanohttpd:nanohttpd-webserver:2.1.1' 
} 
+1

Çok teşekkür ederim, bir arkadaşa sordum ve apache'yi ya da benim app'mda olduğu gibi bunun gibi birşeyi çalıştırmam gerektiğini söyledi, burada söylediğin gibi, kredi senin. –

+0

harika, yardım ettiğine sevindim – dsharew

İlgili konular