java - Consume web service providing updating JSON object -


i need school project, need connect web service providing json document updating every 3 or 4 second, consume , use of information contained. json looks this:

{ "firstname": "john",
"lastname": "smith",
"isalive": true,
"age": 25,
"height_cm": 167.6,
"address": {
"streetaddress": "21 2nd street",
"city": "new york",
"state": "ny",
"postalcode": "10021-3100"
},
"phonecalls": [
{
"type": "home",
"number": "212 555-1234",
"duration": "32"
},
{
"type": "office",
"number": "646 555-4567",
"duration": "79"
}
]
}

every x second json file updated random calls added document, need use information.

i'm not sure how connect local web service, , retrieve information updating document, i'd use java let me know if there better solution.

thanks tips can provide me.

see example on how return jsonobject given url via http in java. includes support basic authentication, may or may not need it.

what call method using timer refresh feed needed.

import org.apache.http.httpentity; import org.apache.http.httpresponse; import org.apache.http.statusline; import org.apache.http.client.clientprotocolexception; import org.apache.http.client.httpclient; import org.apache.http.client.methods.httppost; import org.apache.http.impl.client.defaulthttpclient; import org.apache.http.params.basichttpparams; import org.apache.http.params.httpconnectionparams; import org.apache.http.params.httpparams; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import android.util.base64; 

... other imports

public static jsonobject readjsonfeed(string url, string username,         string password) throws keymanagementexception,         unrecoverablekeyexception, nosuchalgorithmexception,         keystoreexception, clientprotocolexception, ioexception {      string auth = username + ":" + password;     httpclient httpclient = = new defaulthttpclient;      stringbuilder stringbuilder = new stringbuilder();      // build http request     httppost httppost = new httppost(url);     httppost.setheader(             "authorization",             "basic "                     + base64.encodetostring(auth.getbytes(), base64.no_wrap));     httppost.setheader("accept", "application/json");      // send request     httpresponse response = httpclient.execute(httppost);      // read result     statusline statusline = response.getstatusline();     int statuscode = statusline.getstatuscode();     if (statuscode == 200) {         httpentity entity = response.getentity();         inputstream inputstream = entity.getcontent();         bufferedreader reader = new bufferedreader(new inputstreamreader(                 inputstream));         string line;         while ((line = reader.readline()) != null) {             stringbuilder.append(line);         }         inputstream.close();     } else if (statuscode == 401) {         throw new ioexception("authentication failed");     } else {         throw new ioexception(statusline.getstatuscode() + ":"                 + statusline.getreasonphrase());     }      // return json object     return new jsonobject(stringbuilder.tostring()); } 

after that, can retrieve data using methods of jsonobject class, such getint(string) or getstring(string). can obtain nested objects getjsonobject(string). providing string name of field/object parameter.


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -