简体   繁体   中英

Thingspeak json Android Parse

I working on thingspeak.I have a json link. http://api.thingspeak.com/channels/145827/feed/last.json How can I do parse this for android.

Code to get the JSON,this works fine.

Class to get the JSON

    import android.util.Log;        
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.util.EntityUtils;
    import org.json.JSONArray;
    import org.json.JSONException;        
    import java.io.IOException;

    public class ApiConnector { //Connector class
        public JSONArray GetYourJson()
        {
            // URL for getting the json
            String url = "http://api.thingspeak.com/channels/145827/feed/last.json";

            // Get HttpResponse Object from url.
            // Get HttpEntity from Http Response Object
            HttpEntity httpEntity = null;

            try
            {
                DefaultHttpClient httpClient = new DefaultHttpClient();  // Default HttpClient
                HttpGet httpGet = new HttpGet(url);

                HttpResponse httpResponse = httpClient.execute(httpGet);

                httpEntity = httpResponse.getEntity();

            } catch (ClientProtocolException e) {

                // Signals error in http protocol
                e.printStackTrace();

                //Log Errors Here

            } catch (IOException e) {
                e.printStackTrace();
            }


            // Convert HttpEntity into JSON Array
            JSONArray jsonArray = null;

            if (httpEntity != null) {
                try {
                    String entityResponse = EntityUtils.toString(httpEntity);

                    Log.e("Entity Response  : ", entityResponse);

                    jsonArray = new JSONArray(entityResponse);

                } catch (JSONException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return jsonArray;
        }

    }

AsyckTaks to run ApiConnector, doing so your UI won't freeze.

    private class GetYourJsonTask extends AsyncTask<ApiConnector,Long,JSONArray>
    {
        @Override
        protected JSONArray doInBackground(ApiConnector... params) {

            // it is executed on Background thread

            return params[0].GetYourJson();
        }
        @Override
        protected void onPostExecute(JSONArray jsonArray) {
            //TODO: Do what you want with your json here
        }
    }

And then call this from UI

new GetYourJsonTask().execute(new ApiConnector());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM