Example usage for org.json JSONArray getJSONObject

List of usage examples for org.json JSONArray getJSONObject

Introduction

In this page you can find the example usage for org.json JSONArray getJSONObject.

Prototype

public JSONObject getJSONObject(int index) throws JSONException 

Source Link

Document

Get the JSONObject associated with an index.

Usage

From source file:nl.hnogames.domoticzapi.Parsers.TemperaturesParser.java

@Override
public void parseResult(String result) {

    try {// w w  w. ja  v  a  2  s  .c  om
        JSONArray jsonArray = new JSONArray(result);
        ArrayList<TemperatureInfo> mTemperatures = new ArrayList<>();

        if (jsonArray.length() > 0) {

            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject row = jsonArray.getJSONObject(i);
                mTemperatures.add(new TemperatureInfo(row));
            }
        }

        temperatureReceiver.onReceiveTemperatures(mTemperatures);

    } catch (JSONException e) {
        Log.e(TAG, "TemperatureParser JSON exception");
        e.printStackTrace();
        temperatureReceiver.onError(e);
    }
}

From source file:com.sakisds.icymonitor.activities.AddNotificationActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_add_notification);
    mSettings = getSharedPreferences(MainViewActivity.SHAREDPREFS_FILE, 0);

    // Prepare buttons
    findViewById(R.id.button_add).setOnClickListener(this);
    findViewById(R.id.button_cancel).setOnClickListener(this);
    findViewById(R.id.button_notification_help).setOnClickListener(this);

    // Prepare spinners
    mDeviceSpinner = (Spinner) findViewById(R.id.spinner_device);
    mTypeSpinner = (Spinner) findViewById(R.id.spinner_type);
    mSensorSpinner = (Spinner) findViewById(R.id.spinner_sensor);
    mConditionSpinner = (Spinner) findViewById(R.id.spinner_condition);

    // Edittext/*from ww w . j  a v  a 2  s . c  o  m*/
    mSensorValue = (EditText) findViewById(R.id.editText_value);

    // Checkbox
    mRingOnceCheckbox = (CheckBox) findViewById(R.id.checkbox_notif_ringonce);

    // URL
    mURL = getIntent().getExtras().getString(MainViewActivity.EXTRA_ADDRESS);

    // Client
    mClient.setMaxRetriesAndTimeout(2, 2000);

    // Retrieve sensors
    AsyncHttpClient client = new AsyncHttpClient();
    RequestParams params = new RequestParams();
    params.put("type", "listdev");
    params.put("id", String.valueOf(mSettings.getLong("device_id", -2)));

    final Activity context = this;
    final ProgressDialog progress = ProgressDialog.show(this, "",
            getResources().getString(R.string.retrieving_devices), false);

    mClient.get(mURL, params, new JsonHttpResponseHandler() {
        @Override
        public void onSuccess(JSONObject response) {
            try {
                // System
                JSONArray systemResponse = response.getJSONArray("System");

                int len = systemResponse.length();
                int lenTemp = 0;
                int lenFan = 0;
                int lenVolt = 0;

                for (int i = 0; i < len; i++) {
                    Sensor sensor = new Sensor(systemResponse.getJSONObject(i));

                    if (sensor.getType().equals("Temperature")) {
                        lenTemp++;
                    } else if (sensor.getType().equals("Fan")) {
                        lenFan++;
                    } else if (sensor.getType().equals("Voltage")) {
                        lenVolt++;
                    }
                }

                mSystemTempSensors = new Sensor[lenTemp];
                mSystemFanSensors = new Sensor[lenFan];
                mSystemVoltSensors = new Sensor[lenVolt];

                int iTemp = 0;
                int iFan = 0;
                int iVolt = 0;

                for (int i = 0; i < len; i++) {
                    Sensor sensor = new Sensor(systemResponse.getJSONObject(i));

                    if (sensor.getType().equals("Temperature")) {
                        mSystemTempSensors[iTemp] = sensor;
                        iTemp++;
                    } else if (sensor.getType().equals("Fan")) {
                        mSystemFanSensors[iFan] = sensor;
                        iFan++;
                    } else if (sensor.getType().equals("Voltage")) {
                        mSystemVoltSensors[iVolt] = sensor;
                        iVolt++;
                    }
                }

                // CPU
                JSONArray cpuResponse = response.getJSONArray("CPU");

                len = cpuResponse.length();
                lenTemp = 0;
                int lenLoad = 0;
                int lenClock = 0;
                int lenPower = 0;

                for (int i = 0; i < len; i++) {
                    Sensor sensor = new Sensor(cpuResponse.getJSONObject(i));

                    if (sensor.getType().equals("Temperature")) {
                        lenTemp++;
                    } else if (sensor.getType().equals("Load")) {
                        lenLoad++;
                    } else if (sensor.getType().equals("Clock")) {
                        lenClock++;
                    } else if (sensor.getType().equals("Power")) {
                        lenPower++;
                    }
                }

                mCPUTempSensors = new Sensor[lenTemp];
                mCPULoadSensors = new Sensor[lenLoad];
                mCPUPowerSensors = new Sensor[lenPower];
                mCPUClockSensors = new Sensor[lenClock];

                iTemp = 0;
                int iLoad = 0;
                int iPower = 0;
                int iClock = 0;

                for (int i = 0; i < len; i++) {
                    Sensor sensor = new Sensor(cpuResponse.getJSONObject(i));

                    if (sensor.getType().equals("Temperature")) {
                        mCPUTempSensors[iTemp] = sensor;
                        iTemp++;
                    } else if (sensor.getType().equals("Power")) {
                        mCPUPowerSensors[iPower] = sensor;
                        iPower++;
                    } else if (sensor.getType().equals("Clock")) {
                        mCPUClockSensors[iClock] = sensor;
                        iClock++;
                    } else if (sensor.getType().equals("Load")) {
                        mCPULoadSensors[iLoad] = sensor;
                        iLoad++;
                    }
                }

                // GPU
                JSONArray gpuResponse = response.getJSONArray("GPU");

                len = gpuResponse.length();
                lenTemp = 0;
                lenLoad = 0;
                lenClock = 0;
                lenFan = 0;

                for (int i = 0; i < len; i++) {
                    Sensor sensor = new Sensor(gpuResponse.getJSONObject(i));

                    if (sensor.getType().equals("Temperature")) {
                        lenTemp++;
                    } else if (sensor.getType().equals("Load")) {
                        lenLoad++;
                    } else if (sensor.getType().equals("Clock")) {
                        lenClock++;
                    } else if (sensor.getType().equals("Fan")) {
                        lenFan++;
                    }
                }

                mGPUTempSensors = new Sensor[lenTemp];
                mGPULoadSensors = new Sensor[lenLoad];
                mGPUFanSensors = new Sensor[lenFan];
                mGPUClockSensors = new Sensor[lenClock];

                iTemp = 0;
                iLoad = 0;
                iClock = 0;
                iFan = 0;

                for (int i = 0; i < len; i++) {
                    Sensor sensor = new Sensor(gpuResponse.getJSONObject(i));

                    if (sensor.getType().equals("Temperature")) {
                        mGPUTempSensors[iTemp] = sensor;
                        iTemp++;
                    } else if (sensor.getType().equals("Fan")) {
                        mGPUFanSensors[iFan] = sensor;
                        iFan++;
                    } else if (sensor.getType().equals("Clock")) {
                        mGPUClockSensors[iClock] = sensor;
                        iClock++;
                    } else if (sensor.getType().equals("Load")) {
                        mGPULoadSensors[iLoad] = sensor;
                        iLoad++;
                    }
                }

                // Prepare spinners
                setAdapter(mSensorSpinner, mSystemTempSensors);

                mTypeSpinner.setOnItemSelectedListener(new TypeOnItemSelectedListener());
                mDeviceSpinner.setOnItemSelectedListener(new DeviceOnItemSelectedListener());

                // Close window
                progress.hide();
            } catch (JSONException e) {
                Toast.makeText(context, getString(R.string.error_could_not_reach_host), Toast.LENGTH_LONG)
                        .show();
                progress.hide();
            }
        }

        @Override
        public void onFailure(Throwable error, String content) {
            Toast.makeText(context, getString(R.string.error_could_not_reach_host) + ":" + error.getMessage(),
                    Toast.LENGTH_LONG).show();
            finish();
            progress.hide();
            Log.e("Notif", error.getMessage());
        }
    });
}

