Android Studio: how to use streamscraper, to get shoutcast metadata - android

I'm a newbie in Android Development. I want to get metadata from Shoutcast Server, and found streamscraper to be the easiest one to use. But my problem is, I don't know how to use it. The homepage itself only showing something like in how to use it:
import java.net.URI;
import java.util.List;
import net.moraleboost.streamscraper.Stream;
import net.moraleboost.streamscraper.Scraper;
import net.moraleboost.streamscraper.scraper.IceCastScraper;
public class Harvester {
public static void main(String[] args) throws Exception {
Scraper scraper = new IceCastScraper();
List streams = scraper.scrape(new URI("http://host:port/"));
for (Stream stream: streams) {
System.out.println("Song Title: " + stream.getCurrentSong());
System.out.println("URI: " + stream.getUri());
}
}
}
Searched anywhere and found no project sample of how to use this. I hope one of you can post the code of how to use it, or make a tutorial for it.

No need to use external libraries. The following pages give you:
Current song: http://yourstream:port/currentsong?sid=#
Last 20 songs: http://yourstream:port/played.html?sid#
Next songs: http://yourstream:port/nextsongs?sid=#
An Android java class which prints the current song:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
public class NowPlaying {
public void CurrentSong() {
try
{
URL url = new URL("http://www.mofosounds.com:8000/currentsong?sid=#");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
}
Note: the nextsongs?sid=#feature must be supported by the player of the stream.

Related

Trying to use class HttpsURLConnection in android studio, getting a error saying cannot resolve symbol HttpsURLConnection

I have a class developed in eclipse (on the same computer ) and I am trying to bring it over to Android Studio. Android Studio gives me an error, it cannot resolve symbol HttpsURLConnection
On Oracle's website and it is said the class is in java.net.URLConnection, when I import it the line is greyed saying it was already imported.
code:
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Date;
public class cBitTrex {
static int test;
/////////////////////////////////////////////////////////////////////
// Run code to talk to server on a thread
// Every onunce in a while code to tal to server will freex
// WEcall thread methed run and then wait for thread to exit with a time out
public class cThread extends Thread{
String reply=null;
String url;
public void run()
{
String ireply;
error="";
ireply="";
try {
ireply="";
URL myurl = new URL(url);
System.out.println("Open thread connection");
// This is where the error is
HttpsURLConnection con = (HttpsURLConnection)myurl.openConnection();
con.setConnectTimeout(15000);
System.out.println("Get input thread stream");
InputStream ins = con.getInputStream();
System.out.println("Create reader");
InputStreamReader isr = new InputStreamReader(ins);
System.out.println("Creat buffer threadreader");
BufferedReader in = new BufferedReader(isr);
String inputLine;
System.out.println("THREAD Read indata2");
int t;
while ((t= in.read()) != -1)
{
ireply+=(char)t;;
}
System.out.println("finish Read indata");
} catch (Exception e)
{
error=new String(e.getMessage());
System.out.println("Exception in getting data from API server");
reply=null;
}
// Set return valur at end of thread so if thread call tiimout we will not
// get any data
reply=ireply;
}
};
..
...
}
This class you are searching for does not exist, at least in Android. The one that is provided by the Android SDK is java.net.HttpURLConnection. By the way, something you really have to take into account is programming for Android is not just like normal Java, it has some special rules. The correct documentation to search at is the one located in the Android SDK official Page.
Beside that, your error can be that you were importing in Eclipse a Library that you are not using anymore in Android Studio.

How to see large debug message

I use android Log class for general purpose debugging but now the need is to display large text in the device itself as logcat message are truncated and inconvenient.
I tried to create a popup window with a textview inside but it seems like I cannot place it anywhere I want, if I do it may get WindowLeaked error etc.
I would like to place the debug window anywhere on the ui system such as anywhere in the activity class or Views.
Does android has any built-in debug-ui, something like
Dialog.debug(String message);
Otherwise what hack will require to achieve that?
EDIT
This I need not for viewing LogCat log but seeing any random variables in code.
Use this
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
class ReadLogDemo extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
Process process = Runtime.getRuntime().exec("logcat -d");
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(process.getInputStream()));
StringBuilder log=new StringBuilder();
String line = "";
while ((line = bufferedReader.readLine()) != null) {
log.append(line);
}
TextView tv = (TextView)findViewById(R.id.textView1);
tv.setText(log.toString());
} catch (IOException e) { }
}
}
Courtesy: log to be viewed in activity

NanoHTTPD server on desktop

I'm trying to serve a small file on my desktop using NanoHTTPD. The server starts fine but due to some unknown reason, it is unable to serve files. The same program works fine in Android. Can anyone give me some pointers? It's being more than an hour but I've got no clue. Here is my desktop version of NanoHTTPD server:
package com.desktopserver;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLConnection;
import java.util.Map;
import com.desktopserver.NanoHTTPD.Response.Status;
public class MainClass {
static int PORT = 8080;
static WebServer MyServer;
static FileInputStream fis;
static BufferedInputStream bis;
public static void main(String[] args) {
MyServer = new WebServer();
try {
MyServer.start();
System.out.println("Webserver Started # PORT:8080");
} catch (IOException e) {
e.printStackTrace();
}
}
public static class WebServer extends NanoHTTPD {
String MIME_TYPE;
public WebServer() {
super(PORT);
}
#Override
public Response serve(String uri, Method method,
Map<String, String> header, Map<String, String> parameters,
Map<String, String> files) {
try {
File file=new File("/home/evinish/Music/Meant_to_live.mp3");
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
MIME_TYPE= URLConnection.guessContentTypeFromName(file.getName());
System.out.println("\nMIME TYPE: "+MIME_TYPE);
System.out.println("\nFILE NAME: "+file.getName());
} catch (IOException ioe) {
System.out.println("File IO Exception");
}
return new NanoHTTPD.Response(Status.OK, MIME_TYPE, bis);
}
}
}
I do get this output, but that's it:
Webserver Started # PORT:8080
What am I missing here? Thanks a lot for your help.
Because you don't use "ServerRunner" class. ServerRunner hold you server to until any key press.
But in real application this don't work you want some change in NanoHTTPd file
line no 196 to
myThread.setDaemon(false);

How to retrieve data from spreadsheet in android?

I want the data from spreadsheet to my application so that i can use the data in my application. I want the data from the spread sheet having two columns i can use the data from the spread sheet.
Just like you do in java. Use Apache-POI or JExcelApi. JExcelAPI is easier to use, but POI is easier on memory. JExcel holds everything in memory. POI is better maintained.
You can use the code of James Moore: very-simple-google-spreadsheet-code
package com.banshee;
import java.io.IOException;
import java.net.URL;
import com.google.gdata.client.spreadsheet.SpreadsheetService;
import com.google.gdata.data.spreadsheet.CustomElementCollection;
import com.google.gdata.data.spreadsheet.ListEntry;
import com.google.gdata.data.spreadsheet.ListFeed;
import com.google.gdata.util.ServiceException;
public class SpreadsheetSucker {
public static void main(String[] args) {
SpreadsheetService service = new SpreadsheetService("com.banshee");
try {
// Notice that the url ends
// with default/public/values.
// That wasn't obvious (at least to me)
// from the documentation.
String urlString = "https://spreadsheets.google.com/feeds/list/0AsaDhyyXNaFSdDJ2VUxtVGVWN1Yza1loU1RPVVU3OFE/default/public/values";
// turn the string into a URL
URL url = new URL(urlString);
// You could substitute a cell feed here in place of
// the list feed
ListFeed feed = service.getFeed(url, ListFeed.class);
for (ListEntry entry : feed.getEntries()) {
CustomElementCollection elements = entry.getCustomElements();
String name = elements.getValue("name");
System.out.println(name);
String number = elements.getValue("Number");
System.out.println(number);
}
} catch (IOException e) {
e.printStackTrace();
} catch (ServiceException e) {
e.printStackTrace();
}
}
}

download image on android using http request in android

I have used a tutorial and arrived at this result which works in an emulator, but it ceases to work on a real Android device.
The code is given here and all other Android permissions required are set; internet and write to external device are set.
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import org.apache.http.util.ByteArrayBuffer;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
public class main extends Activity {
private final String PATH = "/mnt/sdcard/Pictures/";
TextView tv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.textView1);
DownloadFromUrl(PATH + "dwnldimg.png");
ImageView iv = (ImageView) findViewById(R.id.imageView1);
Bitmap bmp = BitmapFactory.decodeFile(PATH + "dwnldimg.png");
iv.setImageBitmap(bmp);
}
public void DownloadFromUrl(String fileName) {
try {
URL url = new URL("http://192.168.1.4/evilempire.jpg"); //you can write here any link
File file = new File(fileName);
long startTime = System.currentTimeMillis();
tv.setText("Starting download......from " + url);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();;
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
/*
* Read bytes to the Buffer until there is nothing more to read(-1).
*/
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
fos.close();
tv.setText("Download Completed in" + ((System.currentTimeMillis() - startTime) / 1000) + " sec");
} catch (IOException e) {
tv.setText("Error: " + e);
}
}
}
Prime lib will help you, but to learn Android you need to know AsyncTask. Android have the UI Thread where the Activity runs, so if you do complicate operation that block the Android OS will throw a Force Close.
So what you have to know is that if you need to do a download task or any operation that will eventually block the UI thread you can have to use Threads. The easy Android approach is AsyncTask.
I think this simple example will guide you to understand the AsyncTask.
http://developer.android.com/reference/android/os/AsyncTask.html
First of all your doing network operations on the main ui thread, which will result in a anr on pre-3.0 and a NetworkOnMainThreadException on 3.0+. Secondly I built Prime which is a whole lot easier at loading remote images.

Categories

Resources