I'm using this method to read RSS Feeds from URL. Everything works fine except it fails to get feeds from .net webserver (eg. http://www.dotnetnuke.com/Resources/Blogs/rssid/99.aspx).
public String getRSSLinkFromURL(String url) {
// RSS url
String rss_url = null;
try {
// Using JSoup library to parse the html source code
org.jsoup.nodes.Document doc = Jsoup.connect(url).get();
// finding rss links which are having link[type=application/rss+xml]
org.jsoup.select.Elements links = doc.select("link[type=application/rss+xml]");
Log.d("No of RSS links found", " " + links.size());
// check if urls found or not
if (links.size() > 0) {
rss_url = links.get(0).attr("href").toString();
} else {
// finding rss links which are having link[type=application/rss+xml]
org.jsoup.select.Elements links1 = doc.select("link[type=application/atom+xml]");
if(links1.size() > 0){
rss_url = links1.get(0).attr("href").toString();
}
}
} catch (IOException e) {
e.printStackTrace();
}
// returing RSS url
return rss_url;
}
You RSS feed is broken: transfer closed with outstanding read data remaining.
curl will return that message when the socket has been closed before
the final terminating chunk of a chunky transfer is read. It sure
sounds like a server bug to me.
Source: [Re: transfer closed with outstanding read data remaining with Expect: 100-continue][1]
Fix (workaround) for JSoup is here:
https://github.com/jhy/jsoup/pull/323
Related
I cannot for the life of me how to figure out the most simple task of retrieving a text file from a url and reading its contents. All the code I find is 5-12 years old and doesn't work. Since android api 30+ any network request on main thread give a android.os.NetworkOnMainThreadException.
I'm stuck using kotlin for this portion.
I cant use DownloadManager (unless there is a way to store the file temporarily and retrieve contents) as the file doesnt need to be downloaded to the local storage only read.
The closest ive seen is from:
Android - How can I read a text file from a url?
try {
// Create a URL for the desired page
URL url = new URL("mysite.com/thefile.txt");
// Read all the text returned by the server
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str;
while ((str = in.readLine()) != null) {
// str is one line of text; readLine() strips the newline character(s)
}
in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}
But this code doesn't work.
So after 3 days of wanting to jump off a cliff. I found the answer. Of course it was a few minutes after asking the question here (first question ever so be kind.). The only Issue is you need a SSL cert for HTTPS on the server retrieving the file. My server is http but i can get a cert in there and fix that. to Test i threw up a github repository and linked to the raw text file. Here is my solution if this saves you 3 days pour one out for me.
Thread {
try {
val url = URL("https://raw.githubusercontent.com/USERNAME/NeedHTTPSdontWanaSSL/main/info.txt")
val uc: HttpsURLConnection = url.openConnection() as HttpsURLConnection
val br = BufferedReader(InputStreamReader(uc.getInputStream()))
var line: String?
val lin2 = StringBuilder()
while (br.readLine().also { line = it } != null) {
lin2.append(line)
}
Log.d("The Text", "$lin2")
} catch (e: IOException) {
Log.d("texts", "onClick: " + e.getLocalizedMessage())
e.printStackTrace()
}
}.start()
Credit:
answered Aug 12, 2018 at 9:29
Aishik kirtaniya
Android - How can I read a text file from a url?
I am trying to get the headings of Google search with Jsoup.
Here is my code:
String request = "https://www.google.com/search?q=" + query + "&num=5";
try {
Document doc = Jsoup
.connect(request)
.userAgent(
"Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)")
.timeout(5000).get();
Elements headings = doc.select("h3");
//headings array is empty
} catch (IOException e) {
e.printStackTrace();
}
I get no results from doc.select("h3"). What am I doing wrong?
Check your Document's content, perhaps the request didn't go through properly or the result is different from your browser.
I'm building hybrid applications that rely on 2-way communication between javascript in a webview and the hosting application.
Attitudes differ somewhat as in IOS the JS can send a message to swift (using WKWebView), that listens through
userContentController(userContentController: WKUserContentController,
didReceiveScriptMessage message: WKScriptMessage)
when implementing the WKScriptMessageHandler protocol,
whereas in Android the JS can actually call an Android method that has #JavascriptInterface annotation, after calling addJavascriptInterface().
Both approaches are OK for me, as I'm passing around data using JSON strings. Question is, what if I need to pass a media file, say an image or video, from the web page to the application? should I just pass a bitmap inside the json? Seems a little naive... recommendations?
edit: when passing an image from the application to the webpage I save the file to the file system and send the filename to the webview. Can it be done the other way around? Can javascript save to the hosting mobile device file system?
You have to host(in case of webapp) or store(in case of mobile app) the image and pass the image url, not exactly the image.
Almost all api that uses images bitmap also takes image url.
regards
Ashish
To answer your second question which is there are comments, use the following code.
Here the html content is your binary content:
FileWriter imageFileWriter = null;
BufferedWriter imageBufferedWriter = null;
ABOUtil.createDir(InMemoryDataStructure.FILE_PATH.getFileDirForimage());
File imageFileDir = new File(InMemoryDataStructure.FILE_PATH.getFileDirForimage());
String imageName = "/finalimage"+ filename + jpg
File mimageFile = new File(imageFileDir, imageName);
try {
imageFileWriter = new FileWriter(mimageFile, false);
imageBufferedWriter = new BufferedWriter(imageFileWriter);
StringBuilder sb = new StringBuilder();
sb.append(htmlContent);
sb.append(scriptInjectJavascript(lstimageNameValue));
imageBufferedWriter.write(sb.toString());
imageBufferedWriter.close();
return mimageFile;
}
catch (IOException e) {
MAFLogger.e("", "", e);
}
finally{
if(imageFileWriter!=null)
try {
imageFileWriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
MAFLogger.e("","",e);
}
if(imageBufferedWriter!=null)
try {
imageBufferedWriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
MAFLogger.e("","",e);
}
}
I'm thinking about making my first android app, It'd be about movies, I found an excellent data source, it is "http://www.google.com/movies?" but I wanted to know how could I extract this information and put it in my app,
I've searched but I don't know which is the optimal way to do this? does google have an API for this? is that what I want? is it better with the source code?what could I read or see to learn to do this?
thanks a lot guys, Is my first time as well programming retrieving information from the cloud,
cheers
Yup. Here is one way to do it.
First, you need to find the source of the SQL. The Yahoo Developer Console is a great place to look for this sort of stuff. It has EVERYTHING. The way these resources work is that you have a long link, like this....
developer.yahoo.com/blah/this . . . &q=KEYWORD_HERE+blah/ . . .
To access the information you are looking for, you stick whatever the correct keyword is where "KEYWORD_HERE" is, and the link will give you info in SQL format. I'll be doing the example as a stocks app.
First you create an Activity and define both sides of your link as strings. It'll look a bit like this:
public class InfoActivity extends Activity {
String firstHalf = "http://query.yahooapis.com/v1/public/blahblahblah&q=";
String secondHalf = "+blah/blah&blah . . . ";
Then, in your onCreate, you'll need to start an aSync task to do the actual pulling and parsing:
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.id.layout_name);
final String yqlURL = firstHalf + KEYWORD_HERE + secondHalf;
new MyAsyncTask().execute(yqlURL);
}
Then to define our MrAsyncTask:
private class MyAsyncTask extends AsyncTask<String, String, String>{
protected String doInBackground(String... args) {
try {
URL url = new URL(args[0]);
URLConnection connection;
connection = url.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection)connection;
int responseCode = httpConnection.getResponseCode();
// Tests if responseCode == 200 Good Connection
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream in = httpConnection.getInputStream();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document dom = db.parse(in);
Element docEle = dom.getDocumentElement();
NodeList nl = docEle.getElementsByTagName("nodeName1");
if (nl != null && nl.getLength() > 0) {
for (int i = 0 ; i < nl.getLength(); i++) {
//Parse the node here with getTextValue(n1, "Name of element")
//ex: String movieName = getTextValue(n1, "MovieName");
}
}
}
} catch (MalformedURLException e) {
Log.d(TAG, "MalformedURLException", e);
} catch (IOException e) {
Log.d(TAG, "IOException", e);
} catch (ParserConfigurationException e) {
Log.d(TAG, "Parser Configuration Exception", e);
} catch (SAXException e) {
Log.d(TAG, "SAX Exception", e);
}
finally {
}
return null;
}
I hope that gives you some idea of how to do this sort of thing. I'll go see if I can quickly spot a good resource on the yahoo apis to get the movie times at a certain location.
Good luck :) Let me know if you need anything clarified.
EDIT:
Looks like this is EXACTLY what you need (resource wise):
https://developer.yahoo.com/yql/console/?q=show%20tables&env=store://datatables.org/alltableswithkeys#h=select+*+from+google.igoogle.movies+where+movies%3D'68105'%3B
Check that out. Using that, your two halves of the link would be:
String firstHalf = "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20google.igoogle.movies%20where%20movies%3D'"
String secondHalf = "'%3B&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys"
And then to get your final link, you would just do
String yqlURL = firstHalf + "ZIP CODE OF YOUR LOCATION" + secondHalf;
And you would have all of the movies playing near you returned!
Make your life a lot easier and choose the api that is right for you. Choose one of these:
http://www.programmableweb.com/news/52-movies-apis-rovi-rotten-tomatoes-and-internet-video-archive/2013/01/22
Make your decision not only based on the content, but also ease of use and documentation. Documentation is a biggy.
Good luck!
well i would rather advice you to use an TheMovieDB.com API it is simple and provides every info of movies.
I have a webview that loads a google doc with a flyer (pdf, from the web) in it. Some of the flyers are live on my website but others aren't, but I have them coded in the app so when they become available the user will see them.
The google doc in the webview works great with flyers that are available but ones that aren't available yet I get a error-type message in the google doc. I would like to intercept the 404 response code error from the URL that has the flyer (pdf). How do I do this?
Current code:
/*opens in app using google docs*/
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setPluginState(PluginState.ON);
mWebView.loadUrl("https://docs.google.com/viewer?url="+round_num_url);
//round_num_url is the url for my flyer
Message in google doc in webview for flyers unavailable - "Sorry, we were unable to find the document at the original source. Verify that the document still exists. You can also try to download the original document by clicker here"
EDIT: doesn't work yet (crashes) after taking suggestions from #x-code
try {
URL url = new URL(round_num_url);
HttpURLConnection huc = (HttpURLConnection) url.openConnection();
huc.setRequestMethod("HEAD");
huc.connect();
if (huc.getResponseCode() != 404) {
// the pdf is available, continue with your code that loads the web view
// ...
} else {
// the pdf is not available, you may need to notify the user
// ...
}
huc.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}
Although Google docs will receive a 404 when it tries to access your broken URL, it is not going to return a 404 to the web view, because the Google docs URL is valid.
You should first test your own URL (the URL of your PDF) by trying to load it with for example the AndroidHttpClient and an HttpHead request. If this returns a 404 don't bother trying to load gdocs.
Here is some example code. I used HttpUrlConnection because that is the method recommended by Android docs for new code:
HttpUrlConnection huc = new HttpUrlConnection(round_num_url);
huc.setRequestMethod("HEAD");
huc.connect();
if (huc.getResponseCode() != 404) {
// the pdf is available, continue with your code that loads the web view
// ...
} else {
// the pdf is not available, you may need to notify the user
// ...
}
huc.disconnect();
I have not compiled this code, and you may have to wrap it in a try / catch block before it will compile.
Update: I had to modify the code to make it work, as suggested above:
try {
URL url = new URL(strUrl);
HttpURLConnection huc = (HttpURLConnection) url.openConnection();
try {
if (huc.getResponseCode() != 404) {
// url is available
} else {
// url is not available
}
} finally {
huc.disconnect();
}
} catch (MalformedURLException e) {
;
} catch (IOException e) {
;
}