From source file:br.com.indigo.android.facebook.SocialFacebook.java

private void handleFeedResponse(JSONObject jsonResponse, FeedListener listener) {
    FbSimplePost post;//  w  w w  . j  a v  a 2 s . c o  m
    ArrayList<FbSimplePost> posts = new ArrayList<FbSimplePost>();

    try {
        JSONArray objs = jsonResponse.getJSONArray("data");

        for (int i = 0; i < objs.length(); i++) {
            JSONObject obj = objs.getJSONObject(i);

            post = new FbSimplePost();
            post.setId(obj.getString("id"));

            JSONObject fromJson = obj.optJSONObject("from");
            if (fromJson != null) {
                FbSimpleUser from = new FbSimpleUser();
                from.setId(fromJson.getString("id"));
                from.setName(fromJson.getString("name"));

                post.setFrom(from);
            }

            post.setMessage(obj.optString("message"));
            post.setPicture(obj.optString("picture"));
            post.setLink(obj.optString("link"));
            post.setName(obj.optString("name"));
            post.setCaption(obj.optString("caption"));
            post.setDescription(obj.optString("description"));
            post.setSource(obj.optString("source"));
            post.setType(obj.optString("type"));

            post.setCreatedTime(new Date(obj.getLong("created_time")));
            post.setUpdatedTime(new Date(obj.getLong("updated_time")));

            JSONObject comments = obj.optJSONObject("comments");
            if (comments != null) {
                post.setNumberOfComments(comments.getInt("count"));
            }

            JSONObject likes = obj.optJSONObject("likes");
            if (likes != null) {
                post.setNumberOfLikes(likes.getInt("count"));
            }

            posts.add(post);
        }

        String nextPage = null;
        JSONObject paging = jsonResponse.optJSONObject("paging");
        if (paging != null) {
            nextPage = paging.optString("next");
        }

        listener.onComplete(posts, nextPage);

    } catch (JSONException e) {
        Util.logd(TAG, "Could not parse Json response", e);
        listener.onFail(e);
    }
}

From source file:br.com.indigo.android.facebook.SocialFacebook.java

private void handleUsersResponse(JSONObject jsonResponse, UsersListener listener) {

    FbSimpleUser user = null;//from  w  w w  .jav  a 2 s.c  o  m
    ArrayList<FbSimpleUser> friends = new ArrayList<FbSimpleUser>();

    try {
        JSONArray objs = jsonResponse.getJSONArray("data");

        for (int i = 0; i < objs.length(); i++) {
            JSONObject obj = objs.getJSONObject(i);

            user = new FbSimpleUser();
            user.setId(obj.getString("id"));
            user.setName(obj.getString("name"));

            String rsvpStatus = obj.optString("rsvp_status");

            if (rsvpStatus != null) {
                if (rsvpStatus.equals("not_replied")) {
                    user.setRSVPStatus(RSVP_STATUS.NOT_REPLIED);
                } else if (rsvpStatus.equals("attending")) {
                    user.setRSVPStatus(RSVP_STATUS.ATTENDING);
                } else if (rsvpStatus.equals("declined")) {
                    user.setRSVPStatus(RSVP_STATUS.DECLINED);
                } else if (rsvpStatus.equals("unsure")) {
                    user.setRSVPStatus(RSVP_STATUS.MAYBE);
                }
            }

            friends.add(user);
        }

        String nextPage = null;
        JSONObject paging = jsonResponse.optJSONObject("paging");
        if (paging != null) {
            nextPage = paging.optString("next");
        }

        listener.onComplete(friends, nextPage);

    } catch (JSONException e) {
        Util.logd(TAG, "Could not parse Json response", e);
        listener.onFail(e);
    }
}

