retrieving a specific text from a webpage android studio - android

I would like to build an app and in the app i want to pull a specific text from a website. So lets say that we want to retrieve and store the words "Connect with friends and the world around you on Facebook" from facebook.com. which is easy to get if i want to get the whole text from a specific website. Now lets say that we would like to get the word connect only or friends only from the website and store it into a string and print it.
what im doing is a currency app and getting the number http://www.hawlergov.org/en/currency.php and retrieving 128,800.
the number then prints out on the textView

I Answered my own question by splitting the string like this!
public class MainActivity extends AppCompatActivity {
TextView text;
TextView text2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.textView9);
text2 = (TextView) findViewById(R.id.textView8);
Button btn1 = (Button) findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new doit().execute();
}
});
}
public class doit extends AsyncTask<Void, Void, Void> {
String words;
String currentString;
String date;
#Override
protected Void doInBackground(Void... params) {
try {
Document doc = Jsoup.connect("http://www.hawlergov.org/en/currency.php").get();
words = doc.text();
currentString = words;
} catch (Exception e) {
e.printStackTrace();
}
String first = words;
String roar = first.substring(718, 730);
words=roar;
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
String currentDate = new SimpleDateFormat("MM-dd-yyyy", Locale.getDefault()).format(new Date());
text2.setText(currentDate);
text.setText(words);
}
}
here is the example

Related

Jsoup method not storing data into TextView after creating Document from HTML page

I tried this code:
public class MainActivity extends AppCompatActivity {
TextView text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
netAccess();
text = findViewById(R.id.textview1);
}
public void netAccess()
{
try {
Document doc = Jsoup.connect("https://google.com/").get();
String word = doc.title();
text.setText(word);
}
catch(Exception e)
{
e.printStackTrace();
}
}
But after I initialize "doc" nothing happens, the TextView doesn't change, and "word" does not change its value. I used Android Studio's debugging to see if the value changed and the TextView just didn't update for some reason, but nothing happens. I've tried it on multiple AVD's. Nothing I can find indicates why it won't work, I substituted title() for text() and nothing changed.
Document doc = Jsoup.connect("https://google.com/").get();
String word = doc.title();
text.setText(word);
I'm sure there is something obvious I am overlooking. Can anybody see it?
You have to do this operation in another thread. You can try AsynkTask this. Since it is deprecated now, I am just giving you an example.
public void netAccess()
{
new MyTask().execute();
}
class MyTask extends AsyncTask<Void,String,String>
{
#Override
protected String doInBackground(Void... arg0) {
String title = "";
try {
Document doc = Jsoup.connect("https://google.com/").get();
title = doc.title();
Log.v("MYJSOUP", title);
}
catch(Exception e)
{
e.printStackTrace();
}
return title;
}
}

Single button multiple action

