Android Service that searches in a website - android

This is my very first thread so please bear with me. :)
I want to create an Android Service that searches a specific string on a website. To do this I have tried to download that site and search within the html code but the application always crashes when trying to download it.
Does anybody have any idea how to do this?
Thanks in advance

I had a similer problem when i started making an android app that scans imdb.com for movie information. After a lot of searching the internet and testing things out i came up with this:
import java.net.*;
import java.io.*;
URL url = new URL("websiteToLookAt");
InputStreamReader isr = new InputStreamReader(url.openStream());
BufferedReader bufferedReader = new BufferedReader(isr);
String lineThatIsBeingRead = null;
String theString;
while((lineThatIsBeingRead = bufferedReader.readLine()) != null){
if(lineThatIsBeingRead.contains("StringYouAreLookingFor")){
theString = lineThatIsBeingRead;
break;
}
}
The first line sets up the URL of the website you are scanning
The second line opens a the internet to allow you to access the html source directly
The third line makes a Buffered reader that is able to read the source the InputStreamReader gives it
The fifth line is the string that the current line of HTML source is being held in while the buffered reader is checking if it contains the right string. (string1.contains(string2) looks at wether or not string2 is in string1. example: String myName = "john"; if you were to test if myName.contains("oh"); it would return true)
The Sixth line is the string that you will put the string you are looking for from the HTML source(like if you were looking for the name of a movie, this would be the string you would assign the name to)
The while loop reads the next line of the html source code every time the loop starts over and sets the line it just read to the String variable lineThatIsBeingRead. it will keep doing this as long as there is a new line to read. When the buffered reader comes to the end of the HTML source the conditions for the while loop return false and it breaks the loop.
The if statment checks to see if lineThatIsBeingRead has the string StringYouAreLookingFor in it. if it does, it sets theString(the string you are looking for) to lineThatIsBeingRead(the string that is in the buffered reader) then it breaks the while loop. otherwise, it resets the while loop and it starts all over again.
I have the variable theString in there because i was looking for several strings, but if you only need to find one string you can delete lines 6 & 11 and just have lineThatIsBeingRead as the string you assign the string you are looking for to.
Another thing to keep in mind is java doesn't allow you to connect to the internet through the UI thread(the way java wants you to do it is with an intent so when you publish the app remember to take this out and make it run on an intent). but if you add
if(BuildVERSION.SDK_INT >= 9){
StrickMode.ThreadPolicy Policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(Policy);
}
to the onCreate() method and it will bypass that rule.
Hope this was helpful, and good luck with your project!

Related

how to post a form with some cookie

i have posted a question for htmlunit in this link: how to use htmlunit with my android project
mainly i have a link, which i have get after login (i have login through web view) this link give me a simple page. in that page there is a textarea and a submit button. and there are some javascript too (i think these javascript run, when i press the submit button). i can do it through webview, but for some reason i don't want to use webview. whene i press submit button, it deliver the value of textarea and some value of hidden field with existing cookies(which are get when i logged in through webview) Post method. i need to do this without webview. now is there any other option beside htmlunit ?? i heard about HttpClient, HttpUrlConnection. but i don't know how to use them to solve my problem, because they are totaly new to me. i think if i use these class i have to run them in a seperate thread from UI tread. one more thing, after submitting it will redirect me to another page. i don't need to do anything with this redirected page.
thank you
this is the same answer which i have given here
i have solve the problem. first of all i was getting the right cookie all time. so what was the problem then. either i was wrong to integrate the cookie with Jsoup or Jsoup was doing something wrong. so, first i have get the page with HttpUrlConnection and then parse it with Jsoup. like this:
URL form = new URL(uri.toString());
HttpUrlConnection connection1 = (HttpURLConnection)form.openConnection();
connection1.setRequestProperty("Cookie", my_cookie);
connection1.setReadTimeout(10000);
StringBuilder whole = new StringBuilder();
BufferedReader in = new BufferedReader(
new InputStreamReader(new BufferedInputStream(connection1.getInputStream())));
String inputLine;
while ((inputLine = in.readLine()) != null)
whole.append(inputLine);
in.close();
Document doc = Jsoup.parse(whole.toString());
any advice about this code would be appreciated.

