WordPress media upload api on Android

In this post I will be writing about how to upload media on to word-press site using the Media api from wp-api library.

Again the reason for writing this is same, not enough info on this particular topic, and few people are stuck in the same issue I faced.

Another reason for writing is that, if you intend to code this up on to Java, it will work, but the Android HTTP classes are a bit different and the headers they were sending was slightly different than the same classes on Java.

Lets start.

For simplicity we will use a third party HTTP library, HttpRequest by Kevin.

In order to get the api work, at minimum we require 2 fields, 1st is the file and other the title.
Thus we get the following code:


HttpRequest request = HttpRequest.post(url);
request.authorization("Basic "+ah);
request.part("file", fName+".png", "image/png", new File(file));
request.part("title", "test");
if(request.code()==201) {
StringWriter sw = new StringWriter();
request.receive(sw);
onMedia(Media.parse(new JsonParser().parse(sw.toString()).getAsJsonObject()));
}else
onError(new Exception("Unable to upload."));
StringWriter sw = new StringWriter();
request.receive(sw);
Log.i(request.code()+" - "+request.toString());

I hope this will help a lot to those that have been struggling on to this.

Have a nice day.

Reading shoutcast stream on Android

In this post I will be writing about how to extract meta-data info from Shoutcast url.
The reason for writing this post is that not much info is available about this stuff on Android, thus without wasting any more time, lets begin.

The first step in this procedure is to call the url, with Icy-MetaData = 1, as it’s header.
So the code will become some thing like this:

//=== Fetch ICY INT
URL url = new URL(rurl);
URLConnection conn = url.openConnection();
conn.addRequestProperty("Icy-MetaData", "1");
conn.connect();

When you call the url with this header, you will receive a header in response, icy-metaint. This field indicates after how many bytes we have the meta-data, so we will have to skip these many bytes.


int discard = Integer.parseInt(hList.get(META_INT).get(0) + "");
InputStream is = conn.getInputStream();
long read = 0;
long skipped = is.skip(discard);
while (skipped != discard) {
skipped += is.skip(discard - skipped);
}

Its time to read the chunks of meta-data , the first byte *16 of this chunk represents the number of bytes that contain the data, rest are just padded.


//=== Read 1st byte
int fb = is.read();
//=== Read metadata, first byte *16
int mdl = fb * 16;
if(mdl>0){
read = 0;
String md = "";
do {
byte[] mdb = new byte[mdl - (int) read];
read = is.read(mdb);
md += new String(mdb, 0, mdb.length);
} while (read != mdl);
}

So we have our meta data now, time to parse it.
The data is separated by ‘;’ so we just have to split now.

String metdata = new String(md.trim().getBytes(), "utf-8");
//=== Extract
String[] data = metdata.split(";");
Tag t = new Tag();
for (String tag:data){
if(tag.startsWith(STREAM_TITLE))
t.setTitle(tag.split("=")[1]);
}

So the final code looks like this:


Hope you liked this post.
Thanks