Category Archives: Android

JSONArray and JSONObject Android

Having problems retrieving your data using java libraries JSONObject?
I am not surprised because its not so easy. This example will help you:

/* – here is an example JSON object from a URL (guardian API)
{
“response”:{
“status”:”ok”,
“userTier”:”free”,
“total”:1361206,
“startIndex”:1,
“pageSize”:10,
“currentPage”:1,
“pages”:136121,
“orderBy”:”newest”,
“results”:[{
“id”:”world/2011/aug/03/hosni-mubarak-way-trial”,
“sectionId”:”world”,
“sectionName”:”World news”,
“webPublicationDate”:”2011-08-03T07:11:00+01:00″,
“webTitle”:”Hosni Mubarak on way to face trial, says Egyptian official”,
“webUrl”:”http://www.guardian.co.uk/world/2011/aug/03/hosni-mubarak-way-trial”,
“apiUrl”:”http://content.guardianapis.com/world/2011/aug/03/hosni-mubarak-way-trial”
},{
“id”:”public-leaders-network/2011/aug/03/pfi-promises-rhetoric-reality”,
“sectionId”:”public-leaders-network”,
“sectionName”:”Public Leaders Network”,
“webPublicationDate”:”2011-08-03T07:00:04+01:00″,
“webTitle”:”PFI promises are more rhetoric than reality”,
“webUrl”:”http://www.guardian.co.uk/public-leaders-network/2011/aug/03/pfi-promises-rhetoric-reality”,
“apiUrl”:”http://content.guardianapis.com/public-leaders-network/2011/aug/03/pfi-promises-rhetoric-reality”
},{
“id”:”money/2011/aug/03/home-rental-costs-hit-record-highs”,
“sectionId”:”money”,
“sectionName”:”Money”,
“webPublicationDate”:”2011-08-03T07:00:03+01:00″,
“webTitle”:”Rents soar to record levels”,
“webUrl”:”http://www.guardian.co.uk/money/2011/aug/03/home-rental-costs-hit-record-highs”,
“apiUrl”:”http://content.guardianapis.com/money/2011/aug/03/home-rental-costs-hit-record-highs”
},{
“id”:”global-development/poverty-matters/2011/aug/03/africa-drought-uganda-no-somalia”,
“sectionId”:”global-development”,
“sectionName”:”Global development”,
“webPublicationDate”:”2011-08-03T07:00:02+01:00″,
“webTitle”:”East Africa drought: Uganda has problems, but it is no Somalia | Ben Jones”,
“webUrl”:”http://www.guardian.co.uk/global-development/poverty-matters/2011/aug/03/africa-drought-uganda-no-somalia”,
“apiUrl”:”http://content.guardianapis.com/global-development/poverty-matters/2011/aug/03/africa-drought-uganda-no-somalia”
},{
“id”:”money/2011/aug/03/virginia-wallis-redemption-charges”,
“sectionId”:”money”,
“sectionName”:”Money”,
“webPublicationDate”:”2011-08-03T07:00:01+01:00″,
“webTitle”:”Angry and confused over change in mortgage redemption charges”,
“webUrl”:”http://www.guardian.co.uk/money/2011/aug/03/virginia-wallis-redemption-charges”,
“apiUrl”:”http://content.guardianapis.com/money/2011/aug/03/virginia-wallis-redemption-charges”
},{
“id”:”money/2011/aug/03/virginia-wallis-house-flats-mortgage”,
“sectionId”:”money”,
“sectionName”:”Money”,
“webPublicationDate”:”2011-08-03T07:00:00+01:00″,
“webTitle”:”I want to convert flats back into a single house \u2013 what mortgage should I go for?”,
“webUrl”:”http://www.guardian.co.uk/money/2011/aug/03/virginia-wallis-house-flats-mortgage”,
“apiUrl”:”http://content.guardianapis.com/money/2011/aug/03/virginia-wallis-house-flats-mortgage”
},{
“id”:”world/2011/aug/03/china-calls-us-debt-manage”,
“sectionId”:”world”,
“sectionName”:”World news”,
“webPublicationDate”:”2011-08-03T06:45:36+01:00″,
“webTitle”:”China calls on US to manage its debt ‘responsibly’ from now on”,
“webUrl”:”http://www.guardian.co.uk/world/2011/aug/03/china-calls-us-debt-manage”,
“apiUrl”:”http://content.guardianapis.com/world/2011/aug/03/china-calls-us-debt-manage”
},{
“id”:”technology/blog/2011/aug/03/google-sergey-brin-2000-tv-show”,
“sectionId”:”technology”,
“sectionName”:”Technology”,
“webPublicationDate”:”2011-08-03T06:40:00+01:00″,
“webTitle”:”It’s 2000: can you spot the real Sergey Brin on the quizshow?”,
“webUrl”:”http://www.guardian.co.uk/technology/blog/2011/aug/03/google-sergey-brin-2000-tv-show”,
“apiUrl”:”http://content.guardianapis.com/technology/blog/2011/aug/03/google-sergey-brin-2000-tv-show”
},{
“id”:”environment/2011/aug/03/garden-birds-avian-pox-virus”,
“sectionId”:”environment”,
“sectionName”:”Environment”,
“webPublicationDate”:”2011-08-03T06:00:02+01:00″,
“webTitle”:”UK garden birds hit by avian pox virus”,
“webUrl”:”http://www.guardian.co.uk/environment/2011/aug/03/garden-birds-avian-pox-virus”,
“apiUrl”:”http://content.guardianapis.com/environment/2011/aug/03/garden-birds-avian-pox-virus”
},{
“id”:”voluntary-sector-network/2011/aug/03/charity-sector-management-recruitments”,
“sectionId”:”voluntary-sector-network”,
“sectionName”:”Voluntary Sector Network”,
“webPublicationDate”:”2011-08-03T06:00:02+01:00″,
“webTitle”:”Demand for interim managers in charity sector remains high, says recruitment specialist”,
“webUrl”:”http://www.guardian.co.uk/voluntary-sector-network/2011/aug/03/charity-sector-management-recruitments”,
“apiUrl”:”http://content.guardianapis.com/voluntary-sector-network/2011/aug/03/charity-sector-management-recruitments”
}] }}
*/

