What‘s the different of entry.softTtl and entry.ttl in volley? - java

In class HttpHeaderParser:
public static Cache.Entry parseCacheHeaders(NetworkResponse response) {
long now = System.currentTimeMillis();
Map<String, String> headers = response.headers;
long serverDate = 0;
long serverExpires = 0;
long softExpire = 0;
long maxAge = 0;
boolean hasCacheControl = false;
String serverEtag = null;
String headerValue;
headerValue = headers.get("Date");
if (headerValue != null) {
serverDate = parseDateAsEpoch(headerValue);
}
headerValue = headers.get("Cache-Control");
if (headerValue != null) {
hasCacheControl = true;
String[] tokens = headerValue.split(",");
for (int i = 0; i < tokens.length; i++) {
String token = tokens[i].trim();
if (token.equals("no-cache") || token.equals("no-store")) {
return null;
} else if (token.startsWith("max-age=")) {
try {
maxAge = Long.parseLong(token.substring(8));
} catch (Exception e) {
}
} else if (token.equals("must-revalidate") || token.equals("proxy-revalidate")) {
maxAge = 0;
}
}
}
headerValue = headers.get("Expires");
if (headerValue != null) {
serverExpires = parseDateAsEpoch(headerValue);
}
serverEtag = headers.get("ETag");
// Cache-Control takes precedence over an Expires header, even if both exist and Expires
// is more restrictive.
if (hasCacheControl) {
softExpire = now + maxAge * 1000;
} else if (serverDate > 0 && serverExpires >= serverDate) {
// Default semantic for Expire header in HTTP specification is softExpire.
softExpire = now + (serverExpires - serverDate);
}
Cache.Entry entry = new Cache.Entry();
entry.data = response.data;
entry.etag = serverEtag;
entry.softTtl = softExpire;
entry.ttl = entry.softTtl;
entry.serverDate = serverDate;
entry.responseHeaders = headers;
return entry;
}
entry.softTtl = softExpire;
entry.ttl = entry.softTtl;
This two variables has the same value, so why?
In class CacheDispatcher
#Override
public void run() {
...
...
// If it is completely expired, just send it to the network.
if (entry.isExpired()) {
request.addMarker("cache-hit-expired");
request.setCacheEntry(entry);
mNetworkQueue.put(request);
continue;
}
...
if (!entry.refreshNeeded()) {
// Completely unexpired cache hit. Just deliver the response.
mDelivery.postResponse(request, response);
} else {
...
// Post the intermediate response back to the user and have
// the delivery then forward the request along to the network.
mDelivery.postResponse(request, response, new Runnable() {
#Override
public void run() {
try {
mNetworkQueue.put(request);
} catch (InterruptedException e) {
// Not much we can do about this.
}
}
});
}
...
}
How can I differentiate between the cast of entry.isExpired() and entry.refreshNeeded() as the values are the same?

Have a look at this post : Group Google : Volley Users - soft and hard ttl on cache
refreshNeeded() and isExpired() are not exactly the same. One compares to the ttl value and the other to softTtl. This can be used to implement request semantics where you will return a response from cache even if it is "soft" expired, but will then go to the network and refresh, returning a new response if the data has changed.

Related

OkHttp doesn't get the entire JSON

My JSON file that I host in my VPS is 2.2 MB and when I use OkHttp to create a request to retrieve it and then log the JSON I see that not all the JSON was requested.
My code:
public void sendJSONRequest() {
// init http client
mOkHttpClient = new OkHttpClient();
// init a request
mRequest = new okhttp3.Request.Builder().url(url).build();
// execute the request (async)
mOkHttpClient.newCall(mRequest).enqueue(new Callback() {
#Override
public void onFailure(Call call, IOException e) {
Log.i(TAG, e.getMessage());
}
#Override
public void onResponse(Call call, okhttp3.Response response) throws IOException {
Log.i(TAG, response.body().string());
parseGameJSONResponse(response.body().string());
}
});
}
The error that gets throw within parseGameJSONResponse:
java.lang.IllegalStateException: closed
at okio.RealBufferedSource.rangeEquals(RealBufferedSource.java:398)
at okio.RealBufferedSource.rangeEquals(RealBufferedSource.java:392)
at okhttp3.internal.Util.bomAwareCharset(Util.java:449)
at okhttp3.ResponseBody.string(ResponseBody.java:174)
The error is thrown because the JSON was cut
parse json method:
public ArrayList<Game> parseGameJSONResponse(String json) {
ArrayList<Game> upcomingGames = new ArrayList<>();
// Main JSON Object
JSONObject mainJsonObject = null;
try {
mainJsonObject = new JSONObject(json);
} catch (JSONException e) {
e.printStackTrace();
}
boolean removeDuplicates = mSettingsValue.getRemoveDuplicates();
if (mainJsonObject != null) {
// MAIN JSON Data Array
JSONArray jsonArray = null;
try {
jsonArray = mainJsonObject.getJSONArray("data");
} catch (JSONException e) {
e.printStackTrace();
}
if (jsonArray != null && jsonArray.length() > 0) {
try {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject gameObject = jsonArray.getJSONObject(i);
Game game = new Game();
if (gameObject.has("id")) {
game.id = gameObject.getInt("id");
}
if (gameObject.has("name")) {
String name = gameObject.getString("name");
game.name = name;
if (name.endsWith("Edition") && removeDuplicates) {
// skip this iteration because it's a special edition and we don't want editions if setting is set to true
continue;
}
}
if (gameObject.has("slug")) {
// Creates the URL here
game.url = gameObject.getString("slug");
}
if (gameObject.has("updated_at")) {
game.updated_at = gameObject.getLong("updated_at");
}
if (gameObject.has("summary")) {
game.summary = gameObject.getString("summary");
}
if (gameObject.has("first_release_date")) {
game.first_release_date = gameObject.getLong("first_release_date");
}
// Game Release Dates
if (gameObject.has("release_dates")) {
JSONArray jsonReleaseDatesArray = gameObject.getJSONArray("release_dates");
ArrayList<ReleaseDate> releaseDates = new ArrayList<>();
for (int y = 0; y < jsonReleaseDatesArray.length(); y++) {
ReleaseDate releaseDate = new ReleaseDate();
JSONObject jsonReleaseDateObject = jsonReleaseDatesArray.getJSONObject(y);
if (jsonReleaseDateObject.has("category") && !jsonReleaseDateObject.isNull("category")) {
releaseDate.category = jsonReleaseDateObject.getInt("category");
}
if (jsonReleaseDateObject.has("platform") && !jsonReleaseDateObject.isNull("platform")) {
releaseDate.platform = jsonReleaseDateObject.getInt("platform");
}
if (jsonReleaseDateObject.has("date") && !jsonReleaseDateObject.isNull("date")) {
releaseDate.date = jsonReleaseDateObject.getLong("date");
}
if (jsonReleaseDateObject.has("region") && !jsonReleaseDateObject.isNull("region")) {
releaseDate.region = jsonReleaseDateObject.getInt("region");
// Toast.makeText(getContext(), releaseDate.region + ": Region", Toast.LENGTH_SHORT).show();
}
if (jsonReleaseDateObject.has("y") && !jsonReleaseDateObject.isNull("y")) {
releaseDate.year = jsonReleaseDateObject.getInt("y");
}
if (jsonReleaseDateObject.has("m") && !jsonReleaseDateObject.isNull("m")) {
releaseDate.month = jsonReleaseDateObject.getInt("m");
}
if (jsonReleaseDateObject.has("human") && !jsonReleaseDateObject.isNull("human")) {
releaseDate.human = jsonReleaseDateObject.getString("human");
}
releaseDates.add(releaseDate);
}
game.releaseDates = releaseDates;
}
// Screenshots
if (gameObject.has("screenshots")) {
JSONArray jsonScreenshotsArray = gameObject.getJSONArray("screenshots");
ArrayList<String> screenshots = new ArrayList<>();
for (int y = 0; y < jsonScreenshotsArray.length(); y++) {
JSONObject jsonScreenshotObject = jsonScreenshotsArray.getJSONObject(y);
screenshots.add(jsonScreenshotObject.getString("cloudinary_id"));
}
game.screenshots = screenshots;
}
// Videos
if (gameObject.has("videos")) {
ArrayList<String> videos = new ArrayList<>();
JSONArray jsonVideosArray = gameObject.getJSONArray("videos");
for (int y = 0; y < jsonVideosArray.length(); y++) {
JSONObject jsonVideoObject = jsonVideosArray.getJSONObject(y);
videos.add(jsonVideoObject.getString("video_id"));
}
game.videos = videos;
}
// Cover image
if (gameObject.has("cover")) {
JSONObject jsonCoverObject = gameObject.getJSONObject("cover");
game.cover = jsonCoverObject.getString("cloudinary_id");
}
// Websites
if (gameObject.has("websites")) {
JSONArray jsonWebsitesArray = gameObject.getJSONArray("websites");
ArrayList<Website> websites = new ArrayList<>();
for (int y = 0; y < jsonWebsitesArray.length(); y++) {
Website website = new Website();
JSONObject jsonWebsiteObject = jsonWebsitesArray.getJSONObject(y);
website.category = jsonWebsiteObject.getInt("category");
website.url = jsonWebsiteObject.getString("url");
websites.add(website);
}
game.websites = websites;
}
upcomingGames.add(game);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
Toast.makeText(getContext(), "" + upcomingGames.size(), Toast.LENGTH_SHORT).show();
return upcomingGames;
}
Thank you guys. Really appreciate any kind of help so thanks
It seems it tries to read same InputStream twice (may not save in memory).
I think you should use just response.string() instead of response.body().string().
Also if you think it might be related to timing you can edit timeouts.
client = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.build();
For more look at this.
https://github.com/square/okhttp/issues/1240

paging in Java for a lookup

I am trying to implement paging in an already running application, it works like mongodb where you get a query with bunch of parameters like
searchConditions ,limit( how many docs you want), skip ( skip these many docs)
and so on
and the response is back in json my task is to generate the previous and next link to the query that means previous page and the next page as per query
I came up with a method below
public Paging generateLink(RequestFilter filter) {
int prev_skip;
int prev_limit;
String pagePrev = "";
String pageNext = "";
int next_skip;
int next_limit;
String searchCondition = JsonUtils.toSimpleJsonString(filter.getSearch());
String url = "http://isgswmdi1n1.nam.nsroot.net:7014/account-search/rest/api/v1/pmm";
//properties.getProperty("paging.link");
System.out.println(url);
Paging pagingOption = new Paging();
Result result = new Result();
int count = luceneSearchService.searchCount(filter);
try {
if (count > 1) {
if (filter.getSkip() > 0) {
prev_skip = Math.max((filter.getSkip() - 1), 0);
prev_limit = filter.getLimit();
pagePrev = url + "?search=" + searchCondition + "&skip=" + prev_skip + "$limit=" + prev_limit;
} else{
result.setPaging(null);
return null;
}
if (count > (filter.getLimit() + filter.getSkip())) {
next_skip = (filter.getSkip() + filter.getLimit());
next_limit = filter.getLimit();
pageNext = url + "?search=" + searchCondition + "&skip=" + next_skip + "$limit=" + next_limit;
} else{
result.setPaging(null);
return null;
}
pagingOption.setPagePrev(pagePrev);
pagingOption.setPageNext(pageNext);
result.setPaging(pagingOption);
}
return pagingOption;
} catch (NullPointerException n)
{
n.printStackTrace();
return pagingOption;
}
}
call to generateLink method
return Result.ok(resultRow, generateLink(filter));
the ok response method in the Result class
public static Result ok(List<ResultRow> resultRows, Paging paging) {
if (paging != null) {
return new Result(resultRows, 200, "Completed", paging);
} else
return new Result(resultRows, 200, "Completed");
}
Underlying paging class
public class Paging implements Serializable {
public String getPagePrev() {
return pagePrev;
}
public void setPagePrev(String pagePrev) {
this.pagePrev = pagePrev;
}
#JsonProperty("pagePrev")
private String pagePrev;
public String getPageNext() {
return pageNext;
}
public void setPageNext(String pageNext) {
this.pageNext = pageNext;
}
#JsonProperty("pageNext")
private String pageNext;
}
but my test method is failing
#Test
public void search_allFilterParamsAreDefined_hp() throws Exception {
// given
Map<String, Serializable> searchConditions = singletonMap("shortName", "GO");
List<String> returnFields = asList("shortname", "gfcid");
String returnFieldsStr = returnFields.stream().collect(joining(","));
RequestFilter filter = RequestFilter.create(searchConditions, returnFieldsStr, 5, 10, true);
int resultSize = 20;
List<ResultRow> searchResult = getSearchResult(resultSize);
when(luceneSearchService.search(filter)).thenReturn(searchResult);
when(luceneSearchService.searchCount(filter)).thenReturn(resultSize);
// do
Result result = searchCoordinator.search(filter);
// verify
assertEquals(searchResult, result.getRows());
assertReturnFields(returnFields, result.getRows());
assertNotNull(result.getPaging());
Map<String, String> nextParams = getQueryParams(result.getPaging().getPageNext());
assertParam(nextParams, "search", toSimpleJsonString(searchConditions));
assertParam(nextParams, "skip", "15");
assertParam(nextParams, "limit", "10");
}

Get metadata from shoutcast stream

I'm developing a radio app with multiple radios, the stream is playing fine. But I'm struggling to show artist and music playing at the moment.
This is the class I'm using to get metadata from shoutcast stream:
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class IcyStreamMeta<Message> {
protected URL streamUrl;
private Map<String, String> metadata;
private boolean isError;
public IcyStreamMeta(URL streamUrl) {
setStreamUrl(streamUrl);
isError = false;
}
/**
* Get artist using stream's title
*
* #return String
* #throws IOException
*/
public String getArtist() throws IOException {
Map<String, String> data = getMetadata();
if (!data.containsKey("StreamTitle"))
return "";
String streamTitle = data.get("StreamTitle");
String title = streamTitle.substring(0, streamTitle.indexOf("-"));
return title.trim();
}
/**
* Get title using stream's title
*
* #return String
* #throws IOException
*/
public String getTitle() throws IOException {
Map<String, String> data = getMetadata();
if (!data.containsKey("StreamTitle"))
return "";
String streamTitle = data.get("StreamTitle");
String artist = streamTitle.substring(streamTitle.indexOf("-")+1);
return artist.trim();
}
public Map<String, String> getMetadata() throws IOException {
if (metadata == null) {
refreshMeta();
}
return metadata;
}
public void refreshMeta() throws IOException {
retreiveMetadata();
}
private void retreiveMetadata() throws IOException {
URLConnection con = streamUrl.openConnection();
con.setRequestProperty("Icy-MetaData", "1");
con.setRequestProperty("Connection", "close");
con.setRequestProperty("Accept", null);
con.connect();
int metaDataOffset = 0;
Map<String, List<String>> headers = con.getHeaderFields();
InputStream stream = con.getInputStream();
if (headers.containsKey("icy-metaint")) {
// Headers are sent via HTTP
metaDataOffset = Integer.parseInt(headers.get("icy-metaint").get(0));
} else {
// Headers are sent within a stream
StringBuilder strHeaders = new StringBuilder();
char c;
while ((c = (char)stream.read()) != -1) {
strHeaders.append(c);
if (strHeaders.length() > 5 && (strHeaders.substring((strHeaders.length() - 4), strHeaders.length()).equals("\r\n\r\n"))) {
// end of headers
break;
}
}
// Match headers to get metadata offset within a stream
Pattern p = Pattern.compile("\\r\\n(icy-metaint):\\s*(.*)\\r\\n");
Matcher m = p.matcher(strHeaders.toString());
if (m.find()) {
metaDataOffset = Integer.parseInt(m.group(2));
}
}
// In case no data was sent
if (metaDataOffset == 0) {
isError = true;
return;
}
// Read metadata
int b;
int count = 0;
int metaDataLength = 4080; // 4080 is the max length
boolean inData = false;
StringBuilder metaData = new StringBuilder();
// Stream position should be either at the beginning or right after headers
while ((b = stream.read()) != -1) {
count++;
// Length of the metadata
if (count == metaDataOffset + 1) {
metaDataLength = b * 16;
}
if (count > metaDataOffset + 1 && count < (metaDataOffset + metaDataLength)) {
inData = true;
} else {
inData = false;
}
if (inData) {
if (b != 0) {
metaData.append((char)b);
}
}
if (count > (metaDataOffset + metaDataLength)) {
break;
}
}
// Set the data
metadata = IcyStreamMeta.parseMetadata(metaData.toString());
// Close
stream.close();
}
public boolean isError() {
return isError;
}
public URL getStreamUrl() {
return streamUrl;
}
public void setStreamUrl(URL streamUrl) {
this.metadata = null;
this.streamUrl = streamUrl;
this.isError = false;
}
public static Map<String, String> parseMetadata(String metaString) {
Map<String, String> metadata = new HashMap();
String[] metaParts = metaString.split(";");
Pattern p = Pattern.compile("^([a-zA-Z]+)=\\'([^\\']*)\\'$");
Matcher m;
for (int i = 0; i < metaParts.length; i++) {
m = p.matcher(metaParts[i]);
if (m.find()) {
metadata.put(m.group(1), m.group(2));
}
}
return metadata;
}
}
And the method on MainActivity to get the metadata every 10 seconds
private void getMeta()
{
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
try {
IcyStreamMeta icy = new IcyStreamMeta(new URL(RadiophonyService.getRadioURL()));
final String data = icy.getArtist() + " - " + icy.getTitle();
final TextView meta = (TextView) findViewById(R.id.now_playing);
runOnUiThread(new Runnable() {
public void run() {
meta.setText(data);
}
});
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}, 0, 10000);
}
Initially, when I select one station, it plays but does not show metadata and when I select another station the app crashes with this runtime error:
E/AndroidRuntime: FATAL EXCEPTION: Timer-0
Process: com.example.app, PID: 23597
java.lang.StringIndexOutOfBoundsException: length=0; regionStart=0; regionLength=-1
at java.lang.String.startEndAndLength(String.java:504)
at java.lang.String.substring(String.java:1333)
at com.example.app.utilities.IcyStreamMeta.getArtist(IcyStreamMeta.java:41)
at com.example.app.activities.MainActivity$8.run(MainActivity.java:306)
at java.util.Timer$TimerImpl.run(Timer.java:284)
I'm stuck at this for days, and I tried other solutions but nothing works!
Looks like this line
String title = streamTitle.substring(0, streamTitle.indexOf("-"));
is the culprit.
If streamTitle does not have a dash in its title, indexOf() will return a -1, and substring() is choking because you can't have an end index less than the start index.
Maybe you need something like this:
int pos = streamTitle.indexOf("-");
String title = (pos == -1) ? streamTitle : streamTitle.substring(0, pos);

Get complexTypes or parameters from WSDL file

How to get complex types from WSDL file programatically?
I have to get complex types from Adobe echo sign.
https://secure.echosign.com/services/EchoSignDocumentService19?wsdl
There are already some questions on this topic but none has useful answer. So, I am invoking this question again.
This is the code I have written, but no luck.
public class wsdlReader {
public static void main(String[] args) {
try {
WSDLFactory factory = WSDLFactory.newInstance();
WSDLReader reader = factory.newWSDLReader();
reader.setFeature("javax.wsdl.verbose", false);
reader.setFeature("javax.wsdl.importDocuments", true);
Definition def = reader.readWSDL(null, "https://secure.echosign.com/services/EchoSignDocumentService19?wsdl");
Map services = def.getServices();
Iterator servicesIterator = services.values().iterator();
def.getTypes();
while (servicesIterator.hasNext())
{
Service service = (Service) servicesIterator.next();
Map ports = service.getPorts();
Iterator portsIterator = ports.keySet().iterator();
while (portsIterator.hasNext())
{
String strPort = portsIterator.next().toString();
Port port = service.getPort(strPort);
Binding binding = port.getBinding();
PortType portType = binding.getPortType();
List operations = portType.getOperations();
Iterator opIterator = operations.iterator();
while (opIterator.hasNext())
{
Operation operation = (Operation) opIterator.next();
if (!operation.isUndefined())
{
Input inDef = operation.getInput();
Map params = operation.getInput().getMessage().getParts();
Iterator paramsIterator = params.keySet().iterator();
int n = 1;
StringBuffer sbParams = new StringBuffer();
while (paramsIterator.hasNext())
{
PartImpl iParam = (PartImpl) params.get(paramsIterator.next());
if (iParam.getTypeName() == null) {
sbParams.append(iParam.getElementName().getLocalPart()).append(" p").append(n).append(",");
n++;
} else if (iParam.getElementName() == null) {
sbParams.append(iParam.getTypeName().getLocalPart()).append(" ").append(iParam.getName()).append(", ");
} else {
System.err.println("sicis .:.");
}
}
System.out.println(sbParams);
if (sbParams.length() > 0) {
sbParams.delete(sbParams.length() - 1, sbParams.length());
}
}
}
}
}
} catch (WSDLException e) {
e.printStackTrace();
}
}
}
Any help or suggestion will be really helpful.
Thanks

Getting metadata from SHOUTcast using IcyStreamMeta

I am writing an app for Android that grabs meta data from SHOUTcast mp3 streams. I am using a pretty nifty class I found online that I slightly modified, but I am still having 2 problems.
1) I have to continuously ping the server to update the metadata using a TimerTask. I am not fond of this approach but it was all I could think of.
2) There is a metric tonne of garbage collection while my app is running. Removing the TimerTask got rid of the garbage collection issue so I am not sure if I am just doing it wrong or if this is normal.
Here is the class I am using:
public class IcyStreamMeta {
protected URL streamUrl;
private Map<String, String> metadata;
private boolean isError;
public IcyStreamMeta(URL streamUrl) {
setStreamUrl(streamUrl);
isError = false;
}
/**
* Get artist using stream's title
*
* #return String
* #throws IOException
*/
public String getArtist() throws IOException {
Map<String, String> data = getMetadata();
if (!data.containsKey("StreamTitle"))
return "";
try {
String streamTitle = data.get("StreamTitle");
String title = streamTitle.substring(0, streamTitle.indexOf("-"));
return title.trim();
}catch (StringIndexOutOfBoundsException e) {
return "";
}
}
/**
* Get title using stream's title
*
* #return String
* #throws IOException
*/
public String getTitle() throws IOException {
Map<String, String> data = getMetadata();
if (!data.containsKey("StreamTitle"))
return "";
try {
String streamTitle = data.get("StreamTitle");
String artist = streamTitle.substring(streamTitle.indexOf("-")+1);
return artist.trim();
} catch (StringIndexOutOfBoundsException e) {
return "";
}
}
public Map<String, String> getMetadata() throws IOException {
if (metadata == null) {
refreshMeta();
}
return metadata;
}
public void refreshMeta() throws IOException {
retreiveMetadata();
}
private void retreiveMetadata() throws IOException {
URLConnection con = streamUrl.openConnection();
con.setRequestProperty("Icy-MetaData", "1");
con.setRequestProperty("Connection", "close");
//con.setRequestProperty("Accept", null);
con.connect();
int metaDataOffset = 0;
Map<String, List<String>> headers = con.getHeaderFields();
InputStream stream = con.getInputStream();
if (headers.containsKey("icy-metaint")) {
// Headers are sent via HTTP
metaDataOffset = Integer.parseInt(headers.get("icy-metaint").get(0));
} else {
// Headers are sent within a stream
StringBuilder strHeaders = new StringBuilder();
char c;
while ((c = (char)stream.read()) != -1) {
strHeaders.append(c);
if (strHeaders.length() > 5 && (strHeaders.substring((strHeaders.length() - 4), strHeaders.length()).equals("\r\n\r\n"))) {
// end of headers
break;
}
}
// Match headers to get metadata offset within a stream
Pattern p = Pattern.compile("\\r\\n(icy-metaint):\\s*(.*)\\r\\n");
Matcher m = p.matcher(strHeaders.toString());
if (m.find()) {
metaDataOffset = Integer.parseInt(m.group(2));
}
}
// In case no data was sent
if (metaDataOffset == 0) {
isError = true;
return;
}
// Read metadata
int b;
int count = 0;
int metaDataLength = 4080; // 4080 is the max length
boolean inData = false;
StringBuilder metaData = new StringBuilder();
// Stream position should be either at the beginning or right after headers
while ((b = stream.read()) != -1) {
count++;
// Length of the metadata
if (count == metaDataOffset + 1) {
metaDataLength = b * 16;
}
if (count > metaDataOffset + 1 && count < (metaDataOffset + metaDataLength)) {
inData = true;
} else {
inData = false;
}
if (inData) {
if (b != 0) {
metaData.append((char)b);
}
}
if (count > (metaDataOffset + metaDataLength)) {
break;
}
}
// Set the data
metadata = IcyStreamMeta.parseMetadata(metaData.toString());
// Close
stream.close();
}
public boolean isError() {
return isError;
}
public URL getStreamUrl() {
return streamUrl;
}
public void setStreamUrl(URL streamUrl) {
this.metadata = null;
this.streamUrl = streamUrl;
this.isError = false;
}
public static Map<String, String> parseMetadata(String metaString) {
Map<String, String> metadata = new HashMap<String, String>();
String[] metaParts = metaString.split(";");
Pattern p = Pattern.compile("^([a-zA-Z]+)=\\'([^\\']*)\\'$");
Matcher m;
for (int i = 0; i < metaParts.length; i++) {
m = p.matcher(metaParts[i]);
if (m.find()) {
metadata.put((String)m.group(1), (String)m.group(2));
}
}
return metadata;
}
}
And here is my timer:
private void getMeta() {
timer.schedule(new TimerTask() {
public void run() {
try {
icy = new IcyStreamMeta(new URL(stationUrl));
runOnUiThread(new Runnable() {
public void run() {
try {
artist.setText(icy.getArtist());
title.setText(icy.getTitle());
} catch (IOException e) {
e.printStackTrace();
} catch (StringIndexOutOfBoundsException e) {
e.printStackTrace();
}
}
});
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
},0,5000);
}
Much appreciation for any assistance!
I've replaced the IcyStreamMeta class in my program and am getting the meta data from the 7.html file that is a part of the SHOUTcast spec. Far less data usage and all that so I feel it is a better option.
I am still using the TimerTask, which is acceptable. There is practically no GC any more and I am happy with using 7.html and a little regex. :)

Categories

Resources