playing audio files in an app - Java for Android App Development

Hi there
I am desperate for advice and support on how to develop an android app that plays music audio files.
The app itself would have a home screen, about screen and a tracks screen.
The user when they navigate to tracks screen would have a list view of music audio files they can listen to on their android device.
The music audio file itself can either be stored in a SQL server database or ftp site , I don't know what would be best in terms of delivering optimal performance for end user?
The app would be like a service that would just play in the back ground allowing users to use their phone whilst listening to the music track(s).
My problems are:
- how can I actually develop this?
- there are no tutorials on the net, all music playing apps on YouTube, stack overflow and other websites do not show how to read music from SQL or ftp server.
I would be eternally grateful to anyone who could kindly advise me:
- best way to store each music audio file? Ftp or SQL
- in any case SQL would have to be used I believe to store the weblinks if I were to use FTP
- each music file of which there are around 150-180 is around 100mb in size and is of mp3 quality
Please , please , please can you help me develop this? Or guide me so I may be able to do it.
Please advise me on best way to set this up and get me started...
Thank you in advance

xirokx said:
Hi there
I am desperate for advice and support on how to develop an android app that plays music audio files.
The app itself would have a home screen, about screen and a tracks screen.
The user when they navigate to tracks screen would have a list view of music audio files they can listen to on their android device.
The music audio file itself can either be stored in a SQL server database or ftp site , I don't know what would be best in terms of delivering optimal performance for end user?
The app would be like a service that would just play in the back ground allowing users to use their phone whilst listening to the music track(s).
My problems are:
- how can I actually develop this?
- there are no tutorials on the net, all music playing apps on YouTube, stack overflow and other websites do not show how to read music from SQL or ftp server.
I would be eternally grateful to anyone who could kindly advise me:
- best way to store each music audio file? Ftp or SQL
- in any case SQL would have to be used I believe to store the weblinks if I were to use FTP
- each music file of which there are around 150-180 is around 100mb in size and is of mp3 quality
Please , please , please can you help me develop this? Or guide me so I may be able to do it.
Please advise me on best way to set this up and get me started...
Thank you in advance
Click to expand...
Click to collapse
sounds like you need to do lots more research and maybe a few tutorials for a better understanding. I would suspect 99% of developers would host file though http/s (not ftp) and yes all song info and metadata in SQL and delivered though php.
Binary Storage in SQL is not out of the question using BLOBS but not well suited in your case.
hope that helps, sounds simple enough

sounds like you need to do lots more research and maybe a few tutorials for a better understanding.
Click to expand...
Click to collapse
thanks for your reply
i have tried looking for tutorials for hours on end and its a very niche area, i could not find any.....do you know of any? have you tried looking for tutorials?
I would suspect 99% of developers would host file though http/s (not ftp) and yes all song info and metadata in SQL and delivered though php.
Click to expand...
Click to collapse
how do you host mp3 files using HTTPs ?
sounds simple enough
Click to expand...
Click to collapse
if its that simple, perhaps you would not mind guiding me to acheive it?
the page in the app which contains the mp3 files would have a listview or gridview with the names of the mp3s so when user clicks on them the mp3 plays in the background...
perhaps you can create an example if possible? i would be so grateful.
thank you

xirokx said:
i have tried looking for tutorials for hours on end and its a very niche area, i could not find any.....do you know of any? have you tried looking for tutorials?
Click to expand...
Click to collapse
You will never find a tutorial to do an exact app for what you want But you will find 1000's of how to store and use data from a database, 1000's for how to use mySQL and php in android, 1000's for listviews and gridviews powered by adapters and data and again 1000's or hundreds on how to stream audio from web servers using android
xirokx said:
how do you host mp3 files using HTTPs ?
Click to expand...
Click to collapse
upload them ? not sure what your question is here, you place the file on the server and use the URL ?
xirokx said:
if its that simple, perhaps you would not mind guiding me to achieve it?
the page in the app which contains the mp3 files would have a listview or gridview with the names of the mp3s so when user clicks on them the mp3 plays in the background...
perhaps you can create an example if possible? i would be so grateful.
thank you
Click to expand...
Click to collapse
It contains such a generic set of tasks you really dont need me to write it for you.... I'm always happy to answer direct specific questions, but you will seldom get an answer for "how do I do x app"... but like I said, what you want is very common, so there is tonnes of resource online

