I'm trying to use JSoup in my android app to parse a certain website. However I don't seem to be getting anywhere. I've added the .jar of jsoup to the class path and tried to follow the examples on the JSoup website resource, the cookbook.
Here is my code:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView( R.layout.jsoup_layout );
Toast.makeText( getApplicationContext(), "Hello World", Toast.LENGTH_SHORT);
try {
Document doc = Jsoup.connect( "http://en.wikipedia.org/wiki/Main_Page" ).get();
Elements pTag = doc.select( "p" );
String pTagString = pTag.html();
Toast.makeText( getApplicationContext(), pTagString, Toast.LENGTH_SHORT);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Toast.makeText( getApplicationContext(), myString, Toast.LENGTH_SHORT );
}
So from this I'm trying to get the p tags of the wikipedia website. There are 12 or so in total but I only really want to display the value of one at this stage. But my app won't do anything. Even the first toast message meant to just display a message doesn't appear - this was only a check to see if it was working. So does anyone know what the problem is? Am i following the current syntax by choosing:
Elements pTag = doc.select( "p" );
You should not connect to a website on the main thread! Use AsyncTask for such an operation.
Also, to display a Toast, you need to call show():
Toast
.makeText( getApplicationContext(), "Hello World", Toast.LENGTH_SHORT)
.show();
Related
In the app that I'm making, my goal for it is to be a quick and easy notes/documents app. However, a problem that I have is that when the user saves the text they enter into an EditText, if there are extra lines that they put in, for basic formatting, those lines don't get saved into the text file. How could I remedy this? Here's the code for the saving process. Thanks!
String itemName = fileSaveListView.getItemAtPosition(position).toString();
File myExistingFile = new File(savedFilesDir, itemName);
if (myExistingFile.exists()){
myExistingFile.mkdirs();
}
try {
FileOutputStream fosForExistingFiles = new FileOutputStream(myExistingFile);
OutputStreamWriter myOutWriterExistingFiles = new OutputStreamWriter(fosForExistingFiles);
myOutWriterExistingFiles.append(textEntryEditText.getEditableText().toString());
myOutWriterExistingFiles.close();
fosForExistingFiles.close();
Toast.makeText(getBaseContext(), "Finished writing " + itemName + " to the folder", Toast.LENGTH_SHORT).show();
final AlertDialog thefileSaver = fileSaver.create();
thefileSaver.dismiss();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
OK, I finally figured it out, and got it working. For anyone else that is having this problem, here's what you need to do. When grabbing the text from your EditText, convert it to Html with the Html.toHtml function, like below.
myOutWriter.append(Html.toHtml(textEntryEditText.getText()));
Now, your text file will be saved WITH any linebreaks you have entered. The next thing to do, if you wish to display your saved file in the EditText, you just need to convert back from Html. Like so.
textEntryEditText.setText(Html.fromHtml(String.valueOf(<your file reader>)));
I have been trying to fix this problem for a VERY long time, so if anyone else was suffering like I was, I hope this helps you! :D
First, this is my first time "playing" with ffmpeg, so please bear with me.
Generally, i dont understand ffmpeg even a little bit. So i did lot, lot of researches (and also trial & error) and i finally found this project and its library
So i was successfully created the ffmpeg and sox binary file, and i put it in the raw folder at the library project (from the link i shared).
Now, i want to use the library for my project, but i still cant do it. I tried to use some methods in the FfmpegController like combineAudioAndVideo and more but its not working (yet).
I dont post the error here since i still do my trial&errors (and the error change regularly) but im getting tired now.
EDIT
This is what i did :
private FfmpegController ffController;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
File file = new File(Uri.parse("android.resource://com.my.package/" + R.raw.test).getPath());
try {
ffController = new FfmpegController(this, file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
MediaDesc desc = ffController.combineAudioAndVideo(R.raw.test, R.raw.musictest, "test.mp4", null);
}
The combineAudioAndVideo always error because wrong parameters. It needs MediaDesc but i dont know how to do it.
I will be very happy if you can share your working code if you have done the ffmpeg processing with this library.
I'm using Android's httpclient to connect to a domain as follows:
try {
URL url = new URL("example.com");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
read(conn.getInputStream());
} catch (Exception e) {
e.printStackTrace();
}
Now is it fine if I remove this line:
e.printStackTrace();
And replace it with this:
Toast toast = Toast.makeText(getApplicationContext(), "Could not connect to domain.", Toast.LENGTH_LONG);
toast.show();
Or do I have to do something with the 'e' variable? In which case it'll be:
catch (Exception e) {
e.printStackTrace();
Toast toast = Toast.makeText(getApplicationContext(), "Could not connect to domain.", Toast.LENGTH_LONG);
toast.show();
}
And if I do e.printStackTrace(), where does it print to?
I'd recommend catching only the thrown exception type (I think IOException in this case). Other exceptions may be for completely unrelated problems.
Yes, it's fine to replace the printStackTrace with the toast. Bonus tip: Just call
Toast.makeText(getApplicationContext(), "Could not connect to domain.", Toast.LENGTH_LONG).show();
and you can do it all in one line. :)
e.printStackTrace() will print to the standard logcat trace, which you can view with the adb logcat command (adb is part of the Android SDK).
I'm writing a simple app which allows a user to enter their income and it deducts tax, then saves the amount in a file for future reference. Whenever I try to enter an amount I get a warning saying the application has stopped unexpectedly. Here is my code:
public void onClick(View v) {
// TODO Auto-generated method stub
try {
if (preTax !=null){
Double incomeAmount = Double.parseDouble(preTax.getText().toString());
incomeAmount =- (20 *100)/incomeAmount;
Double incomeRounded = Round(incomeAmount);
Toast.makeText(null, "Your income minus tax = "+incomeRounded, Toast.LENGTH_LONG).show();
FileOutputStream fos = openFileOutput("income", Context.MODE_PRIVATE);
fos.write("1000".getBytes());
fos.close();
}
else {
Double incomeAmount = Double.parseDouble(postTax.getText().toString());
Double incomeRounded = Round(incomeAmount);
Toast.makeText(null, "Your income is: "+ incomeRounded, Toast.LENGTH_LONG).show();
FileOutputStream fos = openFileOutput("income", Context.MODE_PRIVATE);
fos.write("1000".getBytes());
fos.close();
}
} catch (Exception e){
Toast.makeText(null, "Please fill in the catagories" + e, Toast.LENGTH_LONG).show();
}
}
This issue was happening before the fileoutstream stuff was added, so I know that that isn't the issue, but it is not clear to me what is. Program crashes regardless of whether the EditText is empty or not. Surely the try/catch should catch any errors?
Toast.makeText(null, "Please fill in the catagories" + e, Toast.LENGTH_LONG).show();
should be
Toast.makeText(v.getContext(), "Please fill in the catagories" + e, Toast.LENGTH_LONG).show();
you can't pass null in for the context, it needs to be valid.
Passing in null for the context does not exactly help. Your app in blowing up, getting caught and then blowing up again.
I noticed that a toast isn't displayed when it's used inside a catch block.
Does anyone know how to show toasts when catching exceptions? An Example:
try {
// try to open a file
} catch (FileNotFoundException e) {
Toast.makeText(this, R.string.txt_file_not_found, Toast.LENGTH_LONG);
return; // cancel processing
}
Should be like this:
Toast toast = Toast.makeText(this, R.string.txt_file_not_found, Toast.LENGTH_LONG);
toast.show();
Yes, I put it right behind the existing line:
Toast.makeText(this, R.string.txt_file_not_found, Toast.LENGTH_LONG).show();