我遵循了将NanoHttpd设置为从这里服务文件的SO - 如何在Android中使用最新的mp3 2.3.0提供NanoHTTPD文件?。
这是可行的,但我需要使用Github的最新版本,因为它处理更多的HTTP方法,并且是项目所必需的。
我在本地构建了jar,并添加和编译了APK。web服务器初始化,但每个请求都以Not Found的形式返回。没别的了。也没有日志可以看到这个问题。怎么一回事?
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.util.Map;
import android.util.Log;
import org.nanohttpd.protocols.http.NanoHTTPD;
import org.nanohttpd.protocols.http.response.Response;
import org.nanohttpd.protocols.http.response.Status;
import org.nanohttpd.protocols.http.request.Method;
import static org.nanohttpd.protocols.http.response.Response.newChunkedResponse;
public class MainActivity extends AppCompatActivity {
public StackOverflowMp3Server server;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
server = new StackOverflowMp3Server();
try {
server.start();
} catch(IOException ioe) {
Log.w("Httpd", "The server could not start.");
}
Log.w("Httpd", "Web server initialized.");
}
@Override
public void onDestroy()
{
super.onDestroy();
if (server != null)
server.stop();
}
public class StackOverflowMp3Server extends NanoHTTPD {
public StackOverflowMp3Server() {
super(8089);
}
public Response serve(String uri, Method method,
Map<String, String> header, Map<String, String> parameters,
Map<String, String> files) {
String answer = "";
Log.w("HTTPD", uri);
Log.w("HTTPD", parameters.toString());
Log.w("HTTPD", "Method is: "+method.toString());
Log.w("HTTPD", "Header is: "+header.toString());
FileInputStream fis = null;
try {
fis = new FileInputStream("/storage/C67A-18F7/"
+ "/Music/"+uri);
Log.w("HTTPD", uri + " found");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return newChunkedResponse(Status.OK, "audio/mpeg", fis);
}
}
}同样的代码在2.2到2.3之间工作。
但不是最近的2.3.2
我在adb日志中获得服务器启动提示。
03-26 18:18:26.005 15056 15056 W Httpd : Web server initialized.
但是所有其他请求都返回Not Found
>$ curl -X GET http://192.168.1.2:8089
Not Found
>$ curl -X GET http://192.168.1.2:8089/demo.mp3
Not Found我找不到密码的问题所在了?
发布于 2018-03-26 13:14:45
您的serve()不覆盖NanoHTTPD正在调用的任何方法。默认实现返回"404 Not“。
serve()的签名是
protected Response serve(IHTTPSession session)不过,这是不可取的。看看在IHandler中引入的此承诺 s。(默认处理程序仍然调用不推荐的serve()方法。)
https://stackoverflow.com/questions/49492220
复制相似问题