thanks for your reply
You will never find a tutorial to do an exact app for what you want But you will find 1000's of how to store and use data from a database, 1000's for how to use mySQL and php in android, 1000's for listviews and gridviews powered by adapters and data and again 1000's or hundreds on how to stream audio from web servers using android
Click to expand...
Click to collapse
If only the world was perfect and I could find an "exact" tutorial.....If I thought it was that easy I would never have started my thread.
really? 1000s or 100s.....yet you cannot point me to one?
It contains such a generic set of tasks you really dont need me to write it for you.... I'm always happy to answer direct specific questions, but you will seldom get an answer for "how do I do x app"... but like I said, what you want is very common, so there is tonnes of resource online
Click to expand...
Click to collapse
if it is that generic and simple, I do not understand why you cannot guide me more, at least to the 1000's or 100's of tutorials you claim that exist and especially as I have mentioned I cannot find them.
Try and see it from my point of view, I am new to this, trying, well actually REALLY REALLY want to learn, have spent hours researching for adhoc tutorials that will enable me to grasp the "generic" concept and have not found much / anything to help me.
Then you come along and make such a bold claim basically saying its "simple and easy, generic and quite straightforward" yet you cannot backup your claim by providing any of these "easy to find, 1000's or 100s tutorials" and are not willing to help any more then this.
Personally if I thought something was that easy, how much of my valuable time is it really going to take me, to provide someone who wants to learn with a few lines of code to get them started, especially after making such BOLD claims....
If I was not willing to, I would not even bother replying, it would have been far more beneficial if you responded with some example code rather then claiming how simple it was to do, I see no evidence that is it so easy only your claims...
Thanks for your time

Seriously though, this is me using google
http://developer.android.com/guide/topics/data/data-storage.html
http://developer.android.com/training/basics/data-storage/databases.html
http://www.vogella.com/tutorials/AndroidSQLite/article.html
http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/
http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/
http://www.mybringback.com/tutorial...-using-remote-databases-php-and-mysql-part-1/
http://forum.xda-developers.com/showthread.php?t=2325799
http://developer.android.com/guide/topics/media/mediaplayer.html
http://stackoverflow.com/questions/18174068/how-to-play-the-online-streaming-radio-in-android
http://stackoverflow.com/questions/1965784/streaming-audio-from-a-url-in-android-using-mediaplayer
if you use google you will find 1000's more results, they were just the top ones really, not sure why you cant find any...seriously pages and pages of relevant results, what were you actually searching for ? as in search terms ... maybe thats the prob ?
Also yes it is generic and yes almost every app uses those components and has had to complete those tasks. Trust me on this, remote database + online resources are in 90% of apps But it's the tasks that have 1000s of tutorials and reference, not the app.... like i say "android database tutorial", "android mysql online database","android media stream url"
hope that helps

thanks alot....I will check them out and get back to you
so grateful for your help

maybe you can solve this, perhaps its "challenging" enough for ya
this is my android java code that a) connects to my db b) uses the Name field from my db to populate a listview in my app c) when any single listview item is selected, it uses the name in the listview matches it to the dbName and plays the dbFile which contain a URL to where the mp3 is stored:
Code:
private void connect() {
List<String> r = new ArrayList<String>();
ArrayAdapter<String>adapter=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,r);
final ListView list=(ListView)findViewById(R.id.listView1);
try {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("myServer/android/files.php");
HttpResponse response = client.execute(request);
HttpEntity entity=response.getEntity();
data=EntityUtils.toString(entity);
Log.e("STRING", data);
try {
JSONObject jsonResponse = new JSONObject(data);
JSONArray jsonMainNode = jsonResponse.optJSONArray("mp3s");
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
ID = jsonChildNode.getInt("ID");
name = jsonChildNode.getString("Name");
FileName = Uri.parse(jsonChildNode.getString("FileName"));
Log.e("STRING", name);
//r.add(ID);
r.add(name);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
MediaPlayer mp = new MediaPlayer();
try{
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setDataSource(getApplicationContext(), FileName);
mp.prepare();
mp.start();
}
catch(IOException e)
{
System.out.println(e);
}
}
});
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (ClientProtocolException e) {
Log.d("HTTPCLIENT", e.getLocalizedMessage());
} catch (IOException e) {
Log.d("HTTPCLIENT", e.getLocalizedMessage());
}
}
Problem is:
- It doesnt open mediaplayer or streaming player on the emulator
- If track 1 is playing and I click on track 3 it plays both tracks simulateously
- as there is no media/stream player view you cannot pause/stop/play the track that is being played
pls help me fix these issues, if you can
thanks
p.s. I did use your links to get me this far, now I am genuinely stuck and would really appreciate a hand
thanks appreciate it

