• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps JSON Examples?

I foind this on google but is it the ideal way to do things?

Code:
String result = queryRESTurl("http://location/of/wellformed/json.json");
 
        try{
                JSONObject json = new JSONObject(result);
                JSONArray nameArray = json.names();
                JSONArray valArray = json.toJSONArray(nameArray);
                for (int i = 0; i < valArray.length(); i++) {
                        Log.i(TAG, "<jsonname" + i + ">&#092;n" + nameArray.getString(i)        + "&#092;n</jsonname" + i + ">&#092;n" + "<jsonvalue" + i + ">&#092;n" + valArray.getString(i) + "&#092;n</jsonvalue"   + i + ">");
                }
        }
        catch (JSONException e) {
                Log.e("JSON", "There was an error parsing the JSON", e);
        }
 
 
 
public String queryRESTurl(String url) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet(url);
        HttpResponse response;
       
        try {
                response = httpclient.execute(httpget);
                Log.i(TAG, "Status:[" + response.getStatusLine().toString() + "]");
                HttpEntity entity = response.getEntity();
               
                if (entity != null) {
                       
                        InputStream instream = entity.getContent();
                        String result = RestClient.convertStreamToString(instream);
                        Log.i(TAG, "Result of converstion: [" + result + "]");
                       
                        instream.close();
                        return result;
                }
        } catch (ClientProtocolException e) {
                Log.e("REST", "There was a protocol based error", e);
        } catch (IOException e) {
                Log.e("REST", "There was an IO Stream related error", e);
        }
       
        return null;
}
Android Development Source Code Respository | Retrieve JSON from a REST web service
 
Back
Top Bottom