Load string value from text file, make an int and pass to androidplot

EDIT: SOLVED AS IM A TOTAL FOOL and didnt notice the file name was wrong between my 2 activities. Never mind, thanks to all those that helped. Still learning, just hope i can stop re-learning very old lessons!
im new to android and java and am trying to do what seems simple, but am struggling and am not sure why.
I want to load a string from a text file and extract the numbers from the string as an int for use elsewhere in my app. I want to use the int then in an array for use with androidplot to draw a graph, i have the graph working with a manual array (ie just type some random numbers in myself) so have omitted most of that code from here for simplicity.
This is my code for the load fo the txt file, split it at " " and then parse the int, but it isnt working.
//actually load the progress file
public void loadDatacomm() {
//load the asset file
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader("/sdcard/.progress/1gppi.txt"));
String line;
while ((line = br.readLine()) != null) {
String line2[] = line.split(" "); //split the string to get the progress
commprogress1 = Integer.parseInt(line2[0]);
}
}
catch (IOException ex) {
return;
}
The only other bit of interest i guess is the array in android plot which i am building like this;
Number[] commprogress = {commprogress1, commprogress2, commprogress3, commprogress4, commprogress5, commprogress6, commprogress7, commprogress8, commprogress9, commprogress1};
When i load the activity then the graph doesnt show any value, if i manually declare values of the commprogress1, 2, 3, then the graph draws fine.
Can someone help me please?
EDIT the file i am trying to load contains the following:
30 %complete
It is written by another activity in the application.
I have no idea why the code isnt working, I am using similar code elsewhere in the application to extract the int (in this case 30) from the split string, but its not working here.
In case you are getting an exception when trying to access the file, it could be due to the concurrency as the other application is writing into it (though since you are just reading the file, Id expect it not to be a problem).
In any case, if you want just to share the percentage in between activities, I think that the use of shared preferences may be more suitable

How to import text from a webpage into a textview?

How do I get text from a basic HTML page and show it in a TextView.
I want to do it this way because it will look better than having a webview showing the text.
There is only one line on the html page. I can change it to a txt file if needed.
Could you also provide a quick example?
You would need to download the HTML first using something like HttpClient to retrieve the data from the Internet (assuming you need to get it from the Internet and not a local file). Once you've done that, you can either display the HTML in a WebView, like you said, or, if the HTML is not complex and contains nothing other than some basic tags (<a>, <img>, <strong>, <em>, <br>, <p>, etc), you can pass it straight to the TextView since it supports some basic HTML display.
To do this, you simply call Html.fromHtml, and pass it your downloaded HTML string. For example:
TextView tv = (TextView) findViewById(R.id.MyTextview);
tv.setText(Html.fromHtml(myHtmlString));
The fromHtml method will parse the HTML and apply some basic formatting, returning a Spannable object which can then be passed straight to TextView's setText method. It even supports links and image tags (for images, though, you'll need to implement an ImageGetter to actually provide the respective Drawables). But I don't believe it supports CSS or inline styles.
How to download the HTML:
myHtmlString in the snippet above needs to contain the actual HTML markup, which of course you must obtain from somewhere. You can do this using HttpClient.
private String getHtml(String url)
{
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
try
{
HttpResponse response = client.execute(request);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line;
StringBuilder builder = new StringBuilder();
while((line = reader.readLine()) != null) {
builder.append(line + '\n');
}
return builder.toString();
}
catch(Exception e)
{
//Handle exception (no data connectivity, 404, etc)
return "Error: " + e.toString();
}
}
It's not enough to just use that code, however, since it should really be done on a separate thread (in fact, Android might flat out refuse to make a network connection on the UI thread. Take a look at AsyncTasks for more information on that. You can find some documentation here (scroll down a bit to "Using Asynctask").