xirokx said:
Problem is:
- It doesnt open mediaplayer or streaming player on the emulator
Click to expand...
Click to collapse
Dont work with the emulator at all, I only use real devices, but if you mean the media player app, then thats due to you not using it. You're using the media player class.
xirokx said:
- If track 1 is playing and I click on track 3 it plays both tracks simulateously
Click to expand...
Click to collapse
Thats cause you create another instance that every click (or new instance rather). I would assume that you just should have 1 and stop/reuse it.
xirokx said:
- as there is no media/stream player view you cannot pause/stop/play the track that is being played
Click to expand...
Click to collapse
Well you would create buttons to do that yourself.

I am now debugging my app on my device
I have changed the code a little so only 1 file plays at any one time and it works....
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
//playAudio(FileName);
Uri uri = FileName;
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
Click to expand...
Click to collapse
However when I click on the list item, a menu opens up asking which browser I want to play the file in....when instead it should open a list of mediaplayers for me to play the file in.
How can I change it?
Thanks for your help.

xirokx said:
I am now debugging my app on my device
I have changed the code a little so only 1 file plays at any one time and it works....
However when I click on the list item, a menu opens up asking which browser I want to play the file in....when instead it should open a list of mediaplayers for me to play the file in.
How can I change it?
Thanks for your help.
Click to expand...
Click to collapse
well thats the way it will work if down to the user not choosing a default and also the way you set it up.... I do reccomend you read the android docs like http://developer.android.com/guide/components/intents-filters.html
it will help when asking question and most of the time you wont have to ask cause the answers are there.

Related

Interest in a task automater / macro recorder and playback utility?

