Extremely new to coding(yesterday)
I have been given the task of creating a Android app for a friend to pull the data from his website and insert it into a quick app rather than him and his colleagues having to open a browser every time they would like to view it.
For reasons, I cannot share the website address.
The data on the website is in a bullet point list.
I have been reading some tutorials and watching some videos, I can pull the website up using WebView and I have now started reading up on JSoup. I have copied a few lines of code from some tutorials and I can now pull the website up in raw/html/just plain text but I could like to remove the unnecessary data and just have the text next to the bullet points.
Here is my code from MainActivity.java
As previously mentioned, I am extremely new to this with no background experience.
What I would like to know is if I can scrape the text between two tags on the page source for example between "div class="scheduler" SCRAPE THE TEXT HERE "ul id="myUL"
That would have all of the information I need in the above tags.
Thank you.
package spcentral.jsouptut;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private Button getBtn;
private TextView result;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
result = (TextView) findViewById(R.id.result);
getBtn = (Button) findViewById(R.id.getBtn);
getBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
getWebsite();
}
});
}
private void getWebsite() {
new Thread(new Runnable() {
#Override
public void run() {
final StringBuilder builder = new StringBuilder();
try {
Document doc = Jsoup.connect("linkremoved").get();
String text = doc.text();
Elements links = doc.select("a[href]");
builder.append(text).append("n");
for (Element link : links) {
builder.append("n").append("Link : ").append(link.attr("href"))
.append("n").append("Text : ").append(link.text());
}
} catch (IOException e) {
builder.append("Error : ").append(e.getMessage()).append("n");
}
runOnUiThread(new Runnable() {
#Override
public void run() {
result.setText(builder.toString());
}
});
}
}).start();
}
Related
My problem statement is to fetch the company logo from the company website.I have the input as the company website url. How can we fetch the company logo from it.I need to implement it in Android App.
For Example I have a Edittextbox with button.When user enter the text in the edittextbox i.e. the company's url eg www.google.com.So when user clicks on search I need to display the company logo i.e. the logo of the google.How can I achieve this in android
Use Glide :
//app build.gradle
compile 'com.github.bumptech.glide:glide:3.7.0'
Glide.with(this)
.load(yourEdittext.getText().toString) //Edit
.diskCacheStrategy(DiskCacheStrategy.ALL)
.signature(new StringSignature(UUID.randomUUID().toString())) //use this
.into(imgView);
Use picasso or glide library for Android.
You cannot do this with just a url to a domain. You need a specific url to a image for libraries such as picasso or glide to work. Because every site has different html code, you have to first find the logo in the html, which is quite impossible since not every site is the same. If you know the image url of your company, just use that url and use a library as suggested.
So this is what I have tried to fetch the logo.I have used JSoup library to read the html page source code.
In that I have called link element of html.After getting all the link element I checked for the icon.From there I got the href of the logo and after getting the source I am done with my logo finding.
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
String url="";
EditText editText;
ImageView imageView;
Button button;
TextView textView;
ProgressDialog mProgressDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText=(EditText) findViewById(R.id.editText);
imageView=(ImageView)findViewById(R.id.imageView);
button=(Button)findViewById(R.id.button);
textView=(TextView)findViewById(R.id.textView);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(editText.getText().toString()!=null && !editText.getText().toString().isEmpty())
{
String temp=editText.getText().toString();
if(temp!=null && !temp.isEmpty())
{
String divide[]=temp.split("\\.");
url="http://www."+divide[divide.length-2]+"."+divide[divide.length-1];
}
}
new Title().execute();
}
});
}
private class Title extends AsyncTask<Void, Void, Void> {
String title;
#Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = new ProgressDialog(MainActivity.this);
mProgressDialog.setTitle("Android Basic JSoup Tutorial");
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}
#Override
protected Void doInBackground(Void... params) {
try {
// Connect to the web site
if(url!=null && !url.isEmpty()) {
Document document = Jsoup.connect(url).get();
// Get the html document title
Elements description = document.select("link");
title = description.attr("href");
for (Element metaElem : description) {
String name = metaElem.attr("rel");
if (name.contains("icon")) {
title = metaElem.attr("href");
break;
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void result) {
// Set title into TextView
//TextView txttitle = (TextView) findViewById(R.id.titletxt);
textView.setText(title);
String imageURL=textView.getText().toString();
if(!textView.getText().toString().contains("http"))
{
String c=imageURL;
c=c.substring(0,1);
if(!c.equals("/"))
{
imageURL=url+"/"+imageURL;
}
else
{
imageURL=url+imageURL;
}
}
Picasso.with(MainActivity.this)
.load(imageURL)
.error(R.drawable.down_arrow)
.into(imageView);
//this is optional the image to display while the url image is downloading
//this is also optional if some error has occurred in downloading the image this image would be displayed
mProgressDialog.dismiss();
}
}
}
I am developing an application in which I want some data from a website to display in activity. For this I'm using Jsoup to parse data. But I am getting error at:
org.jsoup.nodes.Document document = Jsoup.connect(url).get();
Here it is my complete code below, I am not getting any idea about what is wrong I'm doing...
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.select.Elements;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.DocumentsContract.Document;
import android.app.Activity;
import android.view.Menu;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
public class Events extends Activity
{
//WebView web1;
TextView t1;
String url="https://sites.google.com/site/holyfamilychurchpestomsagar/notices-for-the-week";
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_events);
t1=(TextView)findViewById(R.id.textView1);
Title t2=new Title();
t2.execute();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.events, menu);
return true;
}
private class Title extends AsyncTask<Void, Void, Void> {
Elements title;
String desc;
#Override
protected void onPreExecute()
{
super.onPreExecute();
}
#Override
protected Void doInBackground(Void... params) {
try {
// Connect to the web site
org.jsoup.nodes.Document document = Jsoup.connect(url).get();
// Get the html document title
title = document.select("meta[name=title]");
desc = title.attr("content");
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void result)
{
// Set title into TextView
t1.setText(desc);
}
}
}
Is the device online? Ok, im sure you thought of that ;-) Are you
sure its exactly this line? Are you running in your catch clause, or
does the app crash?
You should always add a null pointer check when working with jsoup,
because when the selecting process is unsuccessful, the method will
return null!
Can you post your stacktrace?
Ok when the app crashes then Im pretty shure its not the connection itself but one of the lines below. Add a check for null pointers, or add a catch clause.
I have a problem with the concatenation of the strings that I will I enter.
This is the class:
package com.isma.multisitesearch.Siti;
import com.isma.multisitesearch.R;
import com.isma.multisitesearch.Webviews.GoogleWebView;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class Google extends Activity implements OnClickListener {
private String TAG_ACT = "Caricamento ricerca";
public EditText googletext;
private Button googlebutton;
private String googleurl = "https://www.google.it/search?q=";
public static String newgoogleurl;
public String space = " ";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.google);
googletext = (EditText) findViewById(R.id.googletext);
googlebutton = (Button) findViewById(R.id.googlebutton);
googlebutton.setOnClickListener(this);
newgoogleurl = googleurl + googletext.getText().toString();
newgoogleurl.replaceAll(space, "%20");
System.out.println(newgoogleurl);
}
#Override
public void onClick(View v) {
Log.v(TAG_ACT, "in corso");
Intent intent = new Intent(Google.this, GoogleWebView.class);
startActivity(intent);
}
}
And this is the WebView to which I want to get the concatenation:
package com.isma.multisitesearch.Webviews;
import com.isma.multisitesearch.R;
import com.isma.multisitesearch.Siti.Google;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class GoogleWebView extends Activity {
private WebView googlewebview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.googlewebview);
googlewebview = (WebView) findViewById(R.id.googlewebview);
googlewebview.getSettings().setJavaScriptEnabled(true);
googlewebview.getSettings().setLoadsImagesAutomatically(true);
googlewebview.setWebViewClient(new WebViewClient());
googlewebview.loadUrl(Google.newgoogleurl);
}
}
The app works fine but when I go to write the search text in the EditText, the google but I get the main page where you can enter the search instead of the url with already entered the search text.
I hope you know to help me, after this I was able to run the app.
Try this:
#Override
public void onClick(View v) {
newgoogleurl = googleurl + googletext.getText().toString();
newgoogleurl = newgoogleurl.replaceAll(space, "%20");
Log.v(TAG_ACT, "in corso");
Intent intent = new Intent(Google.this, GoogleWebView.class);
startActivity(intent);
}
Note that
newgoogleurl.replaceAll(space, "%20");
has no effect, you need to code:
newgoogleurl = newgoogleurl.replaceAll(space, "%20");
Have a look here:
http://javarevisited.blogspot.it/2011/12/java-string-replace-example-tutorial.html
Try below code:
String url;
url = googleurl+googletext.getText().toString();
if( url.contains(" ")){
newgoogleurl=url.replaceAll(" ","%20");
}else{
newgoogleurl=url;
}
This is containing validation part also.
Strings are immutable. That means that you cannot modify an instance of String, and need to create a new one each time you want to modify it.
In your case, replaceAll does not modify the String you call it on, but rather returns a new String with your modification applied.
Which is why you need to affect the returned value to the reference of your String.
newgoogleurl = newgoogleurl.replaceAll(space, "%20");
Moreover, you should read the documentation for replaceAll, because I don't think it does what you want. You probably want to use replace.
I'm trying to parse html with Jsoup lib. However, I did not get what I want.
I want to bring to the screen of a mobile device the entire text of the tag <pre>
Please tell me, how do I get the text from web? How do I need to fix?
Web site: http://devanswers.ru/text.php
package com.example.devanswers;
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView DevMainText;
ImageView DevMainImage;
MyTask DevMain;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DevMainText = (TextView) findViewById(R.id.DevMainText);
DevMainImage = (ImageView) findViewById(R.id.DevMainImage);
OnClickListener onClick = new OnClickListener() {
public void onClick(View v) {
DevMain = new MyTask();
DevMain.execute();
}
};
DevMainImage.setOnClickListener(onClick);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
class MyTask extends AsyncTask<Void, Void, Void> {
#Override
protected void onPreExecute() {
super.onPreExecute();
DevMainImage.setEnabled(false);
}
#Override
protected Void doInBackground(Void... params) {
Document doc;
try {
doc = Jsoup.connect("http://devanswers.ru/text.php").get();
Elements links = doc.getElementsByTag("pre");
for (Element link : links) {
DevMainText.setText((link.text()));
}
} catch (IOException e) {
// TODO Auto-generated catch block
DevMainText.setText("Error");
}
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
DevMainImage.setEnabled(true);
}
}
}
I have never use the Jsoup before but what i can see from your code that you over write what in the DevMainText each time you get data from the page
so you should setText in your textview like this:
String maintext = "";
for (Element link : links) {
maintext += link.text() +"\n";
}
DevMainText.setText(maintext);
Infact the response is not wrapped in the <pre> tag. Its the browser who wraps the raw response in the <pre> tag when you view the source.
Instead of doc.getElementsByTag("pre") try doc.getElementsByTag("body")
You could also try using the Android WebView component, if you want to display the entire page.
http://developer.android.com/reference/android/webkit/WebView.html
You can easily parse the page using the URI class Uri uri = Uri.parse("http://devanswers.ru/text.php");and then display this in a WebView.
I am trying to get HTML cleaner to parse the info from a website and then use Xpath to find the data I'm looking for. I have the htmlcleaner stuff in a separate AsyncTask class and the app seems to work on my phone. However, when I push the button nothing happens. Here is my main activity class and my AsyncTask Class.
package ru.habrahabr.stackparser;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.htmlcleaner.TagNode;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
public class stackParser extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.parse);
button.setOnClickListener(myListener);
}
private OnClickListener myListener = new OnClickListener() {
public void onClick(View v) {
new parseSite().execute("http://xjaphx.wordpress.com/");
}
};
private class parseSite extends AsyncTask<String, Void, String> {
protected String doInBackground(String... arg) {
String output = new String();
try {
htmlHelper hh = new htmlHelper();
} finally {
}
return output;
}
protected void onPostExecute(String output) {
TextView view = (TextView) findViewById(R.id.tv1);
view.setText((CharSequence) output);
}
}
}
And here's my referenced class. I'd really appreciate it if someone could look at this and tell me what's up. I tried to follow a working example and put my own Url and Xpath in but it's not working.
package ru.habrahabr.stackparser;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
import org.htmlcleaner.XPatherException;
public class htmlHelper {
TagNode rootNode;
String stats;
static final String XPATH_STATS = "//div[#id='blog-stats']/ul/li";
public String htmlHelper(URL htmlPage) throws IOException, XPatherException {
HtmlCleaner cleaner = new HtmlCleaner();
rootNode = cleaner.clean(htmlPage);
// query XPath
Object[] statsNode = rootNode.evaluateXPath(XPATH_STATS);
// process data if found any node
if (statsNode.length > 0) {
TagNode resultNode = (TagNode) statsNode[0];
stats = resultNode.getText().toString();
}
return stats;
}
}
Change AsyncTask doInBackground method as :
#Override
protected String doInBackground(String... arg) {
String output "";
try {
htmlHelper hh = new htmlHelper();
output=hh.htmlHelper(arg[0]); //<< call htmlHelper method here
} finally {
}
return output;
}
because you are currently only creating an instance of htmlHelper class not calling htmlHelper method from htmlHelper class to get data from web url
Look , I will not care of the data is downloaded or not , but I will assume that the data is already downloaded and besides in the variable output , you just need to place this code inside onPostExecute
runOnUiThread(new Runnable() {
public void run() {
// update data into TextView.
TextView view = (TextView) findViewById(R.id.tv1);
view.setText((CharSequence) output);
}
});
because , sometimes the data is already downloaded but needed to be assigned into Textview or whatever view by using UIthread. Hope this works for you