I am using a single button to do two task.
Watson Conversation.
Watson Text to Speech.
My code is executing only if my TextView has some Text name (string), but the Text to Speech is playing the last conversation response even though the new conversation response is updated at TextView display on my phone UI.. Continuation of this here Race condition with UI thread issue.
Also I found out, if I keep my TextView empty i get error this:
Code here:
private class ConversationTask extends AsyncTask<String, Void, String> {
String textResponse = new String();
#Override
protected String doInBackground(String... params) {
System.out.println("in doInBackground");
MessageRequest newMessage = new MessageRequest.Builder().inputText(params[0]).context(context).build();
// async
GLS_service.message("xxxxxxx", newMessage).enqueue(new ServiceCallback<MessageResponse>() {
#Override
public void onResponse(MessageResponse response) {
context = response.getContext();
textResponse = response.getText().get(0);
reply.setText(textResponse);
System.out.println(textResponse);
}
#Override
public void onFailure(Exception e) {
}
});
return textResponse;
}
}
//
private class WatsonTask extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(final String... textToSpeak) {
/* runOnUiThread(new Runnable() {
#Override
public void run() {
textView.setText(" ");
}
});*/
TextToSpeech textToSpeech = initTextToSpeechService();
streamPlayer = new StreamPlayer();
streamPlayer.playStream(textToSpeech.synthesize(textToSpeak[0], Voice.EN_LISA).execute());
return "Text to Speech Done";
}
/*#Override protected void onPostExecute(String result) {
textView.setText("");
}*/
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Register the UI controls.
input = (EditText) findViewById(R.id.input);
send = (ImageButton) findViewById(R.id.send);
textView = (TextView) findViewById(R.id.textView);
reply = (TextView) findViewById(R.id.reply);
play = (ImageButton) findViewById(R.id.play);
new ConversationTask().execute("");
//Button function
send.setOnClickListener(action3);
}
//five actions on button click
public void action5() {
String textResponse = new String();
System.out.println("Text to Speech:" + reply.getText());
//textView.setText("");
WatsonTask task = new WatsonTask();
task.execute(String.valueOf(reply.getText()));
//new WatsonTask().execute(reply.getText().toString());
}
View.OnClickListener action3 = new View.OnClickListener() {
public void onClick(View v) {
//action here//
new ConversationTask().execute(input.getText().toString());
action5();
}
};
}
Please help.
Action 3
View.OnClickListener action3 = new View.OnClickListener() {
public void onClick(View v) {
//action here//
new ConversationTask().execute(input.getText().toString());
}
};
Action 5
public void action5(String replyString) {
WatsonTask task = new WatsonTask();
task.execute(replyString);
}
Conversation Task
private class ConversationTask extends AsyncTask<String, Void, String> {
String textResponse = new String();
#Override
protected String doInBackground(String... params) {
System.out.println("in doInBackground");
MessageRequest newMessage = new MessageRequest.Builder().inputText(params[0]).context(context).build();
// async
GLS_service.message("xxxxxxx", newMessage).enqueue(new ServiceCallback<MessageResponse>() {
#Override
public void onResponse(MessageResponse response) {
context = response.getContext();
textResponse = response.getText().get(0);
reply.setText(textResponse);
action5(textResponse);
}
#Override
public void onFailure(Exception e) {
}
});
return textResponse;
}
}
WatsonTask
private class WatsonTask extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(final String... textToSpeak) {
reply.setText(textToSpeak[0]);
TextToSpeech textToSpeech = initTextToSpeechService();
streamPlayer = new StreamPlayer();
streamPlayer.playStream(textToSpeech.synthesize(textToSpeak[0], Voice.EN_LISA).execute());
return textToSpeak[0];
}
}
And for the sake of completeness address to the comment by Marcin Jedynak
I think your program scenario will follow next sequences:
Enter text to conversation task.
Get conversation result from GLS_service.message().
Input the result from sequence 2, for make the voice.
So try to change your code like this.
// There is no need to return String. Just send result to TextToSpeech.
//private class ConversationTask extends AsyncTask<String, Void, String> {
private class ConversationTask extends AsyncTask<String, Void, Void> {
String textResponse = new String();
#Override
protected String doInBackground(String... params) {
System.out.println("in doInBackground");
MessageRequest newMessage = new MessageRequest.Builder().inputText(params[0]).context(context).build();
// async
GLS_service.message("xxxxxxx", newMessage).enqueue(new ServiceCallback<MessageResponse>() {
#Override
public void onResponse(MessageResponse response) {
context = response.getContext();
textResponse = response.getText().get(0);
reply.setText(textResponse);
System.out.println(textResponse);
action5(textResponse); // It is real result that you want.
}
#Override
public void onFailure(Exception e) {
}
});
//return textResponse; // Not necessary.
}
}
//
private class WatsonTask extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(final String... textToSpeak) {
/* runOnUiThread(new Runnable() {
#Override
public void run() {
textView.setText(" ");
}
});*/
TextToSpeech textToSpeech = initTextToSpeechService();
streamPlayer = new StreamPlayer();
streamPlayer.playStream(textToSpeech.synthesize(textToSpeak[0], Voice.EN_LISA).execute());
return "Text to Speech Done";
}
/*#Override protected void onPostExecute(String result) {
textView.setText("");
}*/
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Register the UI controls.
input = (EditText) findViewById(R.id.input);
send = (ImageButton) findViewById(R.id.send);
textView = (TextView) findViewById(R.id.textView);
reply = (TextView) findViewById(R.id.reply);
play = (ImageButton) findViewById(R.id.play);
new ConversationTask().execute("");
//Button function
send.setOnClickListener(action3);
}
//five actions on button click
// Need a parameter to get String.
//public void action5() {
public void action5(String text) {
// String textResponse = new String(); // Replace to parameter as "text".
//System.out.println("Text to Speech:" + reply.getText());
System.out.println("Text to Speech:" + text);
//textView.setText("");
WatsonTask task = new WatsonTask();
//task.execute(String.valueOf(reply.getText()));
task.execute(text); // Replace to parameter as "text".
//new WatsonTask().execute(reply.getText().toString());
}
View.OnClickListener action3 = new View.OnClickListener() {
public void onClick(View v) {
//action here//
new ConversationTask().execute(input.getText().toString());
// action5(); // This invoking is not necessary at this point.
// Try to invoke this method after you get conversation result.
}
};
If it is not working even you changed, I want to know how you implement initTextToSpeechService() method.
Hope this helps you.

