首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >android中的nanohttpd不起作用

android中的nanohttpd不起作用
EN

Stack Overflow用户
提问于 2013-07-17 02:24:09
回答 1查看 2.6K关注 0票数 3

我试着让nanohttpd在android下工作。我已经使用了这个例子:

代码语言:javascript
复制
package com.example.android_test;

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

import com.example.android_test.NanoHTTPD.Response.Status;

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

public class MainActivity extends Activity {
  private static final int PORT = 8765;
  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);
    }

    @Override
    public Response serve(String uri, Method method, Map<String, String> headers,
        Map<String, String> parms, Map<String, String> files) {
      final StringBuilder buf = new StringBuilder();
      for (Entry<String, String> kv : headers.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(Status.OK, MIME_HTML, html);
    }
  }
}

当我在我的手机上启动应用程序时,该活动显示正确的ip地址。我也可以ping所示的地址。但是,当我尝试通过浏览器访问时,该网站将无法加载。除了上面显示的MainActivity.java,我只添加了来自nanohttpd项目的NanoHTTPD.java文件。有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-20 05:34:47

我想到两件事,这两件事都是权限相关的。看一下您的android清单,您需要为您的应用程序提供两个权限

代码语言:javascript
复制
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

互联网,因为您正在访问网络服务;WRITE_EXTERNAL_STORAGE,因为当NanoHttpd接收到传入连接时,它会写入临时文件,并且大多数/所有电话都会将"java.io.tmpdir“映射到指向SD卡。

看看能不能帮上忙。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17684223

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档