// retrieve your JSON somehow, I am using URLConnection. If you want the whole code email me.
URL twitter = new URL(“http://content.guardianapis.com/search?format=json”);
URLConnection tc = twitter.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
tc.getInputStream()));

//. put your JSON into a string:
StringBuilder sb = new StringBuilder();
String line;
while ((line = in.readLine()) != null) {
sb.append(line + “\n”);
}
// here is what you probably want most – its not so damn obvious. I got there by ‘trial and error’ 🙂
JSONObject jObject2 = new JSONObject(sb.toString());
JSONObject menuObject2 = jObject2.getJSONObject(“response”);
String attributeId12 = menuObject2.getString(“status”);
System.out.println(“value == ” +attributeId12);
String attributeId22 = menuObject2.getString(“userTier”);
System.out.println(“value == ” +attributeId22);
String attribute3 = menuObject2.getString(“results”);
System.out.println(“value == ” +attribute3);
JSONArray ja4 = jObject2.getJSONObject(“response”).getJSONArray(“results”);
System.out.println(“sub1_att = ” + ja4.getJSONObject(0).getString(“sectionId”).toString()+”\n\n”);
System.out.println(“sub1_att = ” + ja4.getJSONObject(0).getString(“sectionName”).toString()+”\n\n”);
for (int i=0;i<ja4.length();i++){
System.out.println("length = " + ja4.getJSONObject(i).getString("sectionName").toString()+"\n\n");
}

All the best with this JSONObject library. It does work but needs careful coding.
‘Like me’ if you appreciated this pls.

Java Thread wait and notify for Right Handed Thinkers

This code may be enough for you, but I will add a few words at the end. (note: if you use this code replace the WordPress ” with real ones)

*********************************
package com.multiple.threads;

public class Controller
{
public static void main(String[]args) throws InterruptedException
{
WorkingClass b1 =new WorkingClass();
Thread b = new Thread(b1);
b.start();
synchronized(b)
{
System.out.println(“1 Just about to call wait()” + b.getName());
b.wait();
System.out.println(“2 Just got notified”);}
System.out.println(“3 Ended here”);}}

class WorkingClass implements Runnable
{
public void run(){ synchronized (this){
System.out.println(“4 Starting a 4 sec sleep, nighty night.”);
try{ Thread.sleep(4000); }catch(Exception e){}
System.out.println(“5 Finished sleeping and calling notification”);
notify();
}}}
**********************************
output is:
1 Just about to call wait() Thread-0
4 Starting delay
5 Finished sleeping and calling notification
2 Just got notified
3 ended here

So, you have to synchronize your thread object and run() to get locks. Then the thread stops at the wait() stage and does nothing until notify() – after the task is finished. At which point it goes to the last stage in the program and finishes. – The task here is a 4 second sleep, but it could be getting data online.
Try it in your terminal window – it works.