2014-02-03 2 views
0

Я создаю сервер-рестартер в своем приложении для Android. Я использую API-интерфейс RESTlet android от restlet.org Я сделал большую часть его (отдых), я могу пойти в myAndroidPhoneIP:port/something/else, и он обработает мой getrequest без каких-либо проблем.Resteta, обслуживающий статический контент - Android

Теперь моя проблема заключается в обслуживании статических страниц. Если я перейду к m yAndroidPhoneIP:port/static/index.html, он загрузит файл index, но не images, css и такие внешние файлы.

Я вполне уверен, что проблема связана с классом маршрутизаторов, но я потратил целый день на то, чтобы найти ошибку, не имеющую успеха.

Мои статические страницы находятся в папке (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.

Вот скриншот, сделанный из хрома «Inspect Element». output from chrome

Большое спасибо!

ответ

0

Я думаю, проблема в том, что имя файла, используемое там, где загружен актив, неверно.

Что сообщает отчет журнала Log.d(TAG, "path: " + url + fileName);, когда URL-адрес: web_resources/css/bootstrap.min.css?

Стало ли (url + fileName) /web_resources/web_resources/css/bootstrap.min.css?

+0

Извините, я не понимаю, что вы имеете в виду. Не могли бы вы уточнить, пожалуйста? – dazito

+0

Не могли бы вы вывести выходные данные Log.d (TAG, "путь:" + url + fileName)? –

+0

Вот выход: 'путь: web_resources/index.html' – dazito

0

Я думаю, проблема в том, что вы прикрепляете Restlet, который будет обрабатывать статический контент.

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

Переменная uid по умолчанию захватывает один сегмент. URI, то есть все между двумя символами «/» или последним сегментом URI.

Если вы хотите поймать одно имя файла, например «index.html», и относительные пути, такие как «css/bootstrap.min».CSS», выполните одно из следующих вариантов:

  1. обновления типа переменного пути для того, чтобы поймать оставшуюся часть
  2. определяет два маршрута

То есть, в перспективе коды:

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); 

и проверьте переменную «dir» внутри вашего StaticContentRestlet.

Смежные вопросы