From source file:br.com.indigo.android.facebook.SocialFacebook.java

private void handleEventsResponse(JSONObject jsonResponse, EventsListener listener) {

    ArrayList<FbEvent> events = new ArrayList<FbEvent>();

    try {/* ww w  .  ja va2 s  . co  m*/
        JSONArray objs = jsonResponse.getJSONArray("data");

        for (int i = 0; i < objs.length(); i++) {
            JSONObject obj = objs.getJSONObject(i);

            events.add(parseEvent(obj));
        }

        String nextPage = null;
        JSONObject paging = jsonResponse.optJSONObject("paging");
        if (paging != null) {
            nextPage = paging.optString("next");
        }

        listener.onComplete(events, nextPage);

    } catch (JSONException e) {
        Util.logd(TAG, "Could not parse Json response", e);
        listener.onFail(e);
    }
}

From source file:br.com.indigo.android.facebook.SocialFacebook.java

private void handleCommentsResponse(JSONObject jsonResponse, CommentsListener listener) {

    FbComment comment = null;//from   w  w  w .j av  a  2s .c  o  m
    ArrayList<FbComment> comments = new ArrayList<FbComment>();

    try {
        JSONArray objs = jsonResponse.getJSONArray("data");

        for (int i = 0; i < objs.length(); i++) {
            JSONObject obj = objs.getJSONObject(i);

            comment = new FbComment();
            comment.setId(obj.getString("id"));
            comment.setMessage(obj.optString("message"));
            comment.setCreatedTime(new Date(obj.optLong("created_time") * 1000));
            comment.setNumberOfLikes(obj.optInt("likes"));

            JSONObject fromJson = obj.optJSONObject("from");
            if (fromJson != null) {
                FbSimpleUser fromUser = new FbSimpleUser();
                fromUser.setId(fromJson.getString("id"));
                fromUser.setName(fromJson.optString("name"));

                comment.setFrom(fromUser);
            }

            comments.add(comment);
        }

        String nextPage = null;
        JSONObject paging = jsonResponse.optJSONObject("paging");
        if (paging != null) {
            nextPage = paging.optString("next");
        }

        listener.onComplete(comments, nextPage);

    } catch (JSONException e) {
        Util.logd(TAG, "Could not parse Json response", e);
        listener.onFail(e);
    }
}

From source file:br.com.indigo.android.facebook.SocialFacebook.java

/** Shingle **/
public void shingleConfiguration(final String url, final int area, final ShingleListener listener) {
    new Thread() {
        @Override/*w  w w  .  j a  va2s.c om*/
        public void run() {

            try {
                Bundle params = new Bundle();
                params.putString("area_id", String.valueOf(area));

                String response = Util.openUrl(url + "facebook/getFacebook", "GET", params);

                try {
                    JSONArray result = new JSONArray(response);

                    String profileId = result.getJSONObject(0).getString("account_number");
                    Log.d(TAG, "Shingle profileId = " + profileId);

                    listener.onComplete(profileId, false);
                } catch (JSONException e) {
                    Log.d(TAG, "Could not parse server response", e);
                    listener.onFail(new RuntimeException("Could not parse server response", e));
                }

            } catch (MalformedURLException e) {
                Log.d(TAG, "Error retrieving App Acess Token", e);
                listener.onFail(e);
            } catch (IOException e) {
                Log.d(TAG, "Error retrieving App Acess Token", e);
                listener.onFail(e);
            }
        }
    }.start();
}

From source file:com.ledger.android.u2f.bridge.MainActivity.java