Android jsoup "Unfortunately Stop"

I have problem with jsoup. Every time I run application I got Unfortunately Stop. I tried all the ways and still getting it. I don't know what the wrong and this is simple from the code and I tried thread and didn't worked.
public class MainActivity extends Activity {
String result;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new MyInnerClass().execute();
}
private class MyInnerClass extends AsyncTask<String, Void, String> {
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected String doInBackground(String... params) {
try{
Document document = Jsoup.connect("http://google.come").get();
}catch(Exception e){Log.d("doinbackground exception", e.toString());}
return "Done";
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
final TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText(result);
}
}
}
Make correction here
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
final TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText(result);
}
First the get the tv from your activity_main layout and than do setText to it
TextView tv= (TextView) findViewById(R.id.yourTextIDDefinedIn activity_main);
tv.setText(result);
i found the solution :
Press check box for android jsoup in :
(properties --> Java build path --> Order and export) .
Thanks guys.

How to get updated value of EditText in Android

I'm new to android and working on a basic screen to use a web-service with android application.
I am posting values using AsyncTask and fetching the result from the webservice. It works fine until displaying the returned value. While displaying the Toast Message on click, I get old value of TextView resultReturned
public class TestPost extends Activity{
private TextView result = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.my_screen);
result = (TextView)findViewById(R.id.resultReturned);
Button submit = (Button)findViewById(R.id.btnSubmit);
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String[] strPost = new String[]{"value1", "value2"};
SendAsyncRequest asyncSend = new SendAsyncRequest();
asyncSend.execute(strPost);
// ResultView retains old value and gets correct value on second click
String returned = result.getText().toString();
Toast.makeText(getApplicationContext(), returned, Toast.LENGTH_LONG).show();
}
});
}
public class SendAsyncRequest extends AsyncTask<String, Void, String>{
private String fetchedData = "";
#Override
protected String doInBackground(String... params ) {
// perform async task
return fetchedData;
}
#Override
protected void onPostExecute(String result) {
setReturedValue(result);
}
}
private void setReturedValue(String data){
result.setText(data);
}
So, how do I get the updated text value of the TextView?
AsyncTask takes time to get response from request, Show toast message in postExecute() method, like this, and remove from onclick.
#Override
protected void onPostExecute(String result) {
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
Try this
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String[] strPost = new String[]{"value1", "value2"};
SendAsyncRequest asyncSend = new SendAsyncRequest();
asyncSend.execute(strPost);
// ResultView retains old value and gets correct value on second click
String jsonResult;
try {
jsonResult=asyncSend.get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
Toast.makeText(getApplicationContext(), jsonResult, Toast.LENGTH_LONG).show();
}
});
And return your Json String in doInBackground().

Parse html in android app

When I'm trying to get the site title stackoverflow all goes well, but when I try to get the title beastinvest.su nothing happens. What is the reason?
public class MainActivity extends Activity {
/** Called when the activity is first created. */
Button butTest;
TextView textView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
butTest = (Button)findViewById(R.id.button);
textView = (TextView)findViewById(R.id.textView);
new MyParser().execute("http://beastinvest.su/");
}
public class MyParser extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... links) {
Document doc = null;
String title = null;
try {
doc = Jsoup.connect(links[0]).get();
title = doc.title();
textView.setText(title);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
}
Sorry for my bad English
the problem is that the site AntiDDOS - query returns "To visit this site requires cookies and javacript your browser."
Provided site (beastinvest.su) has title in Russian and contains <meta charset="windows-1251". I suppose that Jsoup uses UTF-8 encoding by default during parsing routine.
Hi use below code,
public class MainActivity extends Activity {
/** Called when the activity is first created. */
Button butTest;
TextView textView;
String title;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
butTest = (Button)findViewById(R.id.button);
textView = (TextView)findViewById(R.id.textView);
new MyParser().execute("http://beastinvest.su/");
}
public class MyParser extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... links) {
Document doc = null;
try {
doc = Jsoup.connect(links[0]).get();
title = doc.title();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
}
public void onPostExecute(String result) {
textView.setText(title);
}

Categories

Resources