I am using an embedded http server from the following example on
https://github.com/nikkiii/embedhttp
The http server code is initiated as
File filesDir = getFilesDir();
HttpServer server = new HttpServer();
server.addRequestHandler(new HttpStaticFileHandler(new File(".")));
server.addRequestHandler(new HttpStaticFileHandler(filesDir));
server.bind(8081);
// Start the server thread
server.start();
The http server is embedded so as to serve a www folder from the server as http://localhost:8081/folderpath/index.html . Then in turn I will be using WebView to access the localhost url.
In webview I try to access.
myWebView.loadUrl("`http://localhost:8081/`");
myWebView.loadUrl("`http://localhost:8081/index.html`");
myWebView.loadUrl("`http://localhost:8081/www/index.html`");
myWebView.loadUrl("`http://localhost:8081/assets/www/index.html`");
But none of them work. What is the probable solution to such a situation where the assets/www flder needs to be served from localhost running on android. Thanks.
The Android assets folder is not available via getFilesDir(): getFilesDir() returns a different directory. To serve resources in the assets folder you'll need to use AssetManager (http://developer.android.com/reference/android/content/res/AssetManager.html) and do a little translation from your request paths to the appropriate resource.
Related
Hello I am building my first android app, I am currently trying to post data to a URL on my local server. When I try with my live server it works however with my local server it does not, nothing happens at all, I get no response.
This is the route in my app on my localhost: http://admin.website.dev
I changed my hosts file and virtual host configuration on my localhost so that any address with the extension ".dev" points to a folder on my localhost. eg. "localhost/website"
Is there a reason this does not work in my android app when I try posting data?
I have already added the INTERNET permission in my manifest file.
Since android 5, you need to make a POST on DNS valid URL.
I'm not sure if your changes on hosts file will work.
I've stored my .apk file in my rails server. I've set up a route that redirects a given url to a method that essentially sends the file
in my routes.rb file
match '/myApk.apk' to: 'upgradeapk#index'
Upgradeapk_controller.rb file
def index
#filename = '/myApk/myApk.apk'
#tmpfile = 'upgradedApk.apk'
send_file(#filename, :disposition => 'inline', :stream => true, :type=> 'application/vnd.android.package-archive', :file_name => #tmpfile)
end
When i type my sever url and add '/myApk.apk' it starts the downloading process as long as i do it on my a computer. However if i try to do it on my android device it doesn't work. Checking the download lists in my android device browser i notice that the download "job" for the apk is created, however its in an endless loop changing between states "in queue" and "downloading". Nothing ever downloads.
Do I have to set the send_file differently when it comes to making it work on android devices?
----EDIT------
Ok so i've decided to store the files in a dropbox location instead of storing it in my server. If i pass the url for the file directly in my android function for the http request, it works well. The file is found, downloaded and the installation is prompted.
class UpgradeapkController < ApplicationController
def index
android_apk = Androidaplicacion.first (Model to access the table in which i store the apk dropbox url)
route = android_apk.url
redirect_to route
end
end
I've set up my controller to redirect to the given url for the dropbox file. If i try the url (same as before, using the "match" url) in my android browser, this time it downloads. However, if I try it from the android app, its the same as before, it just doesnt download.
So, the "send_file" method seems to not be working if its on the android platform. redirecting to my dropbox url from the controller works on android but only from a browser, not using the http request on my android app. The only way to get it to work in my android app is if I use the dropbox direct link.
Also, I first thought this was because my server was running on https and the certificate its not a valid one. I found a way to bypass the https certification encryption/certificate validation thing on my android app but it didnt work either (it appeared to have succeed in avoiding the validation) but the results ended up been the same. I then ran an instance of my server using http and still same results.
I was wondering if it was possible to download a file without giving the specific file URL.
For instance, making an http request and inside the response were the file or the content of the file.
I'm able to download a file already giving the exact URL.
For instance, txt file.
But, what I want next is to protect that URL and make it private.
If there is another way, then please explain me
There is a way to facilitate server-client communication without using http request, referred to as Apache Thrift. To achieve the goal you asked - "Download a file from server without giving the file URL", you can do as follow:
Download Thrift, Build and Install Apache Thrift compiler
You can follow steps on official website.
Writing a .thrift file (For my example, I named it as "test.thrift")
For your example, you can define as below:
service MyServer {
binary getFileFromServer()
}
Then, open you terminal and run
thrift --gen java test.thrift
I'll get a "MyServer.java" file and both server/client sides should include this file for implementing. Assuming your server side is prepared for providing file data as binary. In your Android app, you just need to specify the socket, protocol, transport, host, and port for connecting server. And if you would like to get file from server, just using your Client object to call "getFileFromServer()", then you can get the byte[] of your file. For instance:
public TSocket socket = new TSocket(your_host, your_port);
public TFramedTransport transport = new TFramedTransport(socket);
public TBinaryProtocol protocol = new TBinaryProtocol(transport);
public Client myClient = new Client(protocol);
call a thread to get file from server:
new Thread(new Runnable() {
#Override
public void run(){
// this block should be wrapped with try/catch
transport.open();
byte[] myFileInByteArray = myClient.getFileFromServer();
// your custom function for decoding
decodeByteArrayIntoFile(myFileInByteArray);
transport.close();
}
}).start();
The above is the only way I can think of to download a file from server without giving the file URL, but your still need to connect to your server first. Hope this can help.
I am trying to connect to the server from android through tomcat server. There is a .class file which should be eecuted. I have placed this in C:/xampp/tomcat/webapps/examples/WEB-INF/classes/LoginServlet.java
to access this from android, I have given the statement,
response = SimpleHttpClient.executeHttpPost("http://10.0.2.2:8080/webapps/examples/WEB- INF/classes/LoginServlet", postParameters);
Is that corrext? Do I have to place it in a different location or change the path above ?
If I want to access folders that are uploaded on a server, how would I access it from the Android project? I tried to use the following
public URL(String protocol, String host, String file)
My Codes:
URL url = new URL("file", "www.websitename.com", "/folderX");
MyClassName path = new MyClassName(url);
Seems like it couldn't access the folder on the server still. Is there anything that I miss or I should be using some other ways? There are no errors in the logcat so I am not sure what is it. No MalformedURLException being thrown as well.
I you want to access the folder and its files from your server you need FTP account details to connect to your server from your android device.There are more FTP Client libraries available to access the files one of them is
http://commons.apache.org/net/download_net.cgi