private U2FContext parseU2FContextSign(JSONObject json) {
    try {/* w w w  .j a  v  a  2s  .c  o m*/
        String appId = json.getString(TAG_JSON_APPID);
        byte[] challenge = Base64.decode(json.getString(TAG_JSON_CHALLENGE), Base64.URL_SAFE);
        int requestId = json.getInt(TAG_JSON_REQUESTID);
        JSONArray array = json.getJSONArray(TAG_JSON_REGISTERED_KEYS);
        Vector<byte[]> keyHandles = new Vector<byte[]>();
        for (int i = 0; i < array.length(); i++) {
            JSONObject keyHandleItem = array.getJSONObject(i);
            if (!keyHandleItem.getString(TAG_JSON_VERSION).equals(VERSION_U2F_V2)) {
                Log.e(TAG, "Invalid handle version");
                return null;
            }
            byte[] keyHandle = Base64.decode(keyHandleItem.getString(TAG_JSON_KEYHANDLE), Base64.URL_SAFE);
            keyHandles.add(keyHandle);
        }
        return new U2FContext(appId, challenge, keyHandles, requestId, true);
    } catch (JSONException e) {
        Log.e(TAG, "Error decoding request");
        return null;
    }
}

From source file:com.ledger.android.u2f.bridge.MainActivity.java

private U2FContext parseU2FContextRegister(JSONObject json) {
    try {// w  ww .j  ava  2s.  c om
        byte[] challenge = null;
        String appId = json.getString(TAG_JSON_APPID);
        int requestId = json.getInt(TAG_JSON_REQUESTID);
        JSONArray array = json.getJSONArray(TAG_JSON_REGISTER_REQUESTS);
        for (int i = 0; i < array.length(); i++) {
            // TODO : only handle USB transport if several are present
            JSONObject registerItem = array.getJSONObject(i);
            if (!registerItem.getString(TAG_JSON_VERSION).equals(VERSION_U2F_V2)) {
                Log.e(TAG, "Invalid register version");
                return null;
            }
            challenge = Base64.decode(registerItem.getString(TAG_JSON_CHALLENGE), Base64.URL_SAFE);
        }
        return new U2FContext(appId, challenge, null, requestId, false);
    } catch (JSONException e) {
        Log.e(TAG, "Error decoding request");
        return null;
    }
}

From source file:com.jellymold.boss.ImageSearch.java

protected void parseResults(JSONObject jobj) throws JSONException {

    if (jobj != null) {

        setResponseCode(jobj.getInt("responsecode"));
        if (jobj.has("nextpage"))
            setNextPage(jobj.getString("nextpage"));
        if (jobj.has("prevpage"))
            setPrevPage(jobj.getString("prevpage"));
        setTotalResults(jobj.getLong("totalhits"));
        long count = jobj.getLong("count");
        setPagerCount(count);//w ww .  java  2s .  co m
        setPagerStart(jobj.getLong("start"));
        this.setResults(new ArrayList<ImageSearchResult>((int) count));

        if (jobj.has("resultset_images")) {

            JSONArray res = jobj.getJSONArray("resultset_images");
            for (int i = 0; i < res.length(); i++) {
                JSONObject thisResult = res.getJSONObject(i);
                ImageSearchResult imageSearchResult = new ImageSearchResult();
                imageSearchResult.setDescription(thisResult.getString("abstract"));
                imageSearchResult.setClickUrl(thisResult.getString("clickurl"));
                imageSearchResult.setDate(thisResult.getString("date"));
                imageSearchResult.setTitle(thisResult.getString("title"));
                imageSearchResult.setUrl(thisResult.getString("url"));
                imageSearchResult.setSize(thisResult.getLong("size"));
                imageSearchResult.setFilename(thisResult.getString("filename"));
                imageSearchResult.setFormat(thisResult.getString("format"));
                imageSearchResult.setHeight(thisResult.getLong("height"));
                imageSearchResult.setMimeType(thisResult.getString("mimetype"));
                imageSearchResult.setRefererClickUrl(thisResult.getString("refererclickurl"));
                imageSearchResult.setRefererUrl(thisResult.getString("refererurl"));
                imageSearchResult.setThumbnailHeight(thisResult.getLong("thumbnail_height"));
                imageSearchResult.setThumbnailWidth(thisResult.getLong("thumbnail_width"));
                imageSearchResult.setThumbnailUrl(thisResult.getString("thumbnail_url"));
                this.getResults().add(imageSearchResult);
            }
        }
    }

}