2013-10-03 3 views
0

nanoHTTPD로 안드로이드의 간단한 예제를 실행하는 것에 지쳤습니다. 에뮬레이터에서 프로그램을 실행하면 http://xxx.xxx.xxx.xxx:8080이 표시됩니다. 장치에서 동일한 프로그램을 실행하면 IP 주소 http://xxx.xxx.xxx.xxx:8080이 표시됩니다. 모바일 브라우저와 웹 브라우저에서 해당 IP를 시도했습니다. 그것은 페이지가 오류를 표시 할 수 없습니다 보여줍니다. 나는이 예 https://gist.github.com/komamitsu/1893396Android NANOHTTPD 문제가 있습니까?

이 내 코드를 따랐다.

package com.komamitsu; 

import java.io.IOException; 
import java.util.Map.Entry; 
import java.util.Properties; 

import android.app.Activity; 
import android.net.wifi.WifiManager; 
import android.os.Bundle; 
import android.os.Handler; 
import android.widget.TextView; 

public class AndroidWebServerActivity extends Activity { 
    private static final int PORT = 8080; 
    private TextView hello; 
    private MyHTTPD server; 
    private Handler handler = new Handler(); 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    hello = (TextView) findViewById(R.id.hello); 
    } 

    @Override 
    protected void onResume() { 
    super.onResume(); 

    TextView textIpaddr = (TextView) findViewById(R.id.ipaddr); 
    WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE); 
    int ipAddress = wifiManager.getConnectionInfo().getIpAddress(); 
    final String formatedIpAddress = String.format("%d.%d.%d.%d", (ipAddress & 0xff), (ipAddress >> 8 & 0xff), 
     (ipAddress >> 16 & 0xff), (ipAddress >> 24 & 0xff)); 
    textIpaddr.setText("Please access! http://" + formatedIpAddress + ":" + PORT); 

    try { 
     server = new MyHTTPD(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    } 

    @Override 
    protected void onPause() { 
    super.onPause(); 
    if (server != null) 
     server.stop(); 
    } 

    private class MyHTTPD extends NanoHTTPD { 
    public MyHTTPD() throws IOException { 
     super(PORT, null); 
    } 

    @Override 
    public Response serve(String uri, String method, Properties header, Properties parms, Properties files) { 
     final StringBuilder buf = new StringBuilder(); 
     for (Entry<Object, Object> kv : header.entrySet()) 
     buf.append(kv.getKey() + " : " + kv.getValue() + "\n"); 
     handler.post(new Runnable() { 
     @Override 
     public void run() { 
      hello.setText(buf); 
     } 
     }); 

     final String html = "<html><head><head><body><h1>Hello, World</h1></body></html>"; 
     return new NanoHTTPD.Response(HTTP_OK, MIME_HTML, html); 
    } 
    } 
} 

미리 감사드립니다. 누구나 올바른 결과를 얻기 위해 내가해야하는 변화를 말해 줄 수 있습니까?

답변

0

당신은 ... 실제로 서버

0

난 당신이 서버를 시작하는 것을 잊었다 생각을 시작 server.start()를 호출 할 필요가

try { 
     server = new MyHTTPD(); 
     server.start(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
관련 문제