I need to know if there is anyway that I can use file_get_html or any equivalent function in php on GAE? I know it has something called URLFetch() but I am not able to understand how I will call that from a php file.
Any help?
You cannot run PHP on Google App Engine. You can create a servlet which will read from any given URL and manipulate the data in any way you would need to, in Java (since you tagged this question with the Java tag).
From the AppEngine URL Fetch Java API Overview:
URL url = new URL("http://www.example.com/atom.xml");
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
String line;
while ((line = reader.readLine()) != null) {
// ...
}
reader.close();
If you meant that you are running PHP in another application and you wish to call your AppEngine servlet from said PHP application, then you can map the servlet which performs this URL fetch to a URL within your AppEngine application, then hit that URL from your PHP application. This, however, seems like a bad design, as you're making two network calls when you could have just used done it within the PHP application in the first place.
Here's a quick and dirty wrapper function I created for URL Fetch using PHP via Quercus on Google App Engine:
function fetch_url($url){
import java.net.URL;
import java.io.BufferedReader;
import java.io.InputStreamReader;
$java_url = new URL($url);
$java_bufferreader = new BufferedReader(new InputStreamReader($java_url->openStream()));
while (($line = $java_bufferreader->readLine()) != null) {
$content .= $line;
}
return $content;
}
// Sample usage:
echo fetch_url('http://google.com');
Hope this helps someone who is as lost as I was.
Related
Hello guys im writing a web app in java with servlet, but i need for a job to use python, so im using Process.getRuntime().exec() for call the script.
My web app is a survey and between client compile it we take a photo of him.
I need python for deepface, for detection his emotion, and write all the results in a pdf file (what he choose, photos and detection of emotion result).
For 7 question in the survey the script works fine, when i put 8 question he never stop his job (the script working when isn't called from java i tested it).
Can you help me for understand how i can find the error? This process has got a limit of resources or something like that?
Process p = Runtime.getRuntime().exec("python "+rootPath+"\\DeepFaceLearning\\TestFace.py "+nomeFile);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
String temp ="";
while((temp = stdInput.readLine()) != null) {
System.out.println(temp);
}
For python the script is very long, but i think the problem is java beacuse he works fine when i run the script from command line with 40 questions.
I need to add 21 question in total.
Script python:
take a file read it and save the questions,reason e photos in a variables
analyze all photos
wirte all this information on a pdf
save pdf in db
Edit: java enter into script but don't complete all the job.
If the your python is working for 7 the same way as for 8 question, you could try to read the inputstream in other Thread.
Like:
...
new Thread(new Runnable(){
#Override
public void run(){
BufferedReader stdInput = new BufferedReader(newInputStreamReader(p.getInputStream()));
String temp ="";
while((temp = stdInput.readLine()) != null) {
System.out.println(temp);
}}).start();
I am looking for the approach of integrating mongo shell in my application. Wondering whether how to achieve this. There might be two cases,
I created a file which contain mongo query. Also create a list that contain all the credentials of mongodb. The goal of my application is to automatically run the mongo query from the file to mongo shell. I am stuck at how to call and run mongo shell from application.
Or, is there any way to bind mongo shell interpreter in my application? As like https://www.tutorialspoint.com/mongodb_terminal_online.php
My application is written in java .
you can run mongo shell from your java application like this, assuming your mongo binary is correctly linked. But it would be way better to use the java driver instead, as other said.
You can find more about executing mongo query from command line here :
https://docs.mongodb.com/v3.2/tutorial/write-scripts-for-the-mongo-shell/
package test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Test {
public static void main(String[] args) {
Runtime rt = Runtime.getRuntime();
try {
// the query you want to run in mongo, you can get it
// from a file using a FileReader
String query = "db.col.find();";
// the database name you need to use
String db = "database";
// run a command from terminal. this line is equivalent to
// mongo database --eval "db.col.find()"
// it calls the mongo binary and execute the javascript you
// passed in eval parameter. It should work for both unix and
// windows
Process pr = rt.exec(new String[]{"mongo", db, "--eval", query});
// read the output of the command
InputStream in = pr.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder out = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
out.append(line);
}
// print the command and close outputstream reader
System.out.println(out.toString());
reader.close();
} catch (IOException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
You can do the things you want using the Java MongoDB driver. This is the way MongoDB is intended to be accessed using Java and that is how I would do it. While I'm sure you could find a way to access the shell from Java, it seems like a hack.
I would use the Java driver unless you have a compelling reason to do otherwise. For your specific needs look at the Eval function.
Regarding your second example - I suspect that is just a web front end that is styled to look like a terminal instance. It is probably using whatever server side language driver to execute the commands.
I do have two questions where one would be solved after the first question.
1) I want to open simple URL in my company laptop with java but it does not let me open it. It gives connection timed out error. Would it be cause of the network settings that I need to make them in my java code also ? When I click the IE settings on LAN, there is no proxy settings but the configuration part is clicked and there is an adress http://wgate.company.entp.tgc:8080/wpad.dat . Please let me know what I need to do in java code.
My sample code
import java.net.*;
import java.io.*;
public class URLReader {
public static void main(String[] args) throws Exception {
URL oracle = new URL("http://www.google.com/");
BufferedReader in = new BufferedReader(
new InputStreamReader(oracle.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}
2) I do have a list of link pages to download Word files. I want to make a loop in java and I want java to go to these web pages and download files at once instead of me. Is there any special function or code to download a file in java ?
Thank you.
sample url I have
http://www.gooogle.com/attachments/change/TestCase_F257579.doc
Okay I know this should be dead simple but I guess I'm not phrasing my question correctly in my Google & stackoverflow searches.
I have a substantial amount of static data (6 megs) I need to load into my database upon install. Right now I'm fetching a json data file from my web server on first run and populating my database but that can be slow and something could go wrong. I'd prefer to just include the data file in the manifest and then load it on install or first run.
So, where do I put the file, make it so that it ends up on the target device, and then open it?
I've tried putting it in /res/files/ and then doing:
InputStream inputStream = new FileInputStream("/res/files/foo.json");
but of course I'd have been shocked if that had worked.
While I'm at it I should probably use CSV format instead as that would cut down the size but that's another story, I don't seem to have a way to parse it but I do know how to parse JSON data. Sorry I'm a bit new at this. Thanks!
You could store it either in assets or in res\raw.
How to open it from the assets folder:
InputStream is = getAssets().open("foo.json");
How to open it from the res\raw folder:
getResources().openRawResource(R.raw.foo);
I would advise you to use SQLite and/or XML. What #Gabriel suggested will most likely work fine, but loading and processing 6MBs may take some time -a time window of 1 to 5 secs to my experience. Since you downloaded from your webserver I believe your data has some form of structure and in your app you won't need all of the data at once.
Here are some guides/tutorials about SQLite in android, keep in mind that XML is also viable and some will probably advocate XML over SQLite in this case.
http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html
http://www.vogella.com/tutorials/AndroidSQLite/article.html
You can put your JSON file in the raw folder (res/raw) and load it with this code :
InputStream inputStream = getResources().openRawResource(R.raw.foo);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
inputStream.close();
JSONArray jArray = new JSONArray(sb.toString());
Then you can use your knowledge to parse the JSONArray.
I am working with java to get some information from a web page.
The problem is the information I need is generated by a JavaScript function. How can get this information because the code below brings only page information before full loaded (which means I can get only frames of the page).
code1.
URL target = new URL()
HttpURLConnection con = (HttpURLConnection)target.openConnection();
StringBuffer sb = new StringBuffer();
String line = "";
BufferedReader br = null
try {
br = new BufferedReader(new InputStreamReader(con.getInputStream()));
while((line = br.readLine()) != null){
sb.append(line);
}
} catch(Exception e){
e.printStackTrace();
}
Is there a way to know when the page has fully loaded in java? (Extra library can be an answer, but I wish to do it in java only). Thanks.
Your are making an HTTP request from java, this returns a text stream, the concept of "page loaded" is a browser related concept, the browser requests the content of the page (same as you are doing) and then renders the page and executes Javascript. It's the browser that executes Javascript.
If you want to make this only in Java, you need to implement a headless browser (a browser without user interface), or at least get the Javascript in the page you are loading and executing this. Doing this from scratch in pure Java is not an easy task, check out HtmlUnit for an example.
Java won't execute any client-side JavaScript. It will just read it. If you want a browser, use a browser.