Launch default browser with intent and post parameters [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I open android browser with specified POST parameters?
I would like to do something
like this:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.somepage.com?par1=val1&par2=val2"));
But I dont want to send the parameters with get but with post.
How can I do this as described above?
Many thanks in advance,
navajo
It can be done, but in a tricky way.
You can create a little html file with an auto submit form, read it into a string, replace params and embed it in the intent as a data uri instead of a url.
There are a couple little negative things, it only works calling default browser directly, and trick will be stored in browser history, it will appear if you navigate back.
Here is an example:
HTML file (/res/raw):
<html>
<body onLoad="document.getElementById('form').submit()">
<form id="form" target="_self" method="POST" action="${url}">
<input type="hidden" name="param1" value="${value}" />
...
</form>
</body>
</html>
Source code:
private void browserPOST() {
Intent i = new Intent();
// MUST instantiate android browser, otherwise it won't work (it won't find an activity to satisfy intent)
i.setComponent(new ComponentName("com.android.browser", "com.android.browser.BrowserActivity"));
i.setAction(Intent.ACTION_VIEW);
String html = readTrimRawTextFile(this, R.raw.htmlfile);
// Replace params (if any replacement needed)
// May work without url encoding, but I think is advisable
// URLEncoder.encode replace space with "+", must replace again with %20
String dataUri = "data:text/html," + URLEncoder.encode(html).replaceAll("\\+","%20");
i.setData(Uri.parse(dataUri));
startActivity(i);
}
private static String readTrimRawTextFile(Context ctx, int resId) {
InputStream inputStream = ctx.getResources().openRawResource(resId);
InputStreamReader inputreader = new InputStreamReader(inputStream);
BufferedReader buffreader = new BufferedReader(inputreader);
String line;
StringBuilder text = new StringBuilder();
try {
while ((line = buffreader.readLine()) != null) {
text.append(line.trim());
}
}
catch (IOException e) {
return null;
}
return text.toString();
}
Navajo,
What you are trying to do cannot be done with the above URL with the constraints that you have made above. The primary reason for this is that the URL above IS a GET URL. A POST URL does not have the above parameters in it. They are passed in the actual REQUEST and not the URL.
To accomplish what you wish to do, you would have to intercept the Intent, reformat the URL and then start the browser with a new Intent. The source of the URL is the key. If the source is from you or something you can track, that is easy, just create a custom Intent. If the source is outside of your control, then you can run into problems (see below)...
1) GETs and POSTs are not interchangable. If you are messing with data that is not yours or is not going to a site that you control, then you may break the functionality of that site, because not everyone programs for both GETs and POSTs for security reasons.
2) If you are responding to the same Intent that the browser does, then it is possible that the User may not understand what your app does if it always opens the default.
Another possibility, (if you are in control of the website), is to respond to the Intent by creating a cookie that your site can read with the actual data requirements. This would require PHP/ASP on the server or JS activated HttpRequest().
If I had more information I could advise you better.
FuzzicalLogic

Reading Text File From Server on Android

I have a text file on my server. I want to open the text file from my Android App and then display the text in a TextView. I cannot find any examples of how to do a basic connection to a server and feed the data into a String.
Any help you can provide would be appreciated.
Try the following:
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) {
}
(taken from Exampledepot: Getting text from URL)
Should work well on Android.
While URL.openStream will work, you would be better off using the Apache HttpClient library that comes with Android for HTTP. Among other reasons, you can use content encoding (gzip) with it, and that will make text file transfers much smaller (better battery life, less net usage) and faster.
There are various ways to use HttpClient, and several helpers exist to wrap things and make it easier. See this post for more details on that: Android project using httpclient --> http.client (apache), post/get method (and note the HttpHelper I included there does use gzip, though not all do).
Also, regardless of what method you use to retrieve the data over HTTP, you'll want to use AysncTask (or Handler) to make sure not to block the UI thread while making the network call.
And note that you should pretty much NEVER just use URL.openStream (without setting some configuration, like timeouts), though many examples show that, because it will block indefinitely if you the server is unavailable (by default, it has no timeout): URL.openStream() Might Leave You Hanging.
Don't forget to add internet permissions to the manifest when taking net resources: (add in manifest).

Categories

Resources