I wrote a program for Windows 2000/XP called Do It Again ( http://www.spacetornado.com/DoItAgain/ ) that does simple task automation (also called macro recording and playback).
You click "Create a New Task" and it then records all of your keystrokes and mouse clicks in any program until you hit a certain hot key (Scroll Lock by default). You can then run the task back in a number of different ways and it will recreate your keystrokes and mouse clicks... either with the same pauses in between, or it can speed them up. And there are some other options available as well (repeating tasks, manually extending pauses between actions, etc).
I thought something like this might be useful for Windows Mobile devices. I wrote Do It Again in C# using .NET Framework 2.0, so I'm guessing it should be fairly easy to port to .NET Compact Framework 2.0.
But I only want to start porting this to Windows Mobile if there is any interest in it, and if people think they might get some use out of it. I know for me personally there are several things I do on my WinMo device that get repetitive and monotonous... starting and minimizing GPS Test to activate the GPS adapter, dismissing old Notifications, marking all email as Read, etc.
This sounds like a perfect application which can be used for pretty much anything.
Personally, i'm very interested in this, and would be glad to have it ported.
There are a lot of people who're looking to do things the easy way around here, for instance, settings up accounts, changing settings.. and so forth, without having to go 8 clicks...
nir36 said:
This sounds like a perfect application which can be used for pretty much anything.
Personally, i'm very interested in this, and would be glad to have it ported.
There are a lot of people who're looking to do things the easy way around here, for instance, settings up accounts, changing settings.. and so forth, without having to go 8 clicks...
Click to expand...
Click to collapse
I would find this very useful for using both Google Maps and Tom Tom Nav.. Say you want to go somewhere new.. Use google maps to find the address and then activate the script, prime the GPS, load Tom Tom, then copy paste the address info into Tom Tom. One cycle activated by a hot key would be nice.
norkoastal said:
I would find this very useful for using both Google Maps and Tom Tom Nav.. Say you want to go somewhere new.. Use google maps to find the address and then activate the script, prime the GPS, load Tom Tom, then copy paste the address info into Tom Tom. One cycle activated by a hot key would be nice.
Click to expand...
Click to collapse
Yeah that would be a perfect example of how you could use it. And that would be a lot safer to have a task automation app perform those steps for you instead of having to do it yourself while driving.
I'll start working on this in a week or so (as soon as I can get Scrobble finished up and to a point where people stop requesting more features! ) and see if can be ported over.
It should be easy; there are just a couple Win32 library calls it makes that I'm not sure are available in WinMo 6.1:
Code:
// imports mouse_event function from user32.dll
[SRI.DllImport("user32.dll", CharSet = SRI.CharSet.Auto, CallingConvention = SRI.CallingConvention.StdCall)]
public static extern void mouse_event(int dwFlags, int dx, int dy, uint cButtons, uint dwExtraInfo);
// imports keybd_event function from user32.dll
[SRI.DllImport("user32.dll", CharSet = SRI.CharSet.Auto, CallingConvention = SRI.CallingConvention.StdCall)]
public static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);
These two functions mouse_event() and keybd_event() poll for all mouse and keyboard events (clicks, movement, keypresses, etc.) globally... in any window, program, and so on.
Does anyone know off the top of their head if user32.dll is also available in Windows Mobile 6.1 Professional?
I am *VERY* interested in this application. How is the porting coming? I can't wait!!!
Man. That would be a revolution for us ppccusumizenerds! Holy ****! ..that would actually be Holy ****!
There is a version of AutoHotKey for Pocket PCs / WinCE, still in development, but a lot of it working, see here http://www.autohotkey.com/forum/topic27146.html&highlight=wince.
I'm very interested. In fact, I was looking for such an application but gave up finding.
Looking forward.
Thanks.
Hi,
if it's getting as good as Scrobble, then I vote for it . This would be a very handy feature, moreover very clever.
I would test it with pleasure.
Greets.
It will be a very useful app!
Looking forward to it, in the meanwhile I'll play with XP version!!

Launch Program

Hi,
I'm trying to code a simple app and need to be able to launch a program from within the app. I'm initially coding in VB, and then will probably move it over to C#
It seems the shell() command does not work on the CE platform. I've trawled through thousands of forums, but can't find anything to help.
The best I've been able to find will launch a program, but only 'windows' programs - I can't get it to launch anything that has been personally installed on the device.
I figure a few of you here would know how to do it...
Would love a bit of help... It's driving me crazy....
k1sr said:
Hi,
I'm trying to code a simple app and need to be able to launch a program from within the app. I'm initially coding in VB, and then will probably move it over to C#
It seems the shell() command does not work on the CE platform. I've trawled through thousands of forums, but can't find anything to help.
The best I've been able to find will launch a program, but only 'windows' programs - I can't get it to launch anything that has been personally installed on the device.
I figure a few of you here would know how to do it...
Would love a bit of help... It's driving me crazy....
Click to expand...
Click to collapse
Look for ShellExecuteEx or CreateProcess on msdn.
Here is the code in C#
Code:
using System.IO;
using System.Reflection;
...
FileSystemInfo f = ...
try
{
Process p = new Process();
p.StartInfo.FileName = @"" + f.FullName;
p.Start();
}
catch (System.Exception) { }
place the codes in
Code:
Click to expand...
Click to collapse
tag
ather90 said:
place the codes in tag
Click to expand...
Click to collapse
Sorry :S and thankx
no worries, while going through my old projects, i found this
Code:
System.Diagnostics.Process.Start("\\windows\calender.exe", ""); // Start the calender
i use it like this :
Code:
private void button1_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("\\Windows\calender.exe", ""); // Start the calender.
}
Thanks All,
I'll give these a go tonight...
ather90 - will this work for non windows programs - as that seemed to by my issue - ie. can I start \internal storage\programs\myprogram.exe
Thanks...!
k1sr said:
Thanks All,
I'll give these a go tonight...
ather90 - will this work for non windows programs - as that seemed to by my issue - ie. can I start \internal storage\programs\myprogram.exe
Thanks...!
Click to expand...
Click to collapse
this will work for any application that runs on your device when you click on it's icon,
just use the full path and go.
Thank you all...!!!
I was about to top myself...
System.Diagnostics.Process.Start("\\windows\calender.exe", "") was my saviour...
I can't believe how hard this was to find anywhere...

[Q] SOAP Call

Hello all... first time post in this area. Hope Im in the correct spot. I am trying to make a small app that will connect to a webservice. Typically we pass up a JSON object in C# to get a JSON return from our webservice.
Being new to Java, I am struggling with this. Any help would be much appreciated.
assume the following:
http://mysite/webservice.asmx
sO = "{"s_O":{"arPR":[{"sPSUniqueID":"K001"},{"sPSUniqueID":"K002"}]}}";
sUser = "mName";
sPassKey = "aqyu08ioa";
sMethodName = "mGetSummary";
our normal call in c# would look something like this...
JSONString = return androidSOAPCALL( methodName, sUser, sPassKey, sO);
Thanks for help on this guys...
Vic
You could do it that way: http://www.vogella.com/articles/AndroidJSON/article.html
However, you could also use a library made by Google which is calles Gson: http://code.google.com/p/google-gson/
It is much easier with the library.
A tutorial on this (points 3 and 4): http://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/
nikwen said:
You could do it that way: http://www.vogella.com/articles/AndroidJSON/article.html
However, you could also use a library made by Google which is calles Gson: http://code.google.com/p/google-gson/
It is much easier with the library.
A tutorial on this (points 3 and 4): http://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/
Click to expand...
Click to collapse
Those are excellent articles on JSON. Thanks for the reference. I will most likely be using that to handle the returned JSON object.
However, my question is more concerning making my SOAP call to the asmx webservice. I cannot seem to relate any samples to my particular case as mentioned above.

newbie project.

Hi I want to make an app that loads a web page and auto refreshes.
Also I would like it to open any links and search for any text the user has asked to see. (I.e. the name of a secific town) And if found to either highlight that link on the displayed web page or open tabs for any instance.
It sounds simple but I have no idea where to start or what language to code in (although I was thinking java script).
Found a bunch of simple free web apps but would like to do it from scratch.
Any help would be great
Sent from my GT-N7100
The app needs to be written in Java.
However, you can load a html page into a WebView and do the actual coding in the web page you display. That enables to do the simplest things in JavaScript.
If you want, send me your html file and I will create an app for you which displays this page.
But it will be a better learning experience if you do it yourself.
Thanks for the reply but the web page I have in mind is a news page for the fire service I work for.
It has links to incidents with more details. I wanted to load the page and search each of the links for my stations.
That way my wife only has to check her phone to see where I am.
I would love to do it all myself. Have some experience in vb but im not sure if you can program in that for android.
Plus I really want to learn java lol and I learn better by doing. If you can give me some pointers I would be very happy
Sent from my GT-N7100
jaffa1980 said:
Thanks for the reply but the web page I have in mind is a news page for the fire service I work for.
It has links to incidents with more details. I wanted to load the page and search each of the links for my stations.
That way my wife only has to check her phone to see where I am.
I would love to do it all myself. Have some experience in vb but im not sure if you can program in that for android.
Plus I really want to learn java lol and I learn better by doing. If you can give me some pointers I would be very happy
Sent from my GT-N7100
Click to expand...
Click to collapse
Look at the methods of WebView.
The text it searches for, is it all on that one html page? Can it find the identifying info in the URL of each link or will it need to actually load each linked page and search for the text in those?
Sent from my SAMSUNG-SGH-I727 using xda app-developers app
It would need to open each link in order to search I think.
Although it doesnt need to be made visible unless it matches the search criteria... I know that with the vb I could open a link in a new page but keep its visibility zero while it is manipulated... is that possible in java script?
Sent from my GT-N7100
jaffa1980 said:
It would need to open each link in order to search I think.
Although it doesnt need to be made visible unless it matches the search criteria... I know that with the vb I could open a link in a new page but keep its visibility zero while it is manipulated... is that possible in java script?
Sent from my GT-N7100
Click to expand...
Click to collapse
That makes it a bit harder. I don't know javascript very well but either way, I would do it using the Java WebView class to load the links. I have written an app once that used the JSoup library to download and parse the needed needed info from links, assemble them into a small summary page and displayed with WebView. I don't know if thats the best way but it worked for me...
You could search the html file for "<a href".
You basically want a web crawler^^
Regular expressions would be the right place to look, +1 for @nikwen 's answer.
I would basically do something like an edit text where the user inputs the words he wants to find on the webpage, and use regex to parse all <a href / </a> tags in a given html page which the app would previously have downloaded.
You could do something like this to parse the links (this will give you everything between ) :
Code:
File file = new File("/sdcard/index.html");
if(file.exists()) {
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
if (line.contains("<a href")
line.replaceAll("(<a href=\")(.+)(\")(>.*</a>)", "$1$3$4");
text.append(line);
text.append('\n');
//the text variable will contain all the links on your page by the end of the loop
}
}
catch (IOException e) {
}
You could then assign the text variable to a TextView which would have the android:autoLink="web" flag, thus displaying all links to the user
Thanks very much for your input, although i'm extremely new to java so its taking some time to get my head around the whole thing lol.
I have recently found Basic4Android which is very similar to Visual Basic. I have managed to pick that up a bit quicker than Java.
I was wondering if it is a popular language on here and if there are many people i may be able to ask specific questions re my app?
I have managed to "map out" what i want and even start the programming having designed several pages.
The thing i wondered about this is...
Once a web page is loaded are you able to programatically (is that a word?? ha ha) open links found on that web page (for me to search for a station).
the search string I am using, the user selects from a Spinner (drop down box) that i have populated.
once i have the links that have the mentioned stations i can use a tab view to have the main page and all incidents open together.
jaffa1980 said:
Thanks very much for your input, although i'm extremely new to java so its taking some time to get my head around the whole thing lol.
I have recently found Basic4Android which is very similar to Visual Basic. I have managed to pick that up a bit quicker than Java.
I was wondering if it is a popular language on here and if there are many people i may be able to ask specific questions re my app?
I have managed to "map out" what i want and even start the programming having designed several pages.
The thing i wondered about this is...
Once a web page is loaded are you able to programatically (is that a word?? ha ha) open links found on that web page (for me to search for a station).
the search string I am using, the user selects from a Spinner (drop down box) that i have populated.
once i have the links that have the mentioned stations i can use a tab view to have the main page and all incidents open together.
Click to expand...
Click to collapse
Since Java and C++ are the native languages for Android apps, there will not be that much devs which can help you.
Learn Java. It is a great language.
---------- Post added at 01:23 PM ---------- Previous post was at 01:22 PM ----------
And to open links programmatically: There is a method in WebView to load a page. Just load the new one.
If you're using a webview then any familiarity with JavaScript will help. You can find and download all the links on a page using Ajax, quite easily. For example, if you included the jQuery library in your page then you could do the following...
HTML:
$("a").each(function() {
$.ajax({
url: $(this).attr("href");
success: function(data) {
// do something with "data" here - it's a linked page
}
});
});
But, if you're familiar with basic then B4A may be the way for you to go. You'll need to learn how to inspect the DOM (document object model) which is the code behind a webpage.
Also, Stack Overflow is invaluable for quick help from experienced developers. A combination of this place and that (http://stackoverflow.com/questions/tagged/basic4android) will certainly get you going in the right direction. Just a word of warning.... If you're going to post on Stack Overflow then don't just ask "how do I do this?" It doesn't go down well. Show them what you have done and tell them what you want to achieve. They like people who try for themselves first
Good luck.
You definitely need Jsoup here! It is a very fast and easy to use (java) library.
Take a look at this code:
Code:
String URL = "yoururl";
String linkText;
String location;
Boolean stationFound;
ArrayList<String> locations = new ArrayList<String>();
// Get all links to incidents
Document doc = Jsoup.connect(URL).get();
// You have to take a look at the HTML and get some unique attribute to get all the incidents
// This can be a class/id/div etc.
Elements IncidentLinks = doc.select("a[class=someClassToSelectIncidents]");
// Now loop through all incident links
for(IncidentLinks : link){
[INDENT]
// Get the URL of the current detailed page
URL = link.attr("href");
// This is the more detailed page
doc = Jsoup.connect(URL).get();
// Get the HTML of the detailed page
String pagecontent = doc.toString();
// If your station is named in this HTML add it to the locations array
if(pagecontent.contains("Yourstation")){
[INDENT]
// Get the current location
// You need to find it yourself, take a look at the HTML
// It could be something like:
location = doc.select("div[class=location]").text();
// Add the location to the array
locations.add(location);[/INDENT]
}
[/INDENT]
}
// Now you have an array with all locations where your station is at the moment
// I dont know if your station can be at multiple locations at once
// You could loop through them and add them to a textview.
Thanks for all your advice i am starting to learn java, i have got eclipse and am watching all the video lessons i can grab lol i have a couple of books and a complete reference book 8...
I was wondering if i needed to reference this jsoup library before i can use it in eclipse.
Please be patient with me lol i have 2 jobs 3 kids and a prego wife so can be.... well, skatty is a polite way of putting it!
You just have to load the library into your project, it's very easy, just google it. If you have some programming experience you'll learn jav pretty fast. Good luck!
Sent from my NexusHD2 using xda app-developers app
jaffa1980 said:
Thanks for all your advice i am starting to learn java, i have got eclipse and am watching all the video lessons i can grab lol i have a couple of books and a complete reference book 8...
I was wondering if i needed to reference this jsoup library before i can use it in eclipse.
Please be patient with me lol i have 2 jobs 3 kids and a prego wife so can be.... well, skatty is a polite way of putting it!
Click to expand...
Click to collapse
Copy it into the libs folder of the project and it will be linked automatically.
Hi,
Still studying away i was wondering if there is a resource webpage i can download other projects to open in eclipse so that i can explore all the bits I'm reading about.
When i did VB i would often look at other projects with similar components as mine so i could get an idea on how to implement things properly.
I am getting the hang of the eclipse environment but there is so much more to the project area (resource files, xml and java etc).
Does anyone have any recommended sites for tutorials books for reading etc?
Cheers guys
jaffa1980 said:
Hi,
Still studying away i was wondering if there is a resource webpage i can download other projects to open in eclipse so that i can explore all the bits I'm reading about.
When i did VB i would often look at other projects with similar components as mine so i could get an idea on how to implement things properly.
I am getting the hang of the eclipse environment but there is so much more to the project area (resource files, xml and java etc).
Does anyone have any recommended sites for tutorials books for reading etc?
Cheers guys
Click to expand...
Click to collapse
I'm obviously assuming you have the Android SDK installed, so just look in there for the samples folder. It's full of demo apps to rip apart and see how they were made
ok im right fed up!!
i havent had time to be able to read or study or anything!!
would someone throw me a bone and upload a template based on the description i gave earlier... i know its freakin lazy but im having a **** ol time of it recently (health issues... hospital time etc) and could really do with the help...
the url of the newsdesk is
http://www.dsfire.gov.uk/News/Newsdesk/IncidentsPast7days.cfm?siteCategoryId=3&T1ID=26&T2ID=35
and as i said just the option of either viewing the page directly or if selected a station from a drop down box the main url shown on the first tab with other tabs populated by incidents with the selected station preferably from todays incidents. (see link)
i would consider this to be my luck turning and could really do with it
thanks
jaffa1980 said:
ok im right fed up!!
i havent had time to be able to read or study or anything!!
would someone throw me a bone and upload a template based on the description i gave earlier... i know its freakin lazy but im having a **** ol time of it recently (health issues... hospital time etc) and could really do with the help...
the url of the newsdesk is
http://www.dsfire.gov.uk/News/Newsdesk/IncidentsPast7days.cfm?siteCategoryId=3&T1ID=26&T2ID=35
and as i said just the option of either viewing the page directly or if selected a station from a drop down box the main url shown on the first tab with other tabs populated by incidents with the selected station preferably from todays incidents. (see link)
i would consider this to be my luck turning and could really do with it
thanks
Click to expand...
Click to collapse
Thanks for the link, now its more clear what you want.
At first take a look at the HTML of the webpage.
All incidents are listed per day as you can see in this image. The incidents are wrapped inside a DIV tag:
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
When we take a closer look, the link of an incident is stored in this TD tag.
So now we know the structure of the webpage we need to itterate through all the TD tags with the links in it.
If you look even closer you can see all the TD tags are inside a TABLE tag (ORLY??), BUT with the same class: class="tabularData".
The path to each page looks like this TABLE(class="tabularData") > TR > TD(first) > A
When you've got all the links you can visit the page and extract the word(s) you are looking for.
You can do this in two ways:
Use PHP/javascript/whatever to create your own website which displays the data. Then use WebView to display it in your own app.
Use Java to find the data and show it in an android layout (listview).
In PHP you want to use XPATH (http://stackoverflow.com/search?q=xpath)
In Java you want to use JSOUP (http://stackoverflow.com/search?q=JSOUP)
In Android it looks something like this:
HTML:
// Get the content of page
Document doc = Jsoup.connect("http://www.dsfire.gov.uk/News/Newsdesk/IncidentsPast7days.cfm?siteCategoryId=3&T1ID=26&T2ID=35").get();
// Select each day, this TABLE tag is inside the DIV tag you see in the first image.
Elements days = doc.select("table[class=tabularData]");
// Loop through each day
for(Element day : days){
// Select each incident
Elements incidents = day.select("tr");
// This loops through all URL's of the incidents.
for(Element incident : incidents){
// And get the url of the page to get more details of the incident
String url = incident.select("td").first().select("a")..attr("abs:href");
// Now get the content of the detailed page
String incidentpage = Jsoup.connect(url).get().toString();
// And check wether the page contains the string you are looking for
If(incidentpage.contains("Stringyouarelookingfor"){
// Do something
}
}
}
This is just a very rough piece of code, and it might not (probably doesn't) work. But I hope you get the idea and can go on by yourself.

java http to pull website data

hello guys
i am having trouble understanding how such apps work and what i need to start developing one
i've learned that you can pull website information using it's API in JSON format and display it
but if the website does not provide an API is possible to get the JSON data of any website? is it done through http request ?
i was also trying to connect my application to several news websites that keeps posting news update is it possible to read the information each time it is updated ?
and what should i know besides java to be able to connect my app to any website get information from this website and display the result regarding if it has an API or not
example : connect to a news website pull the newly posted news and display it or connect to a gaming website get the top 10 games and display them
thank you for your help
ghattas.akkad said:
hello guys
i am having trouble understanding how such apps work and what i need to start developing one
i've learned that you can pull website information using it's API in JSON format and display it
but if the website does not provide an API is possible to get the JSON data of any website? is it done through http request ?
i was also trying to connect my application to several news websites that keeps posting news update is it possible to read the information each time it is updated ?
and what should i know besides java to be able to connect my app to any website get information from this website and display the result regarding if it has an API or not
example : connect to a news website pull the newly posted news and display it or connect to a gaming website get the top 10 games and display them
thank you for your help
Click to expand...
Click to collapse
If the site you're scraping doesn't provide JSON then you can query the site using HTTP. Something like this.
Code:
URL url = new URL(target);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream in = new BufferedInputStream(connection.getInputStream());
laidlow said:
If the site you're scraping doesn't provide JSON then you can query the site using HTTP. Something like this.
Code:
URL url = new URL(target);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream in = new BufferedInputStream(connection.getInputStream());
Click to expand...
Click to collapse
thank youu
html parsing is very annoying especially when dealing with a lot of websites in a single app
i was looking for an easier method in which i can access the websites databases pull data and post it but i think if they dont have an API for that they wont give me that permission
ghattas.akkad said:
thank youu
html parsing is very annoying especially when dealing with a lot of websites in a single app
i was looking for an easier method in which i can access the websites databases pull data and post it but i think if they dont have an API for that they wont give me that permission
Click to expand...
Click to collapse
If you want an awesome library that helps you using http requests, take a look at Volley, which is really easy to use on all API levels. I didn't raed through it, but to me it sounds like you want to have some kind of RSS reader functionaly, take a look at this tutorial for instance.

Categories

Resources