nanohttpd android系统中无法正常工作 [英] nanohttpd in android wont work

查看:189
本文介绍了nanohttpd android系统中无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让nanohttpd下机器人的工作。我用这个例子:

I tried to get nanohttpd working under android. I have used this example:

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文件。任何想法?

When i start the application on my phone the activity shows the proper ip address. I also can ping the shown address. But when i try to access this via a browser the site will not load. In addition the the above shown MainActivity.java i have only added the NanoHTTPD.java file from the nanohttpd project. Any ideas?

推荐答案

两件事情浮现在脑海中,都是相关的权限。看看你的Andr​​oid清单,你会希望你的应用程序两种权限

Two things come to mind, both are permissions related. Take a look at your android manifest, you'll want two permissions for your app

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

INTERNET,因为你所访问的网络服务,并WRITE_EXTERNAL_STORAG​​E因为当NanoHttpd接收写入临时文件和大多数/所有手机地图的java.io.tmpdir指向在SD卡上的入站连接。

INTERNET, because you're accessing network services, and WRITE_EXTERNAL_STORAGE because when NanoHttpd receives an incoming connection it writes temp files and most / all phones map the "java.io.tmpdir" to point at the SD card.

看看是否有帮助。

这篇关于nanohttpd android系统中无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