我正在使用AndroidAsync构建HTTP服务器 - https://github.com/koush/AndroidAsync。我设法显示我的index.html文件的内容,该文件存储在我的项目assets-folder中。
问题是只显示了一半的内容,因为在我的index.html文件中,我指的是.js和.css文件,我也放在assets-folder中。
我做错了什么?
以下是MainActivity.java的代码:
import android.content.Context;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.koushikdutta.async.AsyncServer;
import com.koushikdutta.async.http.server.AsyncHttpServer;
import com.koushikdutta.async.http.server.AsyncHttpServerRequest;
import com.koushikdutta.async.http.server.AsyncHttpServerResponse;
import com.koushikdutta.async.http.server.HttpServerRequestCallback;
import java.io.IOException;
import java.io.InputStream;
public class MainActivity extends AppCompatActivity {
Context mContext;
private AsyncHttpServer server = new AsyncHttpServer();
private AsyncServer myAsyncServer = new AsyncServer();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onResume() {
super.onResume();
startServer();
}
public String readFileAsString() {
AssetManager assetManager = getAssets();
String index = "Nothing found";
InputStream input;
try {
input = assetManager.open("index.html");
int size = input.available();
byte[] buffer = new byte[size];
input.read(buffer);
input.close();
index = new String(buffer);
} catch(IOException e) {
e.printStackTrace();
}
return index;
}
private void startServer() {
server.get("/", new HttpServerRequestCallback() {
@Override
public void onRequest (AsyncHttpServerRequest request, AsyncHttpServerResponse
response){
response.send(readFileAsString());
}
}
);
server.listen(myAsyncServer, 8080);
}
}
这是我的index.html文件:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet/v0.7.7/leaflet.css" />
<script src="http://cdn.leafletjs.com/leaflet/v0.7.7/leaflet.js"></script>
<script src="get_coordinates.js"></script>
</head>
<body onload="init()">
<div id="map"></div>
<button onClick="angle()">Calculate angles</button>
<button onClick="calcDistance()">Calculate distance</button>
<button onClick="sendData()">Send Data to phone</button>
</body>
答案 0 :(得分:0)
我最终决定将所有的javascript代码和样式放在index.html文件中,一切正常。