Restlet提供静态内容 - Android

时间:2014-02-03 17:33:10

标签: android rest uri restlet

我正在我的Android应用程序上构建一个restlet服务器。我正在使用来自restlet.org的RESTlet android API 我已经完成了大部分工作(休息),我可以转到myAndroidPhoneIP:port/something/else,它会处理我的get request而没有任何问题。

现在我的问题在于提供静态页面。如果我转到yAndroidPhoneIP:port/static/index.html,则会加载index文件,但不会加载imagescss以及此类外部文件。

我很确定这个问题与路由器类有关,但我花了一整天的时间试图找到错误而没有成功。

我的静态页面位于(ecplise)资产文件夹/assets/web_resources

这是我的代码:

我的router班级

public class ContactsRouter extends Router {

    private static final String TAG = "ContactsRouter";

    private final ContactRestlet contactRestlet;
    private final DefaultTargetRestlet defaultTargetRestlet;
    private final ContactListRestlet contactListRestlet;
    private final PhotoRestlet photoRestlet;
    private final PhotoThumbnailRestlet photoThumbnailRestlet;
    private final StaticContentRestlet staticContentRestlet;

    public ContactsRouter(Context appContext ) {
        contactRestlet = new ContactRestlet(appContext);
        defaultTargetRestlet = new DefaultTargetRestlet();
        contactListRestlet = new ContactListRestlet(appContext);
        photoRestlet = new PhotoRestlet(appContext);
        photoThumbnailRestlet = new PhotoThumbnailRestlet(appContext);
        staticContentRestlet = new StaticContentRestlet(appContext);

        this.attach("/contacts", contactListRestlet); // To list all contacts
        this.attach("/contacts/{uid}", contactRestlet); // To show details about a specific contact
        this.attach("/contact/{uid}/photo", photoRestlet); // {uid} must correspond to a valid raw contact id
        this.attach("/contact/{uid}/photo/thumbnail", photoThumbnailRestlet);
        this.attach("/web_resources/{uid}", staticContentRestlet); // Filename is the static file to send to the user's browser
        //this.attachDefault(defaultTargetRestlet);
    }

我的StaticContent类

public class StaticContentRestlet extends Restlet{

private Context context;
private static final String TAG = "StaticContent";
private static final String url = "web_resources/";

public StaticContentRestlet(Context context)
{
    this.context = context;
}



@Override
public void handle(Request request, Response response) {

    String type = request.getMethod().getName();
    String fileName = (String) request.getAttributes().get("uid");

    if(type.equalsIgnoreCase("get"))
    {

        try {
            Log.d(TAG, "path: " + url + fileName);
            Representation r = readStaticFile(url + fileName);
            response.setEntity(r);
        } catch (NotFoundException e) {
            Log.e(TAG, e.getMessage(), e);
            response.setStatus(new Status(Status.CLIENT_ERROR_NOT_FOUND, e.getMessage()));
        } catch (IOException e) {
            Log.e(TAG, e.getMessage(), e);
            response.setStatus(new Status(Status.SERVER_ERROR_INTERNAL, e.getMessage()));
        } 
    }

}





public Representation readStaticFile(String fileName) throws NotFoundException, IOException
{
    InputStream is = context.getAssets().open(fileName);
    Representation representation = new InputRepresentation(is);

    return representation;
}

}

这是我在logCat中得到的输出:

GET /web_resources/index.html   -   200 -   0   24  http://10.17.1.72:8080  Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.102 Safari/537.36   -
GET /web_resources/css/bootstrap.min.css    -   404 439 0   5   http://10.17.1.72:8080  Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.102 Safari/537.36   http://10.17.1.72:8080/web_resources/index.html
GET /web_resources/css/bootstrap-fileupload.css -   404 439 0   4   http://10.17.1.72:8080  Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.102 Safari/537.36   http://10.17.1.72:8080/web_resources/index.html
GET /web_resources/css/unicorn.grey.css -   404 439 0   7   http://10.17.1.72:8080  Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.102 Safari/537.36   http://10.17.1.72:8080/web_resources/index.html
GET /web_resources/css/unicorn.main.css -   404 439 0   6   http://10.17.1.72:8080  Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.102 Safari/537.36   http://10.17.1.72:8080/web_resources/index.html
GET /web_resources/css/bootstrap-formhelpers.min.css    -   404 439 0   2   http://10.17.1.72:8080  Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.102 Safari/537.36   http://10.17.1.72:8080/web_resources/index.html
GET /web_resources/css/bootstrap-responsive.min.css -   404 439 0   14  http://10.17.1.72:8080  Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.102 Safari/537.36   http://10.17.1.72:8080/web_resources/index.html
GET /web_resources/css/jquery.pnotify.default.css   -   404 439 0   3   http://10.17.1.72:8080  Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.102 Safari/537.36   http://10.17.1.72:8080/web_resources/index.html
GET /web_resources/css/font-awesome.css -   404 439 0   3   http://10.17.1.72:8080  Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.102 Safari/537.36   http://10.17.1.72:8080/web_resources/index.html
GET /web_resources/css/jquery.pnotify.default.icons.css -   404 439 0   2   http://10.17.1.72:8080  Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.102 Safari/537.36   http://10.17.1.72:8080/web_resources/index.html
GET /web_resources/js/excanvas.min.js   -   404 439 0   3   http://10.17.1.72:8080  Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.102 Safari/537.36   http://10.17.1.72:8080/web_resources/index.html
GET /web_resources/css/main.css -   404 439 0   3   http://10.17.1.72:8080  Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.102 Safari/537.36   http://10.17.1.72:8080/web_resources/index.html
GET /web_resources/js/jquery.min.js -   404 439 0   8   http://10.17.1.72:8080  Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.102 Safari/537.36   http://10.17.1.72:8080/web_resources/index.html
GET /web_resources/js/jquery-ui.custom.min.js   -   404 439 0   2   http://10.17.1.72:8080  Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.102 Safari/537.36   http://10.17.1.72:8080/web_resources/index.html
GET /web_resources/js/bootstrap.min.js  -   404 439 0   2   http://10.17.1.72:8080  Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.102 Safari/537.36   http://10.17.1.72:8080/web_resources/index.html
GET /web_resources/js/jquery.tr.js  -   404 439 0   6   http://10.17.1.72:8080  Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.102 Safari/537.36   http://10.17.1.72:8080/web_resources/index.html
GET /web_resources/js/bootstrap-datepicker.js   -   404 439 0   2   http://10.17.1.72:8080  Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.102 Safari/537.36   http://10.17.1.72:8080/web_resources/index.html
GET /web_resources/js/unicorn.js    -   404 439 0   3   http://10.17.1.72:8080  Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.102 Safari/537.36   http://10.17.1.72:8080/web_resources/index.html
GET /web_resources/js/jquery.nicescroll.min.js  -   404 439 0   12  http://10.17.1.72:8080  Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.102 Safari/537.36   http://10.17.1.72:8080/web_resources/index.html
GET /web_resources/js/dictionary.js -   404 439 0   2   http://10.17.1.72:8080  Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.102 Safari/537.36   http://10.17.1.72:8080/web_resources/index.html
GET /web_resources/js/breadcrumbs-obj.js    -   404 439 0   2   http://10.17.1.72:8080  Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.102 Safari/537.36   http://10.17.1.72:8080/web_resources/index.html
GET /web_resources/js/jquery.pnotify.min.js -   404 439 0   2   http://10.17.1.72:8080  Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.102 Safari/537.36   http://10.17.1.72:8080/web_resources/index.html
GET /web_resources/js/jquery.cookie.js  -   404 439 0   2   http://10.17.1.72:8080  Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.102 Safari/537.36   http://10.17.1.72:8080/web_resources/index.html
GET /web_resources/js/jquery.form.js    -   404 439 0   2   http://10.17.1.72:8080  Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.102 Safari/537.36   http://10.17.1.72:8080/web_resources/index.html
GET /web_resources/js/angular.min.js    -   404 439 0   5   http://10.17.1.72:8080  Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.102 Safari/537.36   http://10.17.1.72:8080/web_resources/index.html
GET /web_resources/js/angular-route.min.js  -   404 439 0   3   http://10.17.1.72:8080  Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.102 Safari/537.36   http://10.17.1.72:8080/web_resources/index.html
GET /web_resources/js/angular-resource.min.js   -   404 439 0   2   http://10.17.1.72:8080  Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.102 Safari/537.36   http://10.17.1.72:8080/web_resources/index.html
GET /web_resources/js/angular-sanitize.min.js   -   404 439 0   3   http://10.17.1.72:8080  Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.102 Safari/537.36   http://10.17.1.72:8080/web_resources/index.html
GET /web_resources/js/transfercontacts/services.js  -   404 439 0   3   http://10.17.1.72:8080  Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.102 Safari/537.36   http://10.17.1.72:8080/web_resources/index.html
GET /web_resources/js/transfercontacts/transfercontacts.js  -   404 439 0   4   http://10.17.1.72:8080  Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.102 Safari/537.36   http://10.17.1.72:8080/web_resources/index.html
GET /web_resources/js/transfercontacts/module.js    -   404 439 0   2   http://10.17.1.72:8080  Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.102 Safari/537.36   http://10.17.1.72:8080/web_resources/index.html
GET /web_resources/img/loader.gif   -   404 439 0   3   http://10.17.1.72:8080  Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.102 Safari/537.36   http://10.17.1.72:8080/web_resources/index.html

正如您所看到的,它正在尝试加载css,js等文件但它无法加载它们。我在所有这些上都获得了404,只加载了index.html。

以下是Chrome“Inspect Element”的截图。output from chrome

非常感谢!

2 个答案:

答案 0 :(得分:0)

我认为问题在于加载资产时使用的文件名不正确。

当网址为:web_resources / css / bootstrap.min.css时,日志报告Log.d(TAG, "path: " + url + fileName);会报告什么?

(url + fileName)是否成为/web_resources/web_resources/css/bootstrap.min.css?

答案 1 :(得分:0)

我认为问题在于您附加将处理静态内容的Restlet。

=> attach("/web_resources/{uid}", staticContentRestlet);

默认情况下,uid变量捕获单个段。一个URI,也就是说两个“/”字符之间的所有内容或URI的最后一段。

如果要捕获单个文件名(如“index.html”)和相对路径(如“css / bootstrap.min.css”),请选择以下选项之一:

  1. 更新路径变量的类型以捕获剩余部分
  2. 定义两条路线
  3. 也就是说,就代码而言:

    1:

    TemplateRoute route = this.attach("/web_resources/{uid}", staticContentRestlet);
    Variable var = route.getTemplate().getVariables().get("uid");
    var.setType(Variable.TYPE_ALL);
    

    2:

    this.attach("/web_resources/{uid}", staticContentRestlet);
    this.attach("/web_resources/{dir}/{uid}", staticContentRestlet);
    

    并检查StaticContentRestlet中的“dir”变量。