- Forum posts: 3
Feb 18, 2018, 10:37:27 AM via Website
Feb 18, 2018 10:37:27 AM via Website
I am developing an application for both iOS and Android.
For iOS I am using objective-C and Xcode; for Android Java and Android Studio.
I should insert code to activate a remote javascript script, using a remote http link.
codiceisp.shinystat.com/cgi-bin/getcod.cgi?USER=myname
Every time this scrip is executed, a message is sent to a remote server to update statistical data.
In an htlm page this would be accoplished simply inserting a line like:
But in my case the script must be triggered from the app code.
For iOS I found the right solution: the code shown below works and statistical data are
correctly incremented when is is executed.
------------------- IOS CODE START-----------------------------------------------------------
static NSString *shinyC = @"codiceisp.shinystat.com/cgi-bin/getcod.cgi?USER=myname";
(void)shinySend {
result.text = @"";
NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration ephemeralSessionConfiguration];
NSURLSession *ephemeralSession = [NSURLSession sessionWithConfiguration:sessionConfig
delegate: self delegateQueue: [NSOperationQueue mainQueue]];
NSURL *url = [NSURL URLWithString:shinyC];
NSURLSessionDataTask * dataTask = [ephemeralSession dataTaskWithURL:url
completionHandler:^(NSData *data, NSURLResponse *response, NSError *tag) {
[self dataGot:data];
}];
[dataTask resume];
}(void)dataGot: (NSData *)data {
NSString *jscript = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
[webView evaluateJavaScript:jscript completionHandler:^(id name, NSError *error) {
if (error == nil) result.text = @"Success";
else result.text = [NSString stringWithFormat:@"%@",error];
}];
}
-------------------- IOS CODE END-------------------------------------------------------------
Then I tried an analog solution for Android, using the code shown below.
The shinySend method is executed in a Thread.
This apparently runs without errors, but statistical data are not incremented.
It seems that the script is not executed.
Does somebody have an idea for that ?
-------------------- ANDROID CODE START ------------------------------------------------------
static String shinyC = "//codiceisp.shinystat.com/cgi-bin/getcod.cgi?USER=myname";
void shinySend() {
WebView webView;
String result;
try {
URL url = new URL(shinyC);
InputStream ins = url.openStream();
result = IOUtils.toString(ins, StandardCharsets.UTF_8);
runOnUiThread(new Runnable() {
@Override
public void run() {
webView.getSettings().setJavaScriptEnabled(true);
webView.evaluateJavascript(result, new ValueCallback<String>() {
@Override
public void onReceiveValue(String s) {
Log.e("LogName", s);
}
});
}
});
} catch (Exception ex) {
Log.e("error", ex.toString());
}
}
-------------------- ANDROID CODE END --------------------------------------------------------
Recommended editorial content
With your consent, external content is loaded here.
By clicking on the button above, you agree that external content may be displayed to you. Personal data may be transmitted to third-party providers in the process. You can find more information about this in our